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.
- Set up
- Hand over with use
- Test runs
- Clean up
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| A prepared kitchen | The set-up before use |
| The cook asking for a tool | A test naming a fixture |
| The helper who cleans up | The code after use |
| A kitchen shared by many cooks | A 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.
| In the trick | What it stands for |
|---|---|
| Add a sample task | set-up before the test |
| Delete it | clean-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
- 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)
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.
- Market practice
Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Recommends isolated tests and shared set-up through fixtures and hooks (search excerpt, page not opened)
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).
- 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)
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.
- 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.
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
- 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)
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.
- Market practice
Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Recommends isolated tests and shared set-up through fixtures and hooks (search excerpt, page not opened)
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).
- 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)
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.
- 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.
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.