重试
简介
测试重试是一种在测试失败时自动重新运行测试的方法。当测试不稳定并间歇性失败时,这会很有用。测试重试在配置文件中配置。
失败
Playwright Test 在 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 进程 #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
您可以在配置文件中配置重试
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在运行时检测重试,该方法可用于任何测试、钩子或夹具。这是一个在重试之前清除某些服务器端状态的示例。
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 为每个测试创建一个独立的页面对象。但是,如果您想在多个测试之间重用单个页面对象,您可以在test.beforeAll()中创建自己的,并在test.afterAll()中关闭它。
- TypeScript
- JavaScript
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();
});
// @ts-check
const { test } = require('@playwright/test');
test.describe.configure({ mode: 'serial' });
/** @type {import('@playwright/test').Page} */
let 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();
});