Design Python tests with unittest and test doubles

8 steps · about 3 h 50 min · Intermediate

  1. Model behaviour as test cases (guide, pytest documentation, 35 min)

    Use the standard test lifecycle and assertions as the foundation for a portable suite.

  2. 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.

  3. Express related cases clearly (guide, Real Python, 25 min)

    Check several meaningful inputs while preserving a distinct failure for each case.

  4. Replace an external collaborator (guide, Python documentation, 35 min)

    Control a network, clock or service boundary without mocking the behaviour under test.

  5. Patch where a name is used (reference, Python documentation, 25 min)

    Avoid false tests caused by replacing an object in the wrong namespace.

  6. Use disposable files and directories (reference, Python documentation, 25 min)

    Exercise real filesystem behaviour without depending on permanent machine state.

  7. Assert warnings and failure paths (guide, Python documentation, 25 min)

    Treat degraded and invalid behaviour as first-class contracts rather than testing only success.

  8. 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.