Skip to content

Testing in Depth: Mocking and Fixtures

Beginning of the lesson

Testing in Depth: Mocking and Fixtures

Fixtures, parametrization, mocking and coverage: how to test code that touches the real world

Big Idea: Intermediate pytest teaches you to write plain assert statements over pure functions. Real systems are not pure: they call networks, read files, hit databases, and read the clock. To test that code you must isolate it from the world with test doubles (mocks), build reusable test state with fixtures, run one test over many inputs with parametrization, and then measure what your tests actually exercise with coverage. These four skills turn "I wrote a test" into "I can test anything".
By the end you will be able to:
  • write @pytest.fixture functions and inject them by parameter name,
  • choose the right fixture scope and clean up with yield,
  • run one test across many cases with @pytest.mark.parametrize,
  • replace real dependencies with unittest.mock and patch(), applying the "patch where it is looked up" rule,
  • configure return values, side effects and assertions on a mock,
  • measure and read coverage, and know why 100% is not "bug-free".

1. Fixtures: setup as dependency injection

A fixture is a function decorated with @pytest.fixture that produces some object a test needs: a temporary database, a configured client, a sample record. A test requests a fixture simply by naming it as a parameter. pytest sees the name, runs the fixture, and passes its return value in. This is dependency injection: the test declares what it needs, pytest supplies it.

import pytest

@pytest.fixture
def account():
    return {"balance": 100, "owner": "ada"}

def test_withdraw(account):        # pytest injects the fixture by name
    account["balance"] -= 30
    assert account["balance"] == 70

Note what you did not write: no setup call at the top of the test, no global state. Each test that names account gets its own freshly built dict, so tests never leak state into each other. Fixtures can also request other fixtures, letting you compose small pieces into a full environment.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers