日本語

How to Re-run Only the Failed Tests from the Last Run in Playwright

​​Playwright​E2E Testing,TypeScript

When you run Playwright tests with npx playwright test --last-failed, only the tests that failed in the previous run are executed again. When some E2E tests fail, you no longer need to re-run everything after each fix. You can check whether the fix worked by running just the failing tests.

This option was added in Playwright 1.44 (1.44 release notes). I also checked in a test environment where the record of failed tests is stored and which runs overwrite it.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, and @playwright/test 1.63.0. The tests were written in TypeScript, only Chromium (headless) was used as the browser, and I ran with a single worker. I did not test running on CI services such as GitHub Actions, splitting runs with --shard, UI mode, or configurations with multiple projects (browsers).

Setting up two tests that fail

For testing, I prepared a cart screen and a login screen as HTML files. I put one deliberate bug into each. The cart shows the total as 400 yen instead of 450 yen, and the login screen does not show an error message when the button is pressed with the field empty.

I wrote five tests across two files. tests/cart.spec.ts looks like this:

ts
import { test, expect } from '@playwright/test';
import { readFileSync } from 'node:fs';

test.beforeEach(async ({ page }) => {
  await page.setContent(readFileSync('site/cart.html', 'utf8'));
});

test('商品が2つ並ぶ', async ({ page }) => {
  await expect(page.getByRole('listitem')).toHaveCount(2);
});

test('合計金額が450円になる', async ({ page }) => {
  await expect(page.locator('#total')).toHaveText('合計 450円', { timeout: 1000 });
});

test('購入ボタンがある', async ({ page }) => {
  await expect(page.getByRole('button', { name: '購入する' })).toBeVisible();
});

tests/login.spec.ts contains a test that checks whether the email input field exists, and a test that checks whether pressing the login button with the field empty shows an error message asking for an email address. The configuration file is as follows.

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

export default defineConfig({
  testDir: './tests',
  workers: 1,
  projects: [{ name: 'chromium', use: { browserName: 'chromium' } }],
});

First, I ran all the tests. The output in this article keeps only the line for each test, the summary, and the headings of the failed tests. The detailed error contents are omitted.

bash
npx playwright test --reporter=list
text
  ✓  1 [chromium] › tests/cart.spec.ts:8:1 › 商品が2つ並ぶ (129ms)
  ✘  2 [chromium] › tests/cart.spec.ts:12:1 › 合計金額が450円になる (1.1s)
  ✓  3 [chromium] › tests/cart.spec.ts:16:1 › 購入ボタンがある (141ms)
  ✓  4 [chromium] › tests/login.spec.ts:8:1 › メールアドレスの入力欄がある (82ms)
  ✘  5 [chromium] › tests/login.spec.ts:12:1 › 空のまま押すとエラーが出る (1.1s)
  2 failed
    [chromium] › tests/cart.spec.ts:12:1 › 合計金額が450円になる ────────────────────────────────────────────
    [chromium] › tests/login.spec.ts:12:1 › 空のまま押すとエラーが出る ──────────────────────────────────────────
  3 passed (3.8s)

Failed tests are recorded in test-results/.last-run.json

After the run finished, a file named .last-run.json had been created in the test-results folder. Its name starts with ., so I displayed it with ls -A.

text
$ ls -A test-results
.last-run.json
cart-合計金額が450円になる-chromium
login-空のまま押すとエラーが出る-chromium

The file contains the result of the run and a list of the IDs of the failed tests.

json
{
  "status": "failed",
  "failedTests": [
    "1981448f0b79506fb670-eedaec467fb28aff7eca",
    "41d3fd2474a19feb00a1-d1c4794834b5cfa297a4"
  ]
}

--last-failed runs only the tests listed in failedTests in this file. As stated in the official command-line documentation, the default location of the file is <outputDir>/.last-run.json, and if you have not set outputDir, that is test-results.

Before running, you can check which tests will be targeted with --list. When --list was used, the tests were not run and .last-run.json was not rewritten.

