← All notes

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

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

Sources

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

Permanent ID: iil:note:btbqdbe5

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.},
}

Markdown

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.