Playwright Skip and Only
Playwright Skip and Only
test.skip() and test.only() decide which tests run.
They are useful while you work on a test, or when a test should not run in a certain browser.
Skip, Only, Fixme and Fail
Four functions change how Playwright treats a test:
| Function | Effect |
|---|---|
| test.skip() | The test does not run, and is reported as skipped |
| test.fixme() | Like skip, but marks the test as broken and waiting for a fix |
| test.fail() | The test runs, and is expected to fail. If it passes, Playwright reports an error |
| test.only() | Only this test (and other tests marked with only) runs |
Example
tests/skip.spec.js
// @ts-check
import { test, expect } from '@playwright/test';
test('skipped test', async ({ page }) => {
test.skip();
});
test.skip('also skipped', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/todo.html');
});
test('not in WebKit', async ({ page, browserName }) => {
test.skip(browserName === 'webkit', 'Not tested in WebKit yet');
await page.goto('https://www.w3schools.com/playwright/practice/todo.html');
await expect(page).toHaveTitle(/Todo List/);
});
test.fixme('drag todos to reorder them', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/todo.html');
});
test('shows an error for an empty todo', async ({ page }) => {
test.fail(true, 'The app ignores empty todos and shows no error yet');
await page.goto('https://www.w3schools.com/playwright/practice/todo.html');
await page.getByRole('button', { name: 'Add' }).click();
await expect(page.getByRole('alert')).toBeVisible({ timeout: 1000 });
});
Example
npx playwright test tests/skip.spec.js --reporter=list
Output:
Running 5 tests using 5 workers
- 1 [chromium] › tests/skip.spec.js:8:6 › also skipped
- 2 [chromium] › tests/skip.spec.js:18:6 › drag todos to reorder them
- 4 [chromium] › tests/skip.spec.js:4:1 › skipped test
ok 3 [chromium] › tests/skip.spec.js:12:1 › not in WebKit (245ms)
x 5 [chromium] › tests/skip.spec.js:22:1 › shows an error for an empty todo (1.9s)
3 skipped
2 passed (3.9s)
Example Explained
test.skip()inside a test skips it while it runs, andtest.skip('name', ...)skips it before it startstest.skip(condition, reason)is a conditional skip: the test only skips when the condition is true, so here it runs in Chromium and would skip in WebKittest.fixme()shows up as skipped, like a todo note for your team- The last test checks for a feature the app does not have yet
- It failed, as expected, so it counts as passed, and the
xshows that it failed
test.only() is useful while you work on one test:
Example
test.only('starts empty', async ({ page }) => {
// only this test runs
});
Note: Remove test.only() before you save your work to Git or share it.
If you forget it, the other tests stop running, and no error tells you.
Set forbidOnly: !!process.env.CI in the config, so a forgotten only fails the run on your CI server (a computer that runs your tests automatically).
process.env.CI is set on most CI servers, and !! turns it into true or false.
You can also use test.describe.skip(), test.describe.only() and test.describe.fixme() on a whole group.