Playwright 並列・シャーディング・リトライ
この章では、Playwright の並列実行戦略、シャーディング実行、失敗時のリトライ機構を紹介し、大規模なテストスイートを効率的に実行するのに役立ちます。
並列実行
Playwright はデフォルトで複数の Worker プロセスを使用してテストを並列実行し、マルチコア CPU を最大限に活用します。
fullyParallel 設定
fullyParallel同じファイル内のテストを並列実行するかどうかを制御します。
例
// ファイルパス: playwright.config.ts
export default defineConfig({
fullyParallel: true, // 同じファイル内のテストも並列実行
});
export default defineConfig({
fullyParallel: true, // 同じファイル内のテストも並列実行
});
当 fullyParallel: false時、同じファイル内のテストは直列に実行され、異なるファイル間では並列に実行されます。
当 fullyParallel: true時、すべてのテストが完全に並列実行されます(同じファイル内のテストも含みます)。
workers 数の制御
例
export default defineConfig({
// 4 つの Worker を使用
workers: 4,
// ローカルではデフォルト値、CI では固定で 2 つ
workers: process.env.CI ? 2 : undefined,
// シングルスレッドモード(デバッグに便利)
workers: 1,
});
// 4 つの Worker を使用
workers: 4,
// ローカルではデフォルト値、CI では固定で 2 つ
workers: process.env.CI ? 2 : undefined,
// シングルスレッドモード(デバッグに便利)
workers: 1,
});
直列テスト
使用test.describe.serial同じグループ内のテストを強制的に直列実行します。
例
// 直列実行 – 各テストは前のテストの結果に依存
test.describe.serial('ユーザー登録からログイン', () => {
test('ステップ1:登録', async ({ page }) => { /* ... */ });
test('ステップ2:メール検証', async ({ page }) => { /* ... */ });
test('ステップ3:ログイン', async ({ page }) => { /* ... */ });
});
test.describe.serial('ユーザー登録からログイン', () => {
test('ステップ1:登録', async ({ page }) => { /* ... */ });
test('ステップ2:メール検証', async ({ page }) => { /* ... */ });
test('ステップ3:ログイン', async ({ page }) => { /* ... */ });
});
直列テストはテストの分離性を損なうため、使用を避けてください。
テストを順番に実行する必要がある場合は、複数のステップを1つのテストにまとめることを検討してください。
局所並列制御
によってtest.describe.configure単一のグループの並列モードを制御できます。
例
test.describe('グループテスト', () => {
test.describe.configure({ mode: 'serial' }); // 強制的に直列実行
// test.describe.configure({ mode: 'parallel' }); // 強制的に並列実行
test('A', async () => {});
test('B', async () => {});
});
test.describe.configure({ mode: 'serial' }); // 強制的に直列実行
// test.describe.configure({ mode: 'parallel' }); // 強制的に並列実行
test('A', async () => {});
test('B', async () => {});
});
Sharding(シャーディング)実行
シャーディングは、テストを複数の CI マシンに分散して実行し、全体の実行時間を短縮します。
使用方法
# 机器 1:运行第 1/4 片 npx playwright test --shard=1/4 # 机器 2:运行第 2/4 片 npx playwright test --shard=2/4 # 机器 3:运行第 3/4 片 npx playwright test --shard=3/4 # 机器 4:运行第 4/4 片 npx playwright test --shard=4/4
シャーディングレポートの結合
すべてのマシンの実行が完了したら、生成されたレポートファイルを結合します。
# 每台机器生成报告(不自动打开) npx playwright test --shard=1/4 --reporter=blob # 合并所有 blob 报告 npx playwright merge-reports --reporter=html ./all-blob-reports
GitHub Actions でのシャーディング設定
例
# ファイルパス: .github/workflows/playwright.yml
jobs:
test:
strategy:
fail-fast: false
matrix:
shardIndex: [1, 2, 3, 4]
shardTotal: [4]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm ci
- run: npx playwright test --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}
jobs:
test:
strategy:
fail-fast: false
matrix:
shardIndex: [1, 2, 3, 4]
shardTotal: [4]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm ci
- run: npx playwright test --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}
リトライ機構
リトライにより、失敗したテストが自動的に再実行され、断続的な不安定要素を軽減します。
リトライの設定
例
// ファイルパス: playwright.config.ts
export default defineConfig({
// ローカルではリトライしない(迅速なフィードバック)、CI では 2 回リトライ
retries: process.env.CI ? 2 : 0,
});
export default defineConfig({
// ローカルではリトライしない(迅速なフィードバック)、CI では 2 回リトライ
retries: process.env.CI ? 2 : 0,
});
単一ファイルまたはグループごとにリトライを設定
例
// グループ全体にリトライを設定
test.describe.configure({ retries: 3 });
test.describe('不安定な機能(複数回のリトライが必要)', () => {
test('テスト A', async ({ page }) => { /* ... */ });
test('テスト B', async ({ page }) => { /* ... */ });
});
test.describe.configure({ retries: 3 });
test.describe('不安定な機能(複数回のリトライが必要)', () => {
test('テスト A', async ({ page }) => { /* ... */ });
test('テスト B', async ({ page }) => { /* ... */ });
});
リトライ戦略
| 戦略 | 説明 |
|---|---|
| CI 環境でのリトライ | ローカルではリトライしない(クイックフィードバック)、CI では 2〜3 回リトライ |
| Trace 収集 | と併用trace: 'on-first-retry'、リトライ時のみ Trace を記録 |
| リトライを乱用しない | リトライは本当のバグを隠すことはできません。あるテストが頻繁に失敗し、リトライしないと通らない場合は、根本原因を修正する必要があります。 |