TOPIC 14/6020 MINNOT STARTED

Fixtures: shared setup without copy-paste

A Playwright fixture builds set-up before a test, hands it over by name and cleans it up after, so tests only say what they need.

L1–L4 are part of the Pro pass

L0 Explain simply

Analogy: a kitchen prepared before each cook arrives. The board, the knife, and the pans are ready. When the cook leaves, a helper cleans up. The cook asks for what the recipe needs and does not prepare the room.

Daily List is a to-do list app. Leo, the developer, is tired of copy-paste. Five tests each add a sample task and should delete it at the end. Some of them forget the delete.

What it is: Playwright is a library that drives a real browser. This slice is a : setup the test asks for by name. Playwright builds it before the test and cleans it up after. The nearest neighbour is a hook, which runs for every test in a group whether the test needs it or not. Pytest fixtures are the cousin Python teams already know.

Why it matters: Copy-pasted setup drifts. One test forgets the cleanup, and the next run fails for a reason nobody can see. A named fixture keeps the setup and the cleanup in one place, so Tom and Leo share the same start.

A kitchen is prepared for one cook at a time. Playwright can prepare many tests at once, in separate processes. The cleanup still has to be written after the hand-off, or it runs too early.

(SYNTHETIC / TRAINING ONLY: Daily List and every person named here are made up.)

PLAIN WORDS · 2 in this section
playwright
a free Microsoft tool that drives a browser for tests
pytest
A free Python tool that finds and runs checks.

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 Sharing set-up that tests ask for by name, with clean-up kept next to it.

  1. Set up
  2. Hand over with use
  3. Test runs
  4. Clean up
Prepare, hand over, clean up

HOW THE PICTURE MAPS

How each part of the picture matches the software
In the pictureIn the software
A prepared kitchenThe set-up before use
The cook asking for a toolA test naming a fixture
The helper who cleans upThe code after use
A kitchen shared by many cooksA worker-scoped fixture

WHERE THIS PICTURE STOPS

  • A cook has one kitchen at a time, but Playwright prepares many in parallel.
  • A helper cleans without being asked, but code needs a clean-up line.
  • A shared kitchen is clean only if every cook puts things back.

HOOK

Ask by name, built before, cleaned after.

REMEMBER IT THIS WAY

A rule of thumb: if you see this, think that.

If five tests each add a sample task and delete it, write one fixture so no test can forget the delete.

What each part of the trick stands for
In the trickWhat it stands for
Add a sample taskset-up before the test
Delete itclean-up after the test

Set-up that only one test uses does not need to be shared.

TELL A FRIEND

A kitchen that is ready before the cook arrives and cleaned after. The cook only asks for what the recipe needs.

OFTEN MIXED UP WITH Test structure: describe, hooks, tags, annotations and skipping. Hooks run for every test in a group. A fixture is built only for a test that names it.

Sources & standards — 4 sources
  1. Market practice

    Fixtures (Playwright documentation) ↗ — Microsoft (Playwright) · Fixtures are set up per test, built with test.extend, hand a value to the test through use, are created only when a test asks for them, and can be automatic or worker scoped (search excerpt, page not opened)

    Says fixtures are set up per test, built with test.extend, hand a value to the test with use, are created only when a test asks for them, can be automatic with auto true and can be worker scoped with scope worker. Vendor documentation for one tool. Practitioner guidance, not a standard. · Checked 2026-10-18

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.

  2. Market practice

    Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Recommends isolated tests and shared set-up through fixtures and hooks (search excerpt, page not opened)

    Recommends user-facing locators such as role-based ones over Cascading Style Sheets (CSS) or XPath, web-first assertions that retry, and avoiding fixed waits. Vendor documentation. Practitioner or vendor guidance, not a standard unless stated. · Checked 2026-10-18

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail. A second search on 2026-10-20 returned this page for its title and matched the described content; the page was still not opened (search excerpt, page not opened).

  3. Market practice

    Isolation (Playwright documentation) ↗ — Microsoft (Playwright) · Each test gets its own browser context, so the page fixture starts clean (search excerpt, page not opened)

    Says Playwright gives each test its own browser context, a clean-slate environment like an incognito profile that is fast to create and fully isolated. Vendor documentation. Practitioner or vendor guidance, not a standard unless stated. · Checked 2026-10-18

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.

  4. Market practice

    Fixtures (Playwright documentation) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: Complete extracted primary article; examples inspected, not executed. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.

    Fixture setup/hand-off/teardown, dependencies, lazy and automatic fixtures, test/worker scope and options. Test-scoped page cannot be consumed by a worker-scoped fixture. Hooks and dependent fixtures can request values too. Cleanup is attempted under the runner lifecycle, not guaranteed after a hard kill or setup that fails before hand-off. · Checked 2026-11-06

    Primary source opened and operative text inspected on 6 November 2026. Scope: Complete extracted primary article; examples inspected, not executed. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.

PRACTISE AND KEEP

Sources for this topic — 4 sources
  1. Market practice

    Fixtures (Playwright documentation) ↗ — Microsoft (Playwright) · Fixtures are set up per test, built with test.extend, hand a value to the test through use, are created only when a test asks for them, and can be automatic or worker scoped (search excerpt, page not opened)

    Says fixtures are set up per test, built with test.extend, hand a value to the test with use, are created only when a test asks for them, can be automatic with auto true and can be worker scoped with scope worker. Vendor documentation for one tool. Practitioner guidance, not a standard. · Checked 2026-10-18

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.

  2. Market practice

    Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Recommends isolated tests and shared set-up through fixtures and hooks (search excerpt, page not opened)

    Recommends user-facing locators such as role-based ones over Cascading Style Sheets (CSS) or XPath, web-first assertions that retry, and avoiding fixed waits. Vendor documentation. Practitioner or vendor guidance, not a standard unless stated. · Checked 2026-10-18

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail. A second search on 2026-10-20 returned this page for its title and matched the described content; the page was still not opened (search excerpt, page not opened).

  3. Market practice

    Isolation (Playwright documentation) ↗ — Microsoft (Playwright) · Each test gets its own browser context, so the page fixture starts clean (search excerpt, page not opened)

    Says Playwright gives each test its own browser context, a clean-slate environment like an incognito profile that is fast to create and fully isolated. Vendor documentation. Practitioner or vendor guidance, not a standard unless stated. · Checked 2026-10-18

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.

  4. Market practice

    Fixtures (Playwright documentation) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: Complete extracted primary article; examples inspected, not executed. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.

    Fixture setup/hand-off/teardown, dependencies, lazy and automatic fixtures, test/worker scope and options. Test-scoped page cannot be consumed by a worker-scoped fixture. Hooks and dependent fixtures can request values too. Cleanup is attempted under the runner lifecycle, not guaranteed after a hard kill or setup that fails before hand-off. · Checked 2026-11-06

    Primary source opened and operative text inspected on 6 November 2026. Scope: Complete extracted primary article; examples inspected, not executed. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.

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.