pytest fixtures: requesting, scope, yield teardown and conftest.py
Answer: Test functions request fixtures they require by declaring them as arguments; pytest looks at the parameters in the test function's signature and searches for fixtures that have the same names.
Key features
@pytest.fixture(scope="module")makes a fixture run only once per test module; scope can also be set dynamically.- A fixture that uses
yieldruns its teardown code after the yield when the test finishes. - Putting a fixture in
conftest.pylets tests from multiple test modules in the directory use it. - Autouse fixtures apply to tests without being requested.
unittest equivalent
The setUp() and tearDown() methods define instructions executed before and after each test method. addCleanup() adds a function called after tearDown(), with cleanups run in reverse order (LIFO).
See also
Claims
- id: C1 claim: Tests request fixtures as arguments. source: S1 evidence: "test functions request fixtures they require by declaring them as arguments"
- id: C2 claim: Fixtures in conftest.py are shared. source: S1 evidence: "tests from multiple test modules in the directory can access the fixture function"
- id: C3 claim: setUp and tearDown wrap each test. source: S2 evidence: "executed before and after each test method"
Sources
- [S1] How to use fixtures (pytest docs). pytest-dev/pytest (GitHub). https://github.com/pytest-dev/pytest/blob/main/doc/en/how-to/fixtures.rst . License: MIT. Accessed 2026-11-16.
- [S2] unittest — Unit testing framework. Python Software Foundation (docs.python.org). https://docs.python.org/3/library/unittest.html . License: PSF-2.0. Accessed 2026-11-16.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: pytest-dev/pytest (GitHub), How to use fixtures (pytest docs), MIT (https://raw.githubusercontent.com/pytest-dev/pytest/main/LICENSE). Copyright (c) 2004 Holger Krekel and others; Python Software Foundation (docs.python.org), unittest — Unit testing framework, PSF-2.0 (https://docs.python.org/3/library/unittest.html (page footer: licensed under the Python Software Foundation License Version 2; code examples Zero Clause BSD)). Copyright 2001 Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: pytest-dev/pytest (GitHub), How to use fixtures (pytest docs), MIT (https://raw.githubusercontent.com/pytest-dev/pytest/main/LICENSE). Copyright (c) 2004 Holger Krekel and others; Python Software Foundation (docs.python.org), unittest — Unit testing framework, PSF-2.0 (https://docs.python.org/3/library/unittest.html (page footer: licensed under the Python Software Foundation License Version 2; code examples Zero Clause BSD)). Copyright 2001 Python Software Foundation.
Cite this
iinventedlight. 2026. "pytest fixtures: requesting, scope, yield teardown and conftest.py." iinventedlight Library, iil:note:btbqdbe5, version of 2026-11-16. https://iinventedlight.com/library/notes/pytest-fixtures/. License: CC-BY-4.0.
@misc{iil_note_btbqdbe5,
author = {{iinventedlight}},
title = {{pytest fixtures: requesting, scope, yield teardown and conftest.py}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-16},
url = {https://iinventedlight.com/library/notes/pytest-fixtures/},
note = {Permanent ID iil:note:btbqdbe5. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/pytest-fixtures.md.