Pytest Yield Fixtures: Reliable Teardown That Runs Even When Tests Fail
Pytest yield fixtures guarantee teardown runs after every test — pass, fail, or error. Learn the pattern, scope behavior, verification, and autouse pitfalls.
ReadMeFeed / Community knowledge
Real questions. Useful conversations. Find the people who know your stack.
Pytest yield fixtures guarantee teardown runs after every test — pass, fail, or error. Learn the pattern, scope behavior, verification, and autouse pitfalls.
Learn how to replace duplicated test functions with a single parametrized test in pytest, combine it with fixtures, use marks and custom IDs, and understand the trade‑offs.
Learn how to use pytest's parametrize feature to generate test cases, manage data boundaries, and keep your test suite maintainable.
Pytest fixture scope is a speed-versus-isolation trade-off. Here's a decision rule for choosing function, module, or session scope, plus how to verify the behavior you actually get.
Combine pytest fixtures with indirect parametrization to eliminate test setup duplication while keeping test functions readable. A worked example shows how to pass scenario names into a fixture that returns tailored test data.
Learn how pytest fixture scopes—function, class, module, session—shape test speed and isolation. A concrete example shows a temporary SQLite fixture that can swap in Postgres, plus tips on composition and autouse.
Choose the right pytest fixture scope to balance isolation and speed. Compare function, class, module, package, and session scopes, see a nested fixture example, and learn how to verify lifecycle behavior.
Learn how to define a Pytest fixture with module scope to share expensive setup across tests, see a concrete example, and understand the limits and common pitfalls.
The goal is to determine how finalizers registered in an indirect parametrize fixture are ordered relative to other fixture finalizers and the test body. In indirect parametrize, a fixture supplies parameter values, and it may call request.addfinalizer to clean up. However, the exact sequence of finalizer execution is not explicitly documented, and it appear