Start with explicit examples
Place discoverable tests in the project's existing test layout and run them through its configured environment. For a shipping calculation, specify values below, at, and above the threshold. Each expected value should come from the business rule rather than a second copy of the implementation's own formula.
Use pytest parameterization when cases share the same behavior and assertion structure. Give cases readable identifiers when the input alone is unclear. Keep unrelated scenarios separate so a failing case points to a specific rule instead of requiring an engineer to inspect a large loop of checks manually.
Make fixture lifetime deliberate
Use fixtures for reusable setup such as a temporary directory or a small domain object. Default to a lifetime that prevents tests from sharing mutable state accidentally. Larger fixture scopes can improve setup cost, but only when the shared resource remains safe for the way tests execute and modify it.
Use cleanup that runs after failures, especially for changed environment variables or created resources. Prefer pytest's supported temporary-path and monkeypatch facilities where they fit. A test that leaves a fake configuration behind can make later checks fail for reasons unrelated to the behavior named in their test functions.
Control imports and asynchronous boundaries
Patch a dependency where the tested module looks it up. If a module imported a helper into its own namespace, patching only the helper's original definition may leave the used reference unchanged. Verify that the test actually controls the dependency instead of silently reaching a network or reading the real clock.
Keep async code in an appropriate runner setup and await its result before asserting. Do not treat an unawaited coroutine as successful behavior. Run the suite from a clean environment and introduce a small wrong return value in scratch to confirm that the important assertion detects the intended regression.
Working sheet
| Need | Useful mechanism | Check |
|---|---|---|
| Input boundaries | pytest parameterization | Independent expected values |
| Temporary files | tmp_path fixture | No repository writes |
| Environment changes | monkeypatch | State restored |
| External helper | Patch lookup namespace | No live dependency |
Example to adapt
Illustrative example. Run it against your own fixtures and supported tool versions.
import pytest
@pytest.mark.parametrize('subtotal, expected', [(49, 5), (50, 0), (51, 0)])
def test_shipping_boundary(subtotal, expected):
assert shipping_cost(subtotal) == expected
# Import shipping_cost from the application module.
# Example rule: shipping costs 5 below a subtotal of 50.Questions about write Python unit tests with pytest and controlled fixtures
Must I replace unittest with pytest?
No. Keep the existing approach if it provides clear tests and reliable execution. pytest can also run many unittest-style tests.
Why did my patch have no effect?
Check where the tested module resolves the name. Patch that lookup location rather than assuming the original definition is used directly.
How should I use the working sheet?
Download the CSV and replace the sample rows with evidence from your repository. Record a clear acceptance check and an owner for each item. Keep unresolved assumptions visible rather than treating the example as a completed assessment.
Sources and further reading
Examples are illustrative. They are not customer results, security guarantees, or a substitute for checking the documentation for your installed version.
