Python Testing
Test observable behavior with independent expectations and isolated fixtures.
When to use
Use for Python test design, failures, coverage gaps or test infrastructure.
Use python-sdk for inference.sh integration and systematic-debugging to
investigate a failure before guessing at a fix. This skill does not require
converting an existing unittest suite to pytest.
Workflow
- Read the assignment/specification and permitted-edit boundaries first. Inspect the existing test runner, config, supported Python versions and CI. Preserve public APIs and deliverable boundaries, especially for coursework.
- Map each changed requirement to inputs and independently expected results. Include boundary values, invalid inputs and interactions that can change the result. Do not derive the oracle by copying the implementation under test.
- For a bug, reproduce it with a failing assertion when practical; verify the test fails for the intended behavior, not an import/configuration error. For new behavior use a short red/green/refactor cycle. Match effort to risk; do not add tests for a harmless prose edit or chase an arbitrary percentage.
- Reuse the repo's unittest or pytest facilities. Use temporary directories, injected clocks and deterministic inputs. Close files before reopening on Windows; use pathlib and paths containing spaces in file-tool tests.
- Mock only external boundaries. Patch where the symbol is looked up. Check observable results as well as relevant calls; avoid mocking the code whose behavior is the subject of the test. Separate real integration tests clearly.
- Run focused tests, inspect collected/skipped counts and exit status, then run the broader suite required by the project. Report platform-specific skips. Passing mocks do not establish live API or database correctness.
Runner selection
| Existing project | Starting command, adjusted to its configuration |
|---|---|
| unittest | python -m unittest discover -s tests -v |
| pytest | python -m pytest |
| uv-managed project | Use its documented uv run test command |
| Restricted assignment | Use only the assignment's allowed runner and files |
These commands run in the target project, not this skill folder. Check required plugins before adding plugin-only flags. Do not install or upgrade packages just because an example mentions them. Coverage targets come from project policy; coverage measures exercised code, not correctness or hidden-test completeness.
Patterns and verification
Read testing patterns for fixtures, mocks and async
boundaries. The small stdlib example is
self-contained and can be run using its absolute path with python; it touches
only temporary files. It illustrates testing style, not a production port parser.
Use verification-loop to report final evidence. Load optional companions
through skill-router if they are absent from the daily installation.
Provenance
Adapted from ECC python-testing. See MIT notice. Local adaptation adds unittest/coursework support, Windows file behavior and independent oracles; removes blanket coverage mandates.