Sub topic 1
Why Test Data Becomes a Problem
Prerequisites
This chapter assumes you can write and run automated tests in a test runner such as pytest. You should know what a fixture or setup step is. You should also be comfortable with basic Python, including dataclasses, because the examples use them.
The problem in one sentence
Most tests need an object in a particular state before they can check anything. Creating that object by hand in every test is cheap at first. As the suite grows, it becomes the main source of friction.
Where it goes wrong
Consider a User record and a test that checks whether a user can log in.
from dataclasses import dataclass
@dataclass
class User:
id: int
email: str
role: str
active: bool
def can_log_in(user):
return user.active
def test_inactive_user_cannot_log_in():
user = User(id=1, email="[email protected]", role="member", active=False)
assert can_log_in(user) is False
def test_active_user_can_log_in():
user = User(id=1, email="[email protected]", role="member", active=True)
assert can_log_in(user) is TrueThis works. The trouble shows up when the code changes and when the suite grows.
Copy-pasted setup is brittle
Suppose User gains a required field, created_at. Every test that builds a User by hand now fails with a TypeError. If 200 tests construct users inline, you must edit 200 places. None of those edits has anything to do with what the tests check.
The important detail gets lost
In the two tests above, only one value matters: active. The id, email, and role are noise. A reader has to compare the two lines to find the single difference. With ten fields, that comparison gets harder, and the test's intent is buried.
Shared fixtures drift
The usual fix is a shared fixture, such as one user object defined in a setup function or a conftest.py file. This removes duplication but adds new problems:
- Tests depend on values they cannot see. A test asserts that
user.emailends with@example.com, and it works only because the fixture happens to use that value. - Someone changes the fixture for one test, and distant tests start failing.
- The fixture grows more fields to satisfy more tests, until it fits none of them well.
- Tests that mutate the shared object can affect each other, which makes failures depend on test order.
Hard-coded values collide
Hand-written data often repeats ids and emails. That is fine until a rule such as "emails must be unique" is enforced. Then two users in the same test, both using [email protected], cause a failure unrelated to the behavior under test.
How the problems relate
The symptoms share a root cause. Each test is coupled to the full shape of the data, not only to the values it cares about.
What a better approach looks like
A good test states only what it cares about and gets everything else from one place. The test for an inactive user would read like this:
def test_inactive_user_cannot_log_in():
user = make_user(active=False)
assert can_log_in(user) is FalseThe only visible detail is active=False, which is the reason the test exists. The other fields come from make_user. When User gains created_at, you change make_user once and no test changes.
make_user is a test data factory. It is a function that builds a valid object with sensible defaults and lets each test replace the fields it needs. The next chapters compare factories with static fixtures, then build one step by step.
Check your understanding
Exercise
A suite has 150 tests that each build a User(...) inline with every field. The team adds a required field timezone. Name two problems this causes, and say how a factory function would change the cost of this change.
Show answer
First, all 150 tests fail and each must be edited, even though none of them is about timezones. Second, each test's setup now has one more irrelevant value, so the field that matters is harder to spot. With a factory, you add timezone to the defaults in one function. The tests that do not care about it stay unchanged.
Exercise
A shared user fixture uses the email [email protected]. A new rule requires unique emails, and a test now creates two users from that fixture's values. What goes wrong, and which general problem does it illustrate?
Show answer
The second user violates the uniqueness rule, so the test fails for a reason unrelated to its purpose. It illustrates that hard-coded values collide when a constraint is added, and that tests silently depend on fixture values.