🎭

[Playwright] 条件付きでテストをスキップする

2025/02/05に公開

はじめに

この記事では、Playwrightで条件でスキップする 方法ををまとめております。

参考資料

結論

テストコード内にtest.skip()を追記し、条件とコメントを記載する

1.Skipしない

test.spec.ts
import { test, expect } from '@playwright/test';
const ENV:string = "development"

test('DEV環境はテスト実行する', async ({ page }) => {
  test.skip(ENV === 'production', '本番環境は○○ためテスト対象外');
  // --- 準備(Arrange) ---
  // --- 実行(Act) ---
  // --- 確認(Assert) ---
});

実行結果を確認します

$ npx playwright test tests/e2e/test.spec.ts

Running 1 test using 1 worker

  ✓  1 [Microsoft Edge] › test.spec.ts:5:5 › DEV環境はテスト実行する (1.0s)

  1 passed (2.9s)

2. Skipする

test.spec.ts
import { test, expect } from '@playwright/test';
const ENV:string = "production"

test('DEV環境はテスト実行する', async ({ page }) => {
  test.skip(ENV === 'production', '本番環境は○○ためテスト対象外');
  // --- 準備(Arrange) ---
  // --- 実行(Act) ---
  // --- 確認(Assert) ---
});
Skipしない/するのテストコード差分
import { test, expect } from '@playwright/test';
- const ENV:string = "development"
+ const ENV:string = "production"

test('DEV環境はテスト実行する', async ({ page }) => {
  test.skip(ENV === 'production', '本番環境は○○ためテスト対象外');
  // --- 準備(Arrange) ---
  // --- 実行(Act) ---
  // --- 確認(Assert) ---
});

実行結果を確認します

$ npx playwright test tests/e2e/test.spec.ts

Running 1 test using 1 worker

  -  1 [Microsoft Edge] › test.spec.ts:5:5 › DEV環境はテスト実行する

  1 skipped
GitHubで編集を提案

Discussion