pytest_skill

This skill helps you write robust Python tests with pytest by guiding fixtures, mocking, parametrization, and markers.
  • Shell

1.3k

GitHub Stars

1

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill gentleman-programming/gentleman.dots --skill pytest

  • SKILL.md4.2 KB

Overview

This skill provides concise pytest testing patterns and best practices for writing Python tests. It covers test structure, fixtures, mocking, parametrization, markers, async testing, and common pytest commands. Use it to structure reliable, maintainable test suites and speed up test authoring.

How this skill works

It outlines common pytest idioms and examples you can copy into your codebase: class-based and function tests, reusable fixtures with scope and teardown, and shared conftest.py fixtures. It demonstrates mocking with unittest.mock, parameterized test cases with pytest.mark.parametrize, and marker usage for selective runs. It also lists practical pytest CLI flags for running and debugging tests.

When to use it

  • When creating unit and integration tests for Python projects
  • When you need reusable test setup or teardown via fixtures
  • When isolating external dependencies with mocks or MagicMock
  • When validating multiple inputs using parametrized tests
  • When organizing or filtering tests with markers or running async tests

Best practices

  • Keep tests small and focused: one assertion intent per test where practical
  • Use fixtures in conftest.py for shared setup to avoid duplication
  • Prefer parametrization over loops to get individual test reports per case
  • Mock external services (HTTP, payments) to avoid flaky or slow tests
  • Use marker names (slow, integration) and pytest -m to control test runs
  • Clean up resources in fixture teardown (yield) and choose fixture scope wisely

Example use cases

  • Unit test a service by patching external clients with patch and MagicMock
  • Create authenticated test clients via fixtures that force login and teardown DB session fixtures after tests
  • Validate input handling with @pytest.mark.parametrize for many edge cases
  • Mark long-running database or network tests with @pytest.mark.integration and exclude them in fast CI runs
  • Run tests in parallel with pytest -n auto and gather coverage with pytest --cov=src

FAQ

Put fixtures in tests/conftest.py. Pytest auto-discovers those fixtures for any tests under the tests directory.

When should I mock vs use a real dependency?

Mock external services and network calls to keep unit tests fast and deterministic. Use real integrations selectively for integration tests with markers like integration.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational
pytest skill by gentleman-programming/gentleman.dots | VeilStrat