Python Testing Skill
Design and write effective Python tests with pytest.
Load resources/implementation-playbook.md only for a concrete pytest recipe (fixtures, parametrize, mock, async). Do not load it for process or policy. Do not invent coverage percentage gates.
When to Use
- Unit, integration, or API tests in Python
- pytest layout, fixtures, TDD
- Mocking external boundaries
- Async tests, flaky/slow tests
When Not to Use
- Non-Python testing (use crate tests /
dash_duo as appropriate)
- Feature implementation with no test intent (
python-pro)
- Load/performance benches (
python-performance)
Related Skills
- Required for Python behavior changes before code-review.
- Do not co-load other skills unless those tasks are also in scope.
Core Principles
- Test behavior and contracts, not implementation details.
- Fast deterministic unit tests for core logic.
- Integration tests for boundaries (DB, API, filesystem).
- Mock only what you do not own or cannot control.
- Arrange → act → assert. One main behavior per test (not a hard one-assert rule).
- Maintainability over coverage theater.
Test Strategy
| Layer |
Purpose |
Typical tools |
| Unit |
Business logic, pure functions |
pytest, parametrize |
| Integration |
API, DB, queue, filesystem |
pytest + TestClient/httpx |
| End-to-end |
Critical flows |
few, high-value |
Prioritize: critical paths → edge/failure modes → regressions for fixed bugs.
Pytest Patterns
- Fixtures for reusable setup, not hidden magic.
- Parametrize input matrices.
- Factory helpers over giant fixture graphs.
- Name by behavior:
test_rejects_invalid_token.
Mocking
Mock: external HTTP, third-party services, time/randomness.
Do not mock: the logic under test, in-process pure functions, local code you can run.
Prefer explicit fakes over spy-heavy tests.
Async Testing
pytest-asyncio for async def tests.
- Cover success, timeout, cancellation, exceptions.
- No
sleep-based assertions when an asyncio.Event (or equivalent) works.
TDD
Use when it clarifies design: failing test → minimal pass → refactor.
Skip ritual TDD on spikes; add tests before the code hardens.
Bug-fixing Loop
- Reproduce with a minimal failing test.
- Isolate the divergence.
- Fix the root cause.
- Guard keep the test as a regression.
Test Design Checklist
1---2name: python-testing3description: Write or fix pytest suites: fixtures, boundary mocks, async tests, flaky tests, regression guards. Not for implementing features or load/performance benchmarks.4---56# Python Testing Skill78Design and write effective Python tests with pytest.910Load `resources/implementation-playbook.md` **only** for a concrete pytest recipe (fixtures, parametrize, mock, async). Do not load it for process or policy. Do not invent coverage percentage gates.1112## When to Use13- Unit, integration, or API tests in Python14- pytest layout, fixtures, TDD15- Mocking external boundaries16- Async tests, flaky/slow tests1718## When Not to Use19- Non-Python testing (use crate tests / `dash_duo` as appropriate)20- Feature implementation with no test intent (`python-pro`)21- Load/performance benches (`python-performance`)2223## Related Skills24- Required for **Python** behavior changes before **code-review**.25- Do not co-load other skills unless those tasks are also in scope.2627## Core Principles281. Test behavior and contracts, not implementation details.292. Fast deterministic unit tests for core logic.303. Integration tests for boundaries (DB, API, filesystem).314. Mock only what you do not own or cannot control.325. Arrange → act → assert. One main behavior per test (not a hard one-assert rule).336. Maintainability over coverage theater.3435## Test Strategy36| Layer | Purpose | Typical tools |37| :--- | :--- | :--- |38| **Unit** | Business logic, pure functions | pytest, parametrize |39| **Integration** | API, DB, queue, filesystem | pytest + TestClient/httpx |40| **End-to-end** | Critical flows | few, high-value |4142Prioritize: critical paths → edge/failure modes → regressions for fixed bugs.4344## Pytest Patterns45- Fixtures for reusable setup, not hidden magic.46- Parametrize input matrices.47- Factory helpers over giant fixture graphs.48- Name by behavior: `test_rejects_invalid_token`.4950## Mocking51Mock: external HTTP, third-party services, time/randomness.52Do not mock: the logic under test, in-process pure functions, local code you can run.53Prefer explicit fakes over spy-heavy tests.5455## Async Testing56- `pytest-asyncio` for `async def` tests.57- Cover success, timeout, cancellation, exceptions.58- No `sleep`-based assertions when an `asyncio.Event` (or equivalent) works.5960## TDD61Use when it clarifies design: failing test → minimal pass → refactor.62Skip ritual TDD on spikes; add tests before the code hardens.6364## Bug-fixing Loop651. **Reproduce** with a minimal failing test.662. **Isolate** the divergence.673. **Fix** the root cause.684. **Guard** keep the test as a regression.6970## Test Design Checklist71- [ ] Behavior under test is clear72- [ ] Deterministic and isolated73- [ ] Assertions specific74- [ ] External deps handled appropriately75- [ ] Failure messages diagnosable76- [ ] Edge/error paths considered77- [ ] Runtime reasonable