Glossary

Fixture

A fixture in pytest is a function decorated with @pytest.fixture that provides a value or resource to test functions. When a test function includes a fixture's name as a parameter, pytest automatically calls the fixture, passes the result to the test, and handles any teardown. This is a form of dependency injection: tests declare what they need, and the framework provides it.

Fixtures can use yield to separate setup from teardown: code before the yield runs before the test, and code after it runs after the test (regardless of whether the test passed or failed). Fixtures can be scoped to control their lifetime: "function" (default, recreated for each test), "class", "module", or "session" (created once for the entire test run). Fixtures can also request other fixtures, forming a dependency graph.

The conftest.py file is a special file recognised by pytest that makes fixtures available to all tests in its directory and subdirectories without explicit imports. This allows you to define shared fixtures (like a database connection or a test client) in one place and use them across your entire test suite. Fixtures are what make pytest tests clean, composable, and maintainable.

Related terms: pytest, Mock

Discussed in:

This site is currently in Beta. Please email Chris Paton (cpaton@gmail.com) with any suggestions, questions or comments.