04 / 05

How do pytest fixtures improve test setup/teardown compared to unittest's setUp/tearDown?

Difficulty: 6/10
Pytest, Fixtures, Setup Teardown

Fixtures are composable, scope-aware dependency injection instead of fixed per-class hooks

unittest setUp and tearDown are fixed hooks tied to a TestCase class. They run for every test method, they cannot be parameterized per test, and sharing them across classes requires inheritance. pytest fixtures are functions that a test requests by name, so setup is composed by dependency injection rather than by inheritance. A fixture can be function, class, module, package, or session scoped, which means an expensive resource like a database container can be created once per session and injected everywhere. Fixtures can depend on other fixtures, so a db_session can depend on a connection which depends on a container. Teardown is expressed as code after the yield inside the fixture, and pytest guarantees it runs even if the test fails.

  1. 1

    Request fixtures by naming them in the test signature; pytest resolves the dependency graph.

  2. 2

    Scope controls lifetime: function (default), class, module, package, or session.

  3. 3

    Fixtures can be parameterized, which generates one test per parameter set without duplicating code.

  4. 4

    conftest.py exposes fixtures to an entire directory tree without imports.

  5. 5

    autouse fixtures apply implicitly when you need global setup such as environment variables.

  6. 6

    Trade-off: session-scoped fixtures are fast but shared state can leak between tests. Function-scoped fixtures are safer but slower.

  7. 7

    Common mistake: putting mutable state in a session fixture and having one test corrupt another.

  8. 8

    Common mistake: calling teardown code after yield that can itself raise, masking the original test failure.

  9. 9

    Version note: fixtures have been core to pytest since early versions. pytest 7+ and 8+ refined scoping and warning behavior but the API is stable.

Scenario Questions

0-2 years experience

  1. 1How does a test tell pytest which fixture to use?
  2. 2Where does teardown code live in a pytest fixture?

2-5 years experience

  1. 1You have a slow database container and want to start it once for the whole suite. How do you do it with fixtures?
  2. 2You need the same test to run against three different configurations. How do you parameterize a fixture?

5-8 years experience

  1. 1A session-scoped fixture leaks state between tests and one test corrupts another. How do you diagnose and fix it?
  2. 2You need a fixture that yields a resource and guarantees cleanup even when the test fails inside a nested context. How do you structure it?

8+ years experience

  1. 1Design a fixture hierarchy for a large test suite with layered resources (process, connection, transaction, data) that keeps isolation while staying fast.
  2. 2Explain how fixture finalization, scope mismatches, and pytest-asyncio interact, and how to avoid resource leaks in async tests.

Follow-up Questions

  • When is a session-scoped fixture the wrong choice?
  • How do you override a fixture for a single test or module?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.