日本語

Stop Playwright Tests After a Set Number of Failures with –max-failures and maxFailures

​​Playwright​CI,E2E Testing,TypeScript

When you run npx playwright test --max-failures=3, Playwright stops after three failures and does not run the remaining tests. When E2E tests all fail together, for example because the server is down or the whole page is broken, this means CI does not have to wait for every test to fail.

You can get the same result with maxFailures in the config file. I checked in a test environment what the output looks like when the run stops, and which results count as failures with retries and parallel runs.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.5, and @playwright/test 1.63.0. I wrote the tests in TypeScript and used Chromium (headless). The number of workers was 2 only in the parallel section and 1 everywhere else. I did not test runs on CI services such as GitHub Actions, splitting with --shard, or Firefox and WebKit.

Prepare 8 tests where 4 fail

For this check, I wrote 8 tests for a product list page. The page is HTML passed to page.setContent. The price is left unset, there are no buttons, and there is no login link. As a result, tests 2, 4, 5, and 7 fail. The failing tests wait only 500ms, so they finish quickly.

ts
// tests/shop.spec.ts (partial)
import { test, expect } from '@playwright/test';

// Deliberately broken page: the heading exists, but some of the price, buttons, and links are missing
const html = `
  <h1>商品一覧</h1>
  <ul><li>りんご</li><li>みかん</li></ul>
  <p id="price">価格 未設定</p>
  <a href="#help">ヘルプ</a>
`;

test.beforeEach(async ({ page }) => {
  await page.setContent(html);
});

test('1 見出しが出る', async ({ page }) => {
  await expect(page.getByRole('heading', { name: '商品一覧' })).toBeVisible();
});

test('2 価格が出る', async ({ page }) => {
  await expect(page.locator('#price')).toHaveText('価格 120円', { timeout: 500 });
});

// Write 3 to 8 the same way. 4, 5, and 7 fail after waiting 500ms for buttons or links that do not exist

In this article's output, I kept only the lines for each test, the summary, the message shown when the run stops, and the headings of the failed tests. I omitted the detailed error content. First, I ran the tests with no options.

bash
npx playwright test --reporter=list
text
  ✓  1 [chromium] › tests/shop.spec.ts:15:1 › 1 見出しが出る (105ms)
  ✘  2 [chromium] › tests/shop.spec.ts:19:1 › 2 価格が出る (556ms)
  ✓  3 [chromium] › tests/shop.spec.ts:23:1 › 3 商品が2つ並ぶ (107ms)
  ✘  4 [chromium] › tests/shop.spec.ts:27:1 › 4 カートに入れるボタンがある (556ms)
  ✘  5 [chromium] › tests/shop.spec.ts:31:1 › 5 購入ボタンがある (580ms)
  ✓  6 [chromium] › tests/shop.spec.ts:35:1 › 6 ヘルプへのリンクがある (104ms)
  ✘  7 [chromium] › tests/shop.spec.ts:39:1 › 7 ログインへのリンクがある (560ms)
  ✓  8 [chromium] › tests/shop.spec.ts:43:1 › 8 りんごが先頭にある (103ms)
  4 failed
    [chromium] › tests/shop.spec.ts:19:1 › 2 価格が出る ─────────────────────────────────────────────────
    [chromium] › tests/shop.spec.ts:27:1 › 4 カートに入れるボタンがある ─────────────────────────────────────────
    [chromium] › tests/shop.spec.ts:31:1 › 5 購入ボタンがある ──────────────────────────────────────────────
    [chromium] › tests/shop.spec.ts:39:1 › 7 ログインへのリンクがある ──────────────────────────────────────────
  4 passed (5.1s)

All 8 tests ran, and 4 failed. The exit code was 1.

Stop at the second failure with –max-failures=2

I ran the same tests with --max-failures=2.

bash
npx playwright test --reporter=list --max-failures=2
text
  ✓  1 [chromium] › tests/shop.spec.ts:15:1 › 1 見出しが出る (114ms)
  ✘  2 [chromium] › tests/shop.spec.ts:19:1 › 2 価格が出る (557ms)
  ✓  3 [chromium] › tests/shop.spec.ts:23:1 › 3 商品が2つ並ぶ (94ms)
  ✘  4 [chromium] › tests/shop.spec.ts:27:1 › 4 カートに入れるボタンがある (552ms)
Testing stopped early after 2 maximum allowed failures.
  2 failed
    [chromium] › tests/shop.spec.ts:19:1 › 2 価格が出る ─────────────────────────────────────────────────
    [chromium] › tests/shop.spec.ts:27:1 › 4 カートに入れるボタンがある ─────────────────────────────────────────
  4 did not run
  2 passed (2.2s)
  1 error was not a part of any test, see above for details

Test 4 failed, which made the failure count 2, and the run stopped with "Testing stopped early after 2 maximum allowed failures." Tests 5 to 8 did not run, and the summary shows "4 did not run."

The last line of the summary, "1 error was not a part of any test," means there was one error outside any test. I ran the same tests again with only the reporter changed to JSON, using --max-failures=2. The content of that error was "Testing stopped early after 2 maximum allowed failures." When I ran without --max-failures, this line did not appear.

-x is the same as --max-failures=1, so it stops at the first failure (command-line options). When I added -x to the same tests, the run stopped at the second test's failure and printed "Testing stopped early after 1 maximum allowed failures." and "6 did not run."

Always stop with maxFailures in the config file

Instead of adding the option to every command, you can set maxFailures in playwright.config.ts. According to the official documentation, it is the maximum number of failures for the whole test run, and 0 (the default) means never stop.

To check the stopping behavior, I prepared 12 tests that all fail. Each test loads an empty page, waits for a heading that never appears, and fails after the default expect timeout (5 seconds). I set maxFailures: 3 in the config file.

ts
// playwright.broken.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
  testDir: './tests-broken',
  workers: 1,
  maxFailures: 3,
  projects: [{ name: 'chromium', use: { browserName: 'chromium' } }],
});
bash
npx playwright test -c playwright.broken.config.ts --reporter=list
text
  ✘   1 [chromium] › tests-broken/broken.spec.ts:9:3 › 画面1の見出しが出る (5.1s)
  ✘   2 [chromium] › tests-broken/broken.spec.ts:9:3 › 画面2の見出しが出る (5.1s)
  ✘   3 [chromium] › tests-broken/broken.spec.ts:9:3 › 画面3の見出しが出る (5.1s)
Testing stopped early after 3 maximum allowed failures.
  3 failed
    [chromium] › tests-broken/broken.spec.ts:9:3 › 画面1の見出しが出る ──────────────────────────────────────
    [chromium] › tests-broken/broken.spec.ts:9:3 › 画面2の見出しが出る ──────────────────────────────────────
    [chromium] › tests-broken/broken.spec.ts:9:3 › 画面3の見出しが出る ──────────────────────────────────────
  9 did not run
  1 error was not a part of any test, see above for details

The command-line option took priority over the config file. With the same config, --max-failures=1 stopped at the first failure, and --max-failures=0 ran all 12 tests without stopping. You can set the config to stop, and then use --max-failures=0 only when you want to see every failure.

The total time from starting the command to finishing was as follows.

Bar chart of the time to finish running 12 always-failing tests. Without stopping (--max-failures=0), all 12 ran in 66.4 seconds. With maxFailures: 3, 3 ran in 17.1 seconds. With --max-failures=1, 1 ran in 6.0 seconds.
Time to run the 12 always-failing tests with different stop settings

Without stopping, the run took 66.4 seconds. With maxFailures: 3 it took 17.1 seconds, and with --max-failures=1 it took 6.0 seconds. Each failure took about 5 seconds, so the time saved is roughly the number of tests that did not run, multiplied by 5 seconds.

The official documentation's example stops only in CI:

ts
export default defineConfig({
  maxFailures: process.env.CI ? 1 : 0,
});

Tests that pass on retry are not counted as failures

I set retries: 1 and ran the tests with --max-failures=2. There were 6 tests: A, which fails only on the first attempt; C and E, which always fail; and B, D, and F, which always pass. A fails only when testInfo.retry is 0.

