Fixtures & parametrisation
P30.pytest-foundations.02 · Audience: Guest, IT/ML learner, Language professional · Prerequisites: pytest idioms
Where next?
Later in pytest Foundations
This module unlocks
Go up a level
P30.pytest-foundations.02 · Audience: Guest, IT/ML learner, Language professional · Prerequisites: pytest idioms
Later in pytest Foundations
This module unlocks
Go up a level
Two tests need a database connection; five tests are the same assertion with different numbers. pytest answers the first with fixtures (shared setup and guaranteed teardown) and the second with parametrisation (one test, a table of cases). Both are small mechanisms built on things you already have - generators from P29 and decorator factories from P29 - so you will build each one and see there is no magic underneath.
yield is setup, the yielded value is the resource, and code after yield is teardown. The runner advances to the yield, hands the value to the test, then resumes the generator to tear down - which is why P29's yield-as-a-doorway is the exact tool.Why it matters — Untorn-down resources - open files, temp dirs, connections - are the classic source of tests that pass alone but poison each other when run together.
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.
Your turn: build a yield-fixture runner with guaranteed teardown, then a
@parametrize that expands one function over a table of cases.
Loading exercise…
Loading exercise…
Fixtures and parametrisation are where a test suite stops being a pile of functions and becomes maintainable - shared setup in one place, and cases in a table you can extend in one line.
Loading your notes...