背景
弊社のQAチームでは、E2EテストをPlaywrightを用いて実行しておりますが、シナリオ数増大に伴ってテスト実行時間の増加・テストが不安定になるなどが課題になっておりました。(純粋にPlaywrightのWorkersを増やして並列実行するとやや不安定な挙動になるように見受けられました。)
そこでE2Eテストをディレクトリで分けて、Github actionsのmatrixを使ってディレクトリごとに並列実行することにより上記の課題を解決できないかと考えました。
matrixを使用してPlaywrightのテストを実行
純粋にmatrixを使用してディレクトごとに並列でPlaywrightのテストを実行するコードの例は以下のような形になるかと思います。(nameの配列で指定している名前でテストがディレクトリ分けされているものとします。)
name: e2e
on:
workflow_dispatch:
jobs:
test:
strategy:
fail-fast: false
matrix:
name: ["apple", "banana", "orange"]
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install dependencies
run: npm ci
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test --project=e2e-${{ matrix.name }}
workflow_dispatchのinputsを使うように修正
しかし上記のコードでは、ひとつのディレクトリのテストのみを実行したいシチュエーションに対応できない形となっています。そこでworkflow_dispatchのinputsからmatrixの内容を動的に変更して、テストを実行したいディレクトリを指定できるように修正します。
name: e2e
on:
workflow_dispatch:
inputs:
tenant:
description: "tenant"
type: choice
options:
- '"apple","banana","orange"'
- '"apple"'
- '"banana"'
- '"orange"'
required: true
jobs:
test:
strategy:
fail-fast: false
matrix:
name: ${{ fromJSON(format('[{0}]', inputs.tenant || '"apple","banana","orange"')) }}
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install dependencies
run: npm ci
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test --project=e2e-${{ matrix.name }}
Actionsでのinputsの選択イメージとしては以下のような形となるかと思います。
以下の記述をすることで、workflow_dispatchで選択した値をmatrixに指定することができます。例えばappleを選択した場合は、appleディレクトリのファイルのみテストが実行されます。特に指定がない場合は、"apple","banana","orange"ディレクトリすべてが実行されます。
matrix:
name: ${{ fromJSON(format('[{0}]', inputs.tenant || '"apple","banana","orange"')) }}