1. Concept library
  2. Python Foundations
  3. Module 11: Testing

Concept

Test fixtures

A fixture builds something tests need, such as a temporary file or a prepared object, and hands it to each test that asks for it by name. Built-in ones like tmp_path cover the common cases.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain what a fixture provides and how a test asks for one
  2. Write a fixture that builds a prepared object for several tests
  3. Use tmp_path to test code that reads and writes files

In practice

A few lines that show Test fixtures doing its job.

Python
import pytest
from store import Store

@pytest.fixture
def store(tmp_path):
    return Store(tmp_path / "data.json")

def test_saves_and_loads(store):
    store.put("a", 1)
    assert store.get("a") == 1

def test_starts_empty(store):
    assert store.get("a") is None

The common mistake

Watch for

Sharing one mutable object across tests through a module-level variable. Tests then depend on the order they run in, and a failure in one shows up in another.

What to know first

Your roadmap puts these before Test fixtures, unless you already know them.

How it stays learned

Once a session shows you understand Test fixtures, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled
Previous in the module← Writing tests with pytest