bash
npx playwright test --last-failed --list
text
Listing tests:
  [chromium] › cart.spec.ts:12:1 › 合計金額が450円になる
  [chromium] › login.spec.ts:12:1 › 空のまま押すとエラーが出る
Total: 2 tests in 2 files

Re-running with –last-failed while fixing

When I ran with --last-failed without changing anything, only the two failed tests ran, and both failed again. The contents of the record did not change.

Next, I changed the cart total to 450 yen and ran --last-failed again.

bash
npx playwright test --last-failed --reporter=list
text
  ✓  1 [chromium] › tests/cart.spec.ts:12:1 › 合計金額が450円になる (120ms)
  ✘  2 [chromium] › tests/login.spec.ts:12:1 › 空のまま押すとエラーが出る (1.1s)
  1 failed
    [chromium] › tests/login.spec.ts:12:1 › 空のまま押すとエラーが出る ──────────────────────────────────────────
  1 passed (1.9s)

After this run, the failedTests in .last-run.json contained only the ID of the login test. Next, I fixed the login error message and ran --last-failed. Only the login test ran and passed, and the record became "status": "passed" with "failedTests": [].

text
  ✓  1 [chromium] › tests/login.spec.ts:12:1 › 空のまま押すとエラーが出る (182ms)
  1 passed (798ms)

Each time you run with --last-failed, the record is rewritten with the results of the tests that ran. So when you repeat --last-failed, the number of tests that run goes down by the number of tests you have fixed.

Three-stage diagram. Stage 1, "Run everything": of the five tests, "total amount" and "error display" are marked in pink as "failed", and two failures are recorded. Stage 2, "Fix the cart and run --last-failed": "total amount" passes, "error display" fails, and the other three are marked with dotted lines as "not run", and one failure is recorded. Stage 3, "Fix login and run --last-failed again": only "error display" runs and passes, and zero failures are recorded. (The labels in the image are in Japanese.)
The tests that run and the number of recorded failures when –last-failed is repeated

When no failures remain, "No tests found" is reported and the exit code is 1

When the failedTests in the record is empty and you run with --last-failed, there are no tests to run, and it fails with an error.

bash
npx playwright test --last-failed --reporter=list
text
Error: No tests found

The exit code in this case was 1. This is the same exit code as when tests fail, so for a script that judges success or failure by the exit code, it looks like a failure. When I added --pass-with-no-tests as well, no tests ran and it exited with code 0.

bash
npx playwright test --last-failed --pass-with-no-tests --reporter=list

Conversely, when .last-run.json does not exist (for example, after you have deleted test-results) and you run with --last-failed, no filtering is applied, and all five tests ran.

The record is overwritten by the last run

.last-run.json is rewritten every time, even by a run that does not use --last-failed.

After running all the tests and having two fail, I ran only tests/cart.spec.ts.

bash
npx playwright test tests/cart.spec.ts --reporter=list
text
  ✓  1 [chromium] › tests/cart.spec.ts:8:1 › 商品が2つ並ぶ (129ms)
  ✘  2 [chromium] › tests/cart.spec.ts:12:1 › 合計金額が450円になる (1.1s)
  ✓  3 [chromium] › tests/cart.spec.ts:16:1 › 購入ボタンがある (128ms)
  1 failed
    [chromium] › tests/cart.spec.ts:12:1 › 合計金額が450円になる ────────────────────────────────────────────
  2 passed (2.7s)

After this, .last-run.json contained only the ID of the cart test. When I then ran --last-failed, only the cart test ran, and the login failure was no longer targeted.

Warning

To avoid overlooking failures you have not fixed yet, do not run anything else between a full run and --last-failed.

When you stop at the first failure, tests that did not run are also targeted

When I ran with -x (stop at the first failure), the run stopped at the failed cart total test, and the other three did not run.

