Python Testing Patterns
Comprehensive guide to implementing robust testing strategies in Python using pytest, fixtures, mocking, parameterization, and property-based testing.
Test Quality Gate
Before adding or keeping a test, ask:
- What behavior does this test prove?
- Which module owns that behavior?
- Could this fail because an upstream or downstream layer changed?
- Is this exact string or object comparison testing public contract or incidental formatting?
- Does this duplicate another test at a higher or lower layer?
- Can this be a direct unit test instead of full workflow setup?
- Are shared fixtures hiding business examples that should be local to the test?
- Are we checking all fields because they matter, or because object comparison was easy?
- Would a helper reduce noise without hiding behavior?
Prefer:
- one test per owned behavior
- direct inputs to the target function
- small local builders for domain objects
- parametrization for the same behavior matrix
- exact text checks only where the formatter or composer owns wording
Delete or rewrite:
- tests that only prove a dependency or library default
- tests that repeat full workflow coverage in a unit file
- tests whose name says one behavior but assertions check unrelated fields
- tests that inspect private implementation unless no public behavior exposes the contract
Core Concepts
Test Discovery: Files matching test_*.py or *_test.py, functions starting with test_
Fixtures: Reusable test resources with setup and teardown
- Scopes:
function (default), class, module, session
- Composition: Build complex fixtures from simple ones
- Share via
conftest.py for project-wide availability
- Keep expected-value-driving data local to the test; use shared fixtures for mechanics, builders, and cleanup, not hidden business examples.
Assertions: Use assert statements, pytest.raises() for exceptions
Organization: Separate unit/, integration/, e2e/ directories
Quick Reference
Load detailed references for specific topics:
| Task |
Reference File |
| Pytest basics, test structure, AAA pattern |
~/.agents/skills/python-testing-patterns/references/pytest-fundamentals.md |
| Fixtures, scopes, setup/teardown, conftest.py |
~/.agents/skills/python-testing-patterns/references/fixtures.md |
| Parametrization, multiple test cases |
~/.agents/skills/python-testing-patterns/references/parametrized-tests.md |
| Mocking, patching, unittest.mock, pytest-mock |
~/.agents/skills/python-testing-patterns/references/mocking.md |
| Async tests, pytest-asyncio, event loops |
~/.agents/skills/python-testing-patterns/references/async-testing.md |
| Property-based testing, Hypothesis, strategies |
~/.agents/skills/python-testing-patterns/references/property-based-testing.md |
| Monkeypatch, environment variables, attributes |
~/.agents/skills/python-testing-patterns/references/monkeypatch.md |
| Test structure, markers, conftest.py patterns |
~/.agents/skills/python-testing-patterns/references/test-organization.md |
| Coverage measurement, reports, thresholds |
~/.agents/skills/python-testing-patterns/references/coverage.md |
| Database, API, Redis, message queue testing |
~/.agents/skills/python-testing-patterns/references/integration-testing.md |
| Best practices, test quality, fixture design |
~/.agents/skills/python-testing-patterns/references/best-practices.md |
Resources
1---2name: python-testing-patterns3description: Python testing patterns and test-quality review using pytest. Use when writing, modifying, refactoring, or reviewing Python tests; before adding assertions, apply a critical-eye pass for behavior ownership, duplicate coverage, brittle string matching, overbroad fixture use, and unit-vs-integration boundaries.4---56# Python Testing Patterns78Comprehensive guide to implementing robust testing strategies in Python using pytest, fixtures, mocking, parameterization, and property-based testing.910## Test Quality Gate1112Before adding or keeping a test, ask:1314- What behavior does this test prove?15- Which module owns that behavior?16- Could this fail because an upstream or downstream layer changed?17- Is this exact string or object comparison testing public contract or incidental formatting?18- Does this duplicate another test at a higher or lower layer?19- Can this be a direct unit test instead of full workflow setup?20- Are shared fixtures hiding business examples that should be local to the test?21- Are we checking all fields because they matter, or because object comparison was easy?22- Would a helper reduce noise without hiding behavior?2324Prefer:2526- one test per owned behavior27- direct inputs to the target function28- small local builders for domain objects29- parametrization for the same behavior matrix30- exact text checks only where the formatter or composer owns wording3132Delete or rewrite:3334- tests that only prove a dependency or library default35- tests that repeat full workflow coverage in a unit file36- tests whose name says one behavior but assertions check unrelated fields37- tests that inspect private implementation unless no public behavior exposes the contract3839## Core Concepts4041**Test Discovery**: Files matching `test_*.py` or `*_test.py`, functions starting with `test_`4243**Fixtures**: Reusable test resources with setup and teardown44- Scopes: `function` (default), `class`, `module`, `session`45- Composition: Build complex fixtures from simple ones46- Share via `conftest.py` for project-wide availability47- Keep expected-value-driving data local to the test; use shared fixtures for mechanics, builders, and cleanup, not hidden business examples.4849**Assertions**: Use `assert` statements, `pytest.raises()` for exceptions5051**Organization**: Separate `unit/`, `integration/`, `e2e/` directories5253## Quick Reference5455Load detailed references for specific topics:5657| Task | Reference File |58|------|----------------|59| Pytest basics, test structure, AAA pattern | `~/.agents/skills/python-testing-patterns/references/pytest-fundamentals.md` |60| Fixtures, scopes, setup/teardown, conftest.py | `~/.agents/skills/python-testing-patterns/references/fixtures.md` |61| Parametrization, multiple test cases | `~/.agents/skills/python-testing-patterns/references/parametrized-tests.md` |62| Mocking, patching, unittest.mock, pytest-mock | `~/.agents/skills/python-testing-patterns/references/mocking.md` |63| Async tests, pytest-asyncio, event loops | `~/.agents/skills/python-testing-patterns/references/async-testing.md` |64| Property-based testing, Hypothesis, strategies | `~/.agents/skills/python-testing-patterns/references/property-based-testing.md` |65| Monkeypatch, environment variables, attributes | `~/.agents/skills/python-testing-patterns/references/monkeypatch.md` |66| Test structure, markers, conftest.py patterns | `~/.agents/skills/python-testing-patterns/references/test-organization.md` |67| Coverage measurement, reports, thresholds | `~/.agents/skills/python-testing-patterns/references/coverage.md` |68| Database, API, Redis, message queue testing | `~/.agents/skills/python-testing-patterns/references/integration-testing.md` |69| Best practices, test quality, fixture design | `~/.agents/skills/python-testing-patterns/references/best-practices.md` |7071## Resources7273- **pytest**: https://docs.pytest.org/74- **unittest.mock**: https://docs.python.org/3/library/unittest.mock.html75- **pytest-asyncio**: Testing async code76- **pytest-cov**: Coverage reporting77- **pytest-mock**: pytest wrapper for mock78- **Hypothesis**: https://hypothesis.readthedocs.io/79- **pytest-xdist**: Parallel test execution80- **testcontainers**: Docker containers for testing