Fixtures & parametrisation
P30.pytest-foundations.02 · Audience: guest, it-ml, language-pro · Prerequisites: pytest idioms
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.
ⓘ Concept: A generator with a yield in the middle
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 the mentor about this module
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.
🎓 Practice ladder
2 graded rungs · ~28 minYour turn: build a yield-fixture runner with guaranteed teardown, then a
@parametrize that expands one function over a table of cases.
Rung 1 - a yield fixture: setup, use, teardown
Loading exercise…
Rung 2 - build @parametrize for table-driven tests
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.
Where next?
Later in pytest Foundations
This module unlocks
Go up a level