text
  ✓  1 [chromium] › tests/cart.spec.ts:8:1 › 商品が2つ並ぶ (125ms)
  ✘  2 [chromium] › tests/cart.spec.ts:12:1 › 合計金額が450円になる (1.1s)
  1 failed
    [chromium] › tests/cart.spec.ts:12:1 › 合計金額が450円になる ────────────────────────────────────────────
  3 did not run
  1 passed (1.8s)

In this case, the failedTests in .last-run.json contained four IDs: the one that failed and the three that did not run. When I then ran --last-failed, the four tests other than the one that passed (the test that checks two items are listed) ran. Tests that did not run are targeted by the next --last-failed too, so tests you have not checked yet are not left out.

text
  ✘  1 [chromium] › tests/cart.spec.ts:12:1 › 合計金額が450円になる (1.1s)
  ✓  2 [chromium] › tests/cart.spec.ts:16:1 › 購入ボタンがある (137ms)
  ✓  3 [chromium] › tests/login.spec.ts:8:1 › メールアドレスの入力欄がある (95ms)
  ✘  4 [chromium] › tests/login.spec.ts:12:1 › 空のまま押すとエラーが出る (1.1s)
  2 failed
    [chromium] › tests/cart.spec.ts:12:1 › 合計金額が450円になる ────────────────────────────────────────────
    [chromium] › tests/login.spec.ts:12:1 › 空のまま押すとエラーが出る ──────────────────────────────────────────
  2 passed (3.7s)

Flaky tests that pass on retry are not targeted

I set retries: 1 and ran three tests: one that always passes, one that fails only on the first attempt, and one that always fails. The second test was written to fail only when testInfo.retry is 0, so it passes on retry and is flaky.

text
  ✓  1 tests-flaky/flaky.spec.ts:3:1 › いつも通る (6ms)
  ✘  2 tests-flaky/flaky.spec.ts:7:1 › 1回目だけ落ちる (4ms)
  ✓  3 tests-flaky/flaky.spec.ts:7:1 › 1回目だけ落ちる (retry #1) (7ms)
  ✘  4 tests-flaky/flaky.spec.ts:11:1 › いつも落ちる (5ms)
  ✘  5 tests-flaky/flaky.spec.ts:11:1 › いつも落ちる (retry #1) (7ms)
  1 failed
  1 flaky
  1 passed (1.6s)

Only one ID was stored in .last-run.json. When I ran --last-failed next, only the test that always fails (いつも落ちる) ran. --last-failed cannot find flaky tests. They appear as "flaky" in the summary when retries are configured, so check them there.

Changing the location of the record with –last-failed-file

.last-run.json is created inside test-results, so deleting that folder also deletes the record. With --last-failed-file, you can specify where the record is written and read. The environment variable PLAYWRIGHT_LAST_RUN_OUTPUT_FILE does the same thing.

When I ran all the tests with --last-failed-file=ci/last-run.json, the two IDs were written to ci/last-run.json, and no .last-run.json was created in test-results.

bash
npx playwright test --last-failed-file=ci/last-run.json --reporter=list
text
$ ls -A test-results ci
ci:
last-run.json

test-results:
cart-合計金額が450円になる-chromium
login-空のまま押すとエラーが出る-chromium

After that, I deleted test-results and ran --last-failed with the same file specified. Only the two failed tests ran.

bash
npx playwright test --last-failed --last-failed-file=ci/last-run.json --reporter=list
text
  ✘  1 [chromium] › tests/cart.spec.ts:12:1 › 合計金額が450円になる (1.1s)
  ✘  2 [chromium] › tests/login.spec.ts:12:1 › 空のまま押すとエラーが出る (1.1s)
  2 failed
    [chromium] › tests/cart.spec.ts:12:1 › 合計金額が450円になる ────────────────────────────────────────────
    [chromium] › tests/login.spec.ts:12:1 › 空のまま押すとエラーが出る ──────────────────────────────────────────

When I used PLAYWRIGHT_LAST_RUN_OUTPUT_FILE=ci/last-run.json instead of --last-failed-file, the same two tests ran. Even in a project that cleans up test-results, you can keep the record in a separate location and re-run only the failed tests later.

​Posted by 86