TOPIC 16/2216 MINNOT STARTED

pytest conftest.py: share fixtures across files, and choose a fixture scope

Move set-up that many test files need into one conftest.py file, pick how long each fixture lives, and keep that file small so it stays easy to read.

L1–L4 are part of the Pro pass

L0 Explain simply

Analogy: a shared toolbox, and a rule for how long you may borrow a tool. The toolbox sits in the hall. Any room on this floor can use it. A hammer you borrow for one job goes back at the end of that job. A generator you start for the whole day stays on until the day ends.

Tom shares synthetic test resources through a , chooses a when its lifetime is appropriate, and checks fixture lookup order from the requesting test's location.

A shared toolbox or stationery cupboard can serve nearby rooms without each room importing its own copy. Sharing is a visibility and lifetime decision, not permission for tests to depend on another test's leftovers.

The picture has a limit. Session scope is not a guarantee of exactly one setup across every parameter combination or parallel worker. Mutable shared state and failed setup require explicit handling.

(SYNTHETIC / TRAINING ONLY: Daily List and every named person are invented.)

PLAIN WORDS · 1 in this section
mutable
able to be changed

PICTURE IT

The picture is the one the lesson above opens with. The table below shows how each part of it matches the software.

THE REAL JOB Provide appropriately visible pytest fixtures with deliberate lifetime, isolation and cleanup.

  1. Cupboard
  2. Term
  3. Lesson
School corridor

HOW THE PICTURE MAPS

How each part of the picture matches the software
In the pictureIn the software
The stationery cupboardThe conftest file
A classroom on the corridorA test in that folder or below
Pens bought for the whole termA session-scoped fixture
A pen for one lessonA function-scoped fixture

WHERE THIS PICTURE STOPS

  • A shared fixture can retain mutable or external state; choosing a long scope does not prove isolation.
  • Session caching, fixture parameters and separate workers mean session scope is not an exactly-once global guarantee.

HOOK

Cupboard, term, lesson.

TELL A FRIEND

A cupboard that classrooms share.

OFTEN MIXED UP WITH pytest from zero: your first tests, fixtures and markers. That topic writes first tests and fixtures in one file. This one shares fixtures across files and chooses how long they live.

Sources & standards — 3 sources
  1. Implementation-specific

    pytest fixture guide ↗ — pytest-dev contributors · Scopes and parameter caching; yield-setup failure; broader/narrower dependency lifetimes (selected actual primary passages read 7 November 2026; live publisher documentation, not every linked section or historical release).

    Scopes and parameter caching; yield-setup failure; broader/narrower dependency lifetimes. Product/configuration-specific guidance; not a universal team practice. · Checked 2026-11-07

    Selected actual primary passages read, including the specific fixture/marker/collection boundaries described. Daily List rules, people and paths are synthetic; no pytest/xdist execution or complete production fixture design is claimed. Live documentation has no fixed edition claimed.

  2. Implementation-specific

    pytest fixture reference ↗ — pytest-dev contributors · Availability from requesting test perspective; conftest upward search and nearest applicable override (selected actual primary passages read 7 November 2026; live publisher documentation, not every linked section or historical release).

    Availability from requesting test perspective; conftest upward search and nearest applicable override. Product/configuration-specific guidance; not a universal team practice. · Checked 2026-11-07

    Selected actual primary passages read, including the specific fixture/marker/collection boundaries described. Daily List rules, people and paths are synthetic; no pytest/xdist execution or complete production fixture design is claimed. Live documentation has no fixed edition claimed.

  3. Implementation-specific

    pytest-xdist how-to ↗ — pytest-dev contributors · Session-scoped fixtures across separate workers; optional cross-process coordination example (selected actual primary passages read 7 November 2026; live publisher documentation, not every linked section or historical release).

    Session-scoped fixtures across separate workers; optional cross-process coordination example. Product/configuration-specific guidance; not a universal team practice. · Checked 2026-11-07

    Selected actual primary passages read, including the specific fixture/marker/collection boundaries described. Daily List rules, people and paths are synthetic; no pytest/xdist execution or complete production fixture design is claimed. Live documentation has no fixed edition claimed.

PRACTISE AND KEEP

Sources for this topic — 3 sources
  1. Implementation-specific

    pytest fixture guide ↗ — pytest-dev contributors · Scopes and parameter caching; yield-setup failure; broader/narrower dependency lifetimes (selected actual primary passages read 7 November 2026; live publisher documentation, not every linked section or historical release).

    Scopes and parameter caching; yield-setup failure; broader/narrower dependency lifetimes. Product/configuration-specific guidance; not a universal team practice. · Checked 2026-11-07

    Selected actual primary passages read, including the specific fixture/marker/collection boundaries described. Daily List rules, people and paths are synthetic; no pytest/xdist execution or complete production fixture design is claimed. Live documentation has no fixed edition claimed.

  2. Implementation-specific

    pytest fixture reference ↗ — pytest-dev contributors · Availability from requesting test perspective; conftest upward search and nearest applicable override (selected actual primary passages read 7 November 2026; live publisher documentation, not every linked section or historical release).

    Availability from requesting test perspective; conftest upward search and nearest applicable override. Product/configuration-specific guidance; not a universal team practice. · Checked 2026-11-07

    Selected actual primary passages read, including the specific fixture/marker/collection boundaries described. Daily List rules, people and paths are synthetic; no pytest/xdist execution or complete production fixture design is claimed. Live documentation has no fixed edition claimed.

  3. Implementation-specific

    pytest-xdist how-to ↗ — pytest-dev contributors · Session-scoped fixtures across separate workers; optional cross-process coordination example (selected actual primary passages read 7 November 2026; live publisher documentation, not every linked section or historical release).

    Session-scoped fixtures across separate workers; optional cross-process coordination example. Product/configuration-specific guidance; not a universal team practice. · Checked 2026-11-07

    Selected actual primary passages read, including the specific fixture/marker/collection boundaries described. Daily List rules, people and paths are synthetic; no pytest/xdist execution or complete production fixture design is claimed. Live documentation has no fixed edition claimed.

Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.