テスト設定
はじめに
Playwright には、テストの実行方法を設定するための多くのオプションがあります。これらのオプションは設定ファイルで指定できます。テストランナーのオプションはトップレベルであり、use セクションには入れないでください。
基本設定
ここでは、最も一般的な設定オプションをいくつか紹介します。
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
// Look for test files in the "tests" directory, relative to this configuration file.
testDir: 'tests',
// Run all tests in parallel.
fullyParallel: true,
// Fail the build on CI if you accidentally left test.only in the source code.
forbidOnly: !!process.env.CI,
// Retry on CI only.
retries: process.env.CI ? 2 : 0,
// Opt out of parallel tests on CI.
workers: process.env.CI ? 1 : undefined,
// Reporter to use
reporter: 'html',
use: {
// Base URL to use in actions like `await page.goto('/')`.
baseURL: 'https://:3000',
// Collect trace when retrying the failed test.
trace: 'on-first-retry',
},
// Configure projects for major browsers.
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
],
// Run your local dev server before starting the tests.
webServer: {
command: 'npm run start',
url: 'https://:3000',
reuseExistingServer: !process.env.CI,
},
});
| オプション | 説明 |
|---|---|
| testConfig.forbidOnly | test.only とマークされたテストがある場合にエラーで終了するかどうか。CI で役立ちます。 |
| testConfig.fullyParallel | すべてのファイルのすべてのテストを並行して実行します。詳細については、並列処理とシャーディングを参照してください。 |
| testConfig.projects | 複数の構成または複数のブラウザでテストを実行します |
| testConfig.reporter | 使用するレポーター。利用可能なレポーターの詳細については、テストレポーターを参照してください。 |
| testConfig.retries | テストごとの再試行の最大回数。再試行の詳細については、テストの再試行を参照してください。 |
| testConfig.testDir | テストファイルのあるディレクトリ。 |
| testConfig.use | use{} を使用したオプション |
| testConfig.webServer | テスト中にサーバーを起動するには、webServer オプションを使用します |
| testConfig.workers | テストを並列化するために使用する同時ワーカプロセスの最大数。論理 CPU コアの割合として設定することもできます。たとえば、'50%'。詳細については、並列処理とシャーディングを参照してください。 |
テストのフィルタリング
グロブパターンまたは正規表現でテストをフィルタリングします。
playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
// Glob patterns or regular expressions to ignore test files.
testIgnore: '*test-assets',
// Glob patterns or regular expressions that match test files.
testMatch: '*todo-tests/*.spec.ts',
});
| オプション | 説明 |
|---|---|
| testConfig.testIgnore | テストファイルを探すときに無視すべきグロブパターンまたは正規表現。たとえば、'*test-assets' |
| testConfig.testMatch | テストファイルに一致するグロブパターンまたは正規表現。たとえば、'*todo-tests/*.spec.ts'。デフォルトでは、Playwright は .*(test|spec).(js|ts|mjs) ファイルを実行します。 |
高度な設定
playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
// Folder for test artifacts such as screenshots, videos, traces, etc.
outputDir: 'test-results',
// path to the global setup files.
globalSetup: require.resolve('./global-setup'),
// path to the global teardown files.
globalTeardown: require.resolve('./global-teardown'),
// Each test is given 30 seconds.
timeout: 30000,
});
| オプション | 説明 |
|---|---|
| testConfig.globalSetup | グローバルセットアップファイルへのパス。このファイルはすべてのテストの前に読み込まれて実行されます。単一の関数をエクスポートする必要があります。 |
| testConfig.globalTeardown | グローバルティアダウンファイルへのパス。このファイルはすべてのテストの後に読み込まれて実行されます。単一の関数をエクスポートする必要があります。 |
| testConfig.outputDir | スクリーンショット、ビデオ、トレースなどのテスト成果物のフォルダ。 |
| testConfig.timeout | Playwright は、各テストにタイムアウトを強制します(デフォルトでは 30 秒)。テスト関数、テストフィクスチャ、および beforeEach フックで費やされた時間は、テストタイムアウトに含まれます。 |
Expect オプション
expect アサーションライブラリの設定。
playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
expect: {
// Maximum time expect() should wait for the condition to be met.
timeout: 5000,
toHaveScreenshot: {
// An acceptable amount of pixels that could be different, unset by default.
maxDiffPixels: 10,
},
toMatchSnapshot: {
// An acceptable ratio of pixels that are different to the
// total amount of pixels, between 0 and 1.
maxDiffPixelRatio: 0.1,
},
},
});
| オプション | 説明 |
|---|---|
| testConfig.expect | expect(locator).toHaveText() のようなWeb ファーストアサーションは、デフォルトで 5 秒の個別のタイムアウトを持ちます。これは、条件が満たされるのを expect() が待機する最大時間です。テストと期待されるタイムアウト、および単一のテストに対してそれらをどのように設定するかについて詳しく学習してください。 |
| expect(page).toHaveScreenshot() | expect(locator).toHaveScreenshot() メソッドの設定。 |
| expect(value).toMatchSnapshot() | expect(locator).toMatchSnapshot() メソッドの設定。 |