跳到主内容

重试

引言

测试重试是一种在测试失败时自动重新运行测试的方法。这对于间歇性失败的测试非常有用。测试重试在配置文件中配置。

失败

Playwright Test 在 worker 进程中运行测试。这些进程是操作系统进程,独立运行,由测试运行器进行编排。所有 worker 都具有相同的环境,每个 worker 都启动自己的浏览器。

考虑以下代码片段

import { test } from '@playwright/test';

test.describe('suite', () => {
test.beforeAll(async () => { /* ... */ });
test('first good', async ({ page }) => { /* ... */ });
test('second flaky', async ({ page }) => { /* ... */ });
test('third good', async ({ page }) => { /* ... */ });
test.afterAll(async () => { /* ... */ });
});

所有测试通过时,它们将在同一个 worker 进程中按顺序运行。

  • Worker 进程启动
    • beforeAll 钩子运行
    • first good 通过
    • second flaky 通过
    • third good 通过
    • afterAll 钩子运行

如果任何测试失败,Playwright Test 将丢弃整个 worker 进程及其浏览器,并启动一个新的 worker 进程。测试将在新的 worker 进程中继续进行,从下一个测试开始。

  • Worker 进程 #1 启动
    • beforeAll 钩子运行
    • first good 通过
    • second flaky 失败
    • afterAll 钩子运行
  • Worker 进程 #2 启动
    • beforeAll 钩子再次运行
    • third good 通过
    • afterAll 钩子运行

如果您启用重试,第二个 worker 进程将通过重试失败的测试并从那里继续。

  • Worker 进程 #1 启动
    • beforeAll 钩子运行
    • first good 通过
    • second flaky 失败
    • afterAll 钩子运行
  • Worker 进程 #2 启动
    • beforeAll 钩子再次运行
    • second flaky 被重试并成功通过
    • third good 通过
    • afterAll 钩子运行

此方案非常适合独立测试,并保证失败的测试不会影响健康的测试。

重试

Playwright 支持测试重试。启用后,失败的测试将被多次重试,直到它们通过,或直到达到最大重试次数。默认情况下,失败的测试不会被重试。

# Give failing tests 3 retry attempts
npx playwright test --retries=3

您可以在配置文件中配置重试

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

export default defineConfig({
// Give failing tests 3 retry attempts
retries: 3,
});

Playwright Test 将测试分为以下类别

  • "passed" - 首次运行时通过的测试;
  • "flaky" - 首次运行时失败,但在重试时通过的测试;
  • "failed" - 首次运行时失败并所有重试都失败的测试。
Running 3 tests using 1 worker

✓ example.spec.ts:4:2 › first passes (438ms)
x example.spec.ts:5:2 › second flaky (691ms)
✓ example.spec.ts:5:2 › second flaky (522ms)
✓ example.spec.ts:6:2 › third passes (932ms)

1 flaky
example.spec.ts:5:2 › second flaky
2 passed (4s)

您可以在运行时使用 testInfo.retry 检测重试,它可用于任何测试、钩子或 fixture。这是一个在重试前清除某些服务器端状态的示例。

import { test, expect } from '@playwright/test';

test('my test', async ({ page }, testInfo) => {
if (testInfo.retry)
await cleanSomeCachesOnTheServer();
// ...
});

您可以使用 test.describe.configure() 为特定的测试组或单个文件指定重试次数。

import { test, expect } from '@playwright/test';

test.describe(() => {
// All tests in this describe group will get 2 retry attempts.
test.describe.configure({ retries: 2 });

test('test 1', async ({ page }) => {
// ...
});

test('test 2', async ({ page }) => {
// ...
});
});

串行模式

使用 test.describe.serial() 将相互依赖的测试分组,以确保它们始终一起按顺序运行。如果其中一个测试失败,所有后续测试都将被跳过。组中的所有测试都将一起重试。

考虑使用 test.describe.serial 的以下代码片段

import { test } from '@playwright/test';

test.describe.configure({ mode: 'serial' });

test.beforeAll(async () => { /* ... */ });
test('first good', async ({ page }) => { /* ... */ });
test('second flaky', async ({ page }) => { /* ... */ });
test('third good', async ({ page }) => { /* ... */ });

在没有重试的情况下运行时,失败后的所有测试都将被跳过

  • Worker 进程 #1
    • beforeAll 钩子运行
    • first good 通过
    • second flaky 失败
    • third good 完全被跳过

在有重试的情况下运行时,所有测试将一起重试

  • Worker 进程 #1
    • beforeAll 钩子运行
    • first good 通过
    • second flaky 失败
    • third good 被跳过
  • Worker 进程 #2
    • beforeAll 钩子再次运行
    • first good 再次通过
    • second flaky 通过
    • third good 通过
注意

通常最好让您的测试相互隔离,这样它们才能高效地独立运行和重试。

在测试之间复用单个页面

Playwright Test 为每个测试创建一个隔离的 Page 对象。但是,如果您想在多个测试之间复用单个 Page 对象,您可以在 test.beforeAll() 中创建自己的对象,并在 test.afterAll() 中将其关闭。

example.spec.ts
import { test, type Page } from '@playwright/test';

test.describe.configure({ mode: 'serial' });

let page: Page;

test.beforeAll(async ({ browser }) => {
page = await browser.newPage();
});

test.afterAll(async () => {
await page.close();
});

test('runs first', async () => {
await page.goto('https://playwright.net.cn/');
});

test('runs second', async () => {
await page.getByText('Get Started').click();
});