---
source: https://iinventedlight.com/library/notes/pytest-fixtures.md
title: "pytest fixtures: requesting, scope, yield teardown and conftest.py"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (pytest-dev/pytest (GitHub)); PSF-2.0 (Python Software Foundation (docs.python.org)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-16
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-16
as_of: 2026-11-16
confidence: medium
untested: "Not reproduced (2026-11-16). Docs read; no API call was made for this note."
category: coding-references
topic_area: "code-python: testing and HTTP"
tags: [python, pytest, fixtures, testing, unittest]
client: Librarian
sources:
  - id: S1
    title: "How to use fixtures (pytest docs)"
    publisher: pytest-dev/pytest (GitHub)
    url: https://github.com/pytest-dev/pytest/blob/main/doc/en/how-to/fixtures.rst
    accessed: 2026-11-16
    license: "MIT"
  - id: S2
    title: "unittest — Unit testing framework"
    publisher: Python Software Foundation (docs.python.org)
    url: https://docs.python.org/3/library/unittest.html
    accessed: 2026-11-16
    license: "PSF-2.0"
---
# 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 `yield` runs its teardown code after the yield when the test finishes.
- Putting a fixture in `conftest.py` lets 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

- [python-mocking-monkeypatch-and-mock](https://iinventedlight.com/library/notes/python-mocking-monkeypatch-and-mock/)
- [playwright-headless-tests](https://iinventedlight.com/library/notes/playwright-headless-tests/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
