Design Python tests with unittest and test doubles
8 steps · about 3 h 50 min · Intermediate
- Model behaviour as test cases (guide, pytest documentation, 35 min)
Use the standard test lifecycle and assertions as the foundation for a portable suite.
- Organise setup and cleanup (guide, Python Developer's Guide, 30 min)
Share only the fixture work each test genuinely needs and guarantee cleanup after failures.
- Express related cases clearly (guide, Real Python, 25 min)
Check several meaningful inputs while preserving a distinct failure for each case.
- Replace an external collaborator (guide, Python documentation, 35 min)
Control a network, clock or service boundary without mocking the behaviour under test.
- Patch where a name is used (reference, Python documentation, 25 min)
Avoid false tests caused by replacing an object in the wrong namespace.
- Use disposable files and directories (reference, Python documentation, 25 min)
Exercise real filesystem behaviour without depending on permanent machine state.
- Assert warnings and failure paths (guide, Python documentation, 25 min)
Treat degraded and invalid behaviour as first-class contracts rather than testing only success.
- Run discovery as a clean suite (guide, Python Developer's Guide, 30 min)
Verify isolation, ordering independence and useful failure output from the same command automation will use.