bash
npx playwright test -c playwright.retry.config.ts --reporter=list --max-failures=2
text
  ✘  1 tests-retry/retry.spec.ts:3:1 › A 1回目だけ落ちる (5ms)
  ✓  2 tests-retry/retry.spec.ts:3:1 › A 1回目だけ落ちる (retry #1) (4ms)
  ✓  3 tests-retry/retry.spec.ts:7:1 › B いつも通る (5ms)
  ✘  4 tests-retry/retry.spec.ts:11:1 › C いつも落ちる (3ms)
  ✘  5 tests-retry/retry.spec.ts:11:1 › C いつも落ちる (retry #1) (6ms)
  ✓  6 tests-retry/retry.spec.ts:15:1 › D いつも通る (4ms)
  ✘  7 tests-retry/retry.spec.ts:19:1 › E いつも落ちる (3ms)
  ✘  8 tests-retry/retry.spec.ts:19:1 › E いつも落ちる (retry #1) (7ms)
Testing stopped early after 2 maximum allowed failures.
  2 failed
    tests-retry/retry.spec.ts:11:1 › C いつも落ちる ──────────────────────────────────────────────────────
    tests-retry/retry.spec.ts:19:1 › E いつも落ちる ──────────────────────────────────────────────────────
  1 flaky
    tests-retry/retry.spec.ts:3:1 › A 1回目だけ落ちる ─────────────────────────────────────────────────────
  1 did not run
  2 passed (1.8s)
  1 error was not a part of any test, see above for details

Two ✘ lines had already appeared by line 4, but the run did not stop there. It stopped after line 8, when E failed again on retry. The two counted failures were C and E, which failed even after all retries were used. A passed on retry, so the summary counts it as flaky, and it was not counted as a failure. F did not run ("1 did not run").

Before the count reached the limit of 2, C and E each ran twice. When retries are enabled, the run executes more attempts before it stops.

In parallel runs, tests that are still running are also stopped

I set 2 workers (workers: 2, fullyParallel: true) and ran a slow test that passes after 3 seconds, two tests that fail in 0.2 seconds, and one test that passes immediately, with --max-failures=2.

bash
npx playwright test -c playwright.parallel.config.ts --reporter=list --max-failures=2
text
  ✘  1 tests-parallel/parallel.spec.ts:8:1 › すぐ落ちるテスト1 (206ms)
  ✘  3 tests-parallel/parallel.spec.ts:13:1 › すぐ落ちるテスト2 (209ms)
Testing stopped early after 2 maximum allowed failures.
  ✘  2 tests-parallel/parallel.spec.ts:3:1 › 遅いテスト(3秒かかって通る) (695ms)
  2 failed
    tests-parallel/parallel.spec.ts:8:1 › すぐ落ちるテスト1 ────────────────────────────────────────────────
    tests-parallel/parallel.spec.ts:13:1 › すぐ落ちるテスト2 ───────────────────────────────────────────────
  1 interrupted
    tests-parallel/parallel.spec.ts:3:1 › 遅いテスト(3秒かかって通る) ──────────────────────────────────────────
  1 did not run
  1 error was not a part of any test, see above for details

The slow test was running on the other worker. It was cut off at 695ms, without waiting the full 3 seconds, and the summary counted it as "1 interrupted." Even a test that would have passed after 3 seconds is shown with ✘ when it is cut off. It is not included in the failed count. The test that passed immediately did not run ("1 did not run").

Teardown projects still run after the stop

I also tested a setup where I prepare and clean up data around the tests, using project dependencies and teardown. The setup project has teardown: 'cleanup', and the app project, which contains the test bodies, depends on setup.

ts
// playwright.teardown.config.ts (projects part)
projects: [
  { name: 'setup', testMatch: /.*\.setup\.ts/, teardown: 'cleanup' },
  { name: 'cleanup', testMatch: /.*\.teardown\.ts/ },
  { name: 'app', testMatch: /.*\.spec\.ts/, dependencies: ['setup'] },
],

I ran this configuration with --max-failures=1.

bash
npx playwright test -c playwright.teardown.config.ts --reporter=list --max-failures=1

After the first test in app failed and the run stopped, the cleanup tests still ran, and the console.log message "teardown: cleaned up the data" was printed.

text
setup: データを用意した
  ✓  1 [setup] › tests-teardown/db.setup.ts:3:1 › テスト用のデータを用意する (1ms)
  ✘  2 [app] › tests-teardown/app.spec.ts:3:1 › 落ちるテスト1 (5ms)
Testing stopped early after 1 maximum allowed failures.
teardown: データを片づけた
  ✓  3 [cleanup] › tests-teardown/db.teardown.ts:3:1 › テスト用のデータを片づける (1ms)
  1 failed
    [app] › tests-teardown/app.spec.ts:3:1 › 落ちるテスト1 ───────────────────────────────────────────────
  2 did not run
  2 passed (982ms)
  1 error was not a part of any test, see above for details

Even when the run stopped early, the cleanup of the test data was not skipped.

Point

In every run here, the exit code was 1 when the run stopped because of --max-failures or maxFailures. This is the same exit code as a normal test failure, so a script that checks the exit code will see the run as failed.

​Posted by 86