How do I run a single Playwright test?
Written by Jatin Thacker, Automation Architect · Updated
npx playwright test single-test.spec.js:8. Or match its title with -g: npx playwright test -g "second test".This answer is one piece of Chapter 16: The Test Runner.
- Test Structure & Lifecycle
- playwright.config.ts Complete Reference
- Test Organization at Scale
- Tags, Grep, and the Suite You Actually Run
- What Runs Where, and What Serial Costs
- Practice and interview questions: Assertions & Test Runner
Every way of running part of a suite is a filter on npx playwright test. Pick the one that matches what you know about the test.
| You know | Command | What runs |
|---|---|---|
| the file | npx playwright test single-test.spec.js | every test in that file |
| part of the file name | npx playwright test single | every file whose name matches |
| the line | npx playwright test single-test.spec.js:8 | the test declared on line 8 |
| the title | npx playwright test -g "second test" | tests whose title matches |
| the browser | npx playwright test --project=chromium | only that project |
Filters combine. In the run below, single-test.spec.js --project=chromium -g first ran exactly one test.
-g takes a regular expression and looks at the tests in every file the run picks up, so a short pattern can catch more than you meant. Add the file name to narrow it.
Before a long run, add --list. It collects the tests and prints them without running any, so you can check that a filter picks what you expect.
You can also write test.only('title', ...) in the file. It works, but it is easy to commit by mistake, and then CI runs one test and reports green. Prefer the command line; --forbid-only makes CI fail if a .only slips through.
import { test, expect } from '@playwright/test';
test('first test: header renders', async ({ page }) => {
await page.goto('/catalog.html?mock=static&seed=42&autologin=1');
await expect(page.getByTestId('site-header')).toBeVisible();
});
test('second test: search form exists', async ({ page }) => {
await page.goto('/catalog.html?mock=static&seed=42&autologin=1');
await expect(page.getByTestId('catalog-query')).toBeVisible();
});$ npx playwright test single-test.spec.js:8 ok 1 [chromium] › single-test.spec.js:8:1 › second test: search form exists (423ms) 1 passed (1.5s) $ npx playwright test -g "second test" ok 1 [chromium] › single-test.spec.js:8:1 › second test: search form exists (450ms) 1 passed (1.6s) $ npx playwright test single-test.spec.js --project=chromium -g first ok 1 [chromium] › single-test.spec.js:3:1 › first test: header renders (366ms) 1 passed (1.4s) $ npx playwright test single-test.spec.js --list Listing tests: [chromium] › single-test.spec.js:3:1 › first test: header renders [chromium] › single-test.spec.js:8:1 › second test: search form exists Total: 2 tests in 1 file
'submits contact form' in tests/contact.spec.ts. How can you execute only this test from the terminal?Related questions
AI can write the test now. The job is knowing whether it checks anything. That's what the course teaches, one chapter at a time.
163 lessons · 226 drills · start free