报告器
简介
Playwright Test 提供了一些内置报告器以满足不同需求,并支持自定义报告器。尝试内置报告器的最简单方法是使用 --reporter
命令行选项。
npx playwright test --reporter=line
如需更多控制,您可以在配置文件中以编程方式指定报告器。
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: 'line',
});
多报告器
您可以同时使用多个报告器。例如,可以使用'list'
获得美观的终端输出,同时使用'json'
获取包含测试结果的完整json文件。
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: [
['list'],
['json', { outputFile: 'test-results.json' }]
],
});
CI上的报告器
您可以在本地和CI上使用不同的报告器。例如,使用简洁的'dot'
报告器可以避免过多输出。这是CI上的默认设置。
import { defineConfig } from '@playwright/test';
export default defineConfig({
// Concise 'dot' for CI, default 'list' when running locally
reporter: process.env.CI ? 'dot' : 'list',
});
内置报告器
所有内置的报告器都会显示关于失败的详细信息,主要区别在于成功运行时的详细程度。
列表报告器
列表报告器是默认选项(在CI环境中除外,默认使用dot
报告器)。它会为每个运行的测试打印一行信息。
npx playwright test --reporter=list
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: 'list',
});
以下是一个测试运行过程中的示例输出。失败情况将在最后列出。
npx playwright test --reporter=list
Running 124 tests using 6 workers
1 ✓ should access error in env (438ms)
2 ✓ handle long test names (515ms)
3 x 1) render expected (691ms)
4 ✓ should timeout (932ms)
5 should repeat each:
6 ✓ should respect enclosing .gitignore (569ms)
7 should teardown env after timeout:
8 should respect excluded tests:
9 ✓ should handle env beforeEach error (638ms)
10 should respect enclosing .gitignore:
您可以通过传递以下配置选项来选择启用步骤渲染:
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: [['list', { printSteps: true }]],
});
列表报告支持以下配置选项和环境变量:
Environment Variable Name | Reporter Config Option | Description | Default |
---|---|---|---|
PLAYWRIGHT_LIST_PRINT_STEPS | printSteps | Whether to print each step on its own line. | false |
PLAYWRIGHT_FORCE_TTY | Whether to produce output suitable for a live terminal. If a number is specified, it will also be used as the terminal width. | true when terminal is in TTY mode, false otherwise. | |
FORCE_COLOR | Whether to produce colored output. | true when terminal is in TTY mode, false otherwise. |
行报告器
行报告器比列表报告器更为简洁。它使用单行报告最后完成的测试,并在出现失败时打印失败信息。行报告器适用于大型测试套件,它能显示进度但不会通过列出所有测试来刷屏输出。
npx playwright test --reporter=line
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: 'line',
});
以下是测试运行过程中的一个示例输出。失败情况会内联报告。
npx playwright test --reporter=line
Running 124 tests using 6 workers
1) dot-reporter.spec.ts:20:1 › render expected ===================================================
Error: expect(received).toBe(expected) // Object.is equality
Expected: 1
Received: 0
[23/124] gitignore.spec.ts - should respect nested .gitignore
行报告支持以下配置选项和环境变量:
Environment Variable Name | Reporter Config Option | Description | Default |
---|---|---|---|
PLAYWRIGHT_FORCE_TTY | Whether to produce output suitable for a live terminal. If a number is specified, it will also be used as the terminal width. | true when terminal is in TTY mode, false otherwise. | |
FORCE_COLOR | Whether to produce colored output. | true when terminal is in TTY mode, false otherwise. |
点状报告器
点报告器非常简洁 - 每个成功的测试运行仅输出一个字符。它是CI环境中的默认选项,适用于不需要大量输出的场景。
npx playwright test --reporter=dot
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: 'dot',
});
以下是一个测试运行过程中的示例输出。失败情况将在最后列出。
npx playwright test --reporter=dot
Running 124 tests using 6 workers
······F·············································
每个运行的测试都会显示一个字符,表示其状态:
Character | Description |
---|---|
· | Passed |
F | Failed |
× | Failed or timed out - and will be retried |
± | Passed on retry (flaky) |
T | Timed out |
° | Skipped |
Dot报告支持以下配置选项和环境变量:
Environment Variable Name | Reporter Config Option | Description | Default |
---|---|---|---|
PLAYWRIGHT_FORCE_TTY | Whether to produce output suitable for a live terminal. If a number is specified, it will also be used as the terminal width. | true when terminal is in TTY mode, false otherwise. | |
FORCE_COLOR | Whether to produce colored output. | true when terminal is in TTY mode, false otherwise. |
HTML reporter
HTML reporter produces a self-contained folder that contains report for the test run that can be served as a web page.
npx playwright test --reporter=html
By default, HTML report is opened automatically if some of the tests failed. You can control this behavior via the open
property in the Playwright config or the PLAYWRIGHT_HTML_OPEN
environmental variable. The possible values for that property are always
, never
and on-failure
(default).
You can also configure host
and port
that are used to serve the HTML report.
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: [['html', { open: 'never' }]],
});
By default, report is written into the playwright-report
folder in the current working directory. One can override that location using the PLAYWRIGHT_HTML_OUTPUT_DIR
environment variable or a reporter configuration.
在配置文件中,直接传递选项:
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: [['html', { outputFolder: 'my-report' }]],
});
If you are uploading attachments from data folder to other location, you can use attachmentsBaseURL
option to let html report where to look for them.
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: [['html', { attachmentsBaseURL: 'https://external-storage.com/' }]],
});
快速打开最后一次测试运行报告的方法是:
npx playwright show-report
或者如果存在自定义文件夹名称:
npx playwright show-report my-report
HTML report supports the following configuration options and environment variables:
Environment Variable Name | Reporter Config Option | Description | Default |
---|---|---|---|
PLAYWRIGHT_HTML_OUTPUT_DIR | outputFolder | Directory to save the report to. | playwright-report |
PLAYWRIGHT_HTML_OPEN | open | When to open the html report in the browser, one of 'always' , 'never' or 'on-failure' | 'on-failure' |
PLAYWRIGHT_HTML_HOST | host | When report opens in the browser, it will be served bound to this hostname. | localhost |
PLAYWRIGHT_HTML_PORT | port | When report opens in the browser, it will be served on this port. | 9323 or any available port when 9323 is not available. |
PLAYWRIGHT_HTML_ATTACHMENTS_BASE_URL | attachmentsBaseURL | A separate location where attachments from the data subdirectory are uploaded. Only needed when you upload report and data separately to different locations. | data/ |
Blob 报告器
Blob报告包含有关测试运行的所有详细信息,可用于后续生成任何其他报告。它们的主要功能是促进来自分片测试的报告合并。
npx playwright test --reporter=blob
默认情况下,报告会写入package.json所在目录或当前工作目录(如果未找到package.json)下的blob-report
目录。报告文件名格式为report-
或使用分片时的report-
。该哈希值是根据--grep
、--grepInverted
、--project
以及命令行参数传递的文件过滤器计算得出的可选值。这个哈希值确保使用不同命令行选项运行Playwright时会产生不同但在多次运行间保持稳定的报告名称。输出文件名可以通过配置文件覆盖,或通过'PLAYWRIGHT_BLOB_OUTPUT_FILE'
环境变量指定。
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: [['blob', { outputFile: `./blob-report/report-${os.platform()}.zip` }]],
});
Blob报告支持以下配置选项和环境变量:
Environment Variable Name | Reporter Config Option | Description | Default |
---|---|---|---|
PLAYWRIGHT_BLOB_OUTPUT_DIR | outputDir | Directory to save the output. Existing content is deleted before writing the new report. | blob-report |
PLAYWRIGHT_BLOB_OUTPUT_NAME | fileName | Report file name. | report-<project>-<hash>-<shard_number>.zip |
PLAYWRIGHT_BLOB_OUTPUT_FILE | outputFile | Full path to the output file. If defined, outputDir and fileName will be ignored. | undefined |
JSON报告器
JSON报告器生成一个包含测试运行所有信息的对象。
最有可能的是您希望将JSON写入文件。当使用--reporter=json
运行时,请使用PLAYWRIGHT_JSON_OUTPUT_NAME
环境变量:
- Bash
- PowerShell
- 批量处理
PLAYWRIGHT_JSON_OUTPUT_NAME=results.json npx playwright test --reporter=json
$env:PLAYWRIGHT_JSON_OUTPUT_NAME="results.json"
npx playwright test --reporter=json
set PLAYWRIGHT_JSON_OUTPUT_NAME=results.json
npx playwright test --reporter=json
在配置文件中,直接传递选项:
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: [['json', { outputFile: 'results.json' }]],
});
JSON报告支持以下配置选项和环境变量:
Environment Variable Name | Reporter Config Option | Description | Default |
---|---|---|---|
PLAYWRIGHT_JSON_OUTPUT_DIR | Directory to save the output file. Ignored if output file is specified. | cwd or config directory. | |
PLAYWRIGHT_JSON_OUTPUT_NAME | outputFile | Base file name for the output, relative to the output dir. | JSON report is printed to the stdout. |
PLAYWRIGHT_JSON_OUTPUT_FILE | outputFile | Full path to the output file. If defined, PLAYWRIGHT_JSON_OUTPUT_DIR and PLAYWRIGHT_JSON_OUTPUT_NAME will be ignored. | JSON report is printed to the stdout. |
JUnit 报告器
JUnit报告器生成JUnit风格的xml报告。
您很可能希望将报告写入一个xml文件。当使用--reporter=junit
运行时,请使用PLAYWRIGHT_JUNIT_OUTPUT_NAME
环境变量:
- Bash
- PowerShell
- 批量处理
PLAYWRIGHT_JUNIT_OUTPUT_NAME=results.xml npx playwright test --reporter=junit
$env:PLAYWRIGHT_JUNIT_OUTPUT_NAME="results.xml"
npx playwright test --reporter=junit
set PLAYWRIGHT_JUNIT_OUTPUT_NAME=results.xml
npx playwright test --reporter=junit
在配置文件中,直接传递选项:
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: [['junit', { outputFile: 'results.xml' }]],
});
JUnit报告支持以下配置选项和环境变量:
Environment Variable Name | Reporter Config Option | Description | Default |
---|---|---|---|
PLAYWRIGHT_JUNIT_OUTPUT_DIR | Directory to save the output file. Ignored if output file is not specified. | cwd or config directory. | |
PLAYWRIGHT_JUNIT_OUTPUT_NAME | outputFile | Base file name for the output, relative to the output dir. | JUnit report is printed to the stdout. |
PLAYWRIGHT_JUNIT_OUTPUT_FILE | outputFile | Full path to the output file. If defined, PLAYWRIGHT_JUNIT_OUTPUT_DIR and PLAYWRIGHT_JUNIT_OUTPUT_NAME will be ignored. | JUnit report is printed to the stdout. |
PLAYWRIGHT_JUNIT_STRIP_ANSI | stripANSIControlSequences | Whether to remove ANSI control sequences from the text before writing it in the report. | By default output text is added as is. |
PLAYWRIGHT_JUNIT_INCLUDE_PROJECT_IN_TEST_NAME | includeProjectInTestName | Whether to include Playwright project name in every test case as a name prefix. | By default not included. |
PLAYWRIGHT_JUNIT_SUITE_ID | Value of the id attribute on the root <testsuites/> report entry. | Empty string. | |
PLAYWRIGHT_JUNIT_SUITE_NAME | Value of the name attribute on the root <testsuites/> report entry. | Empty string. |
GitHub Actions 注解
您可以使用内置的 github
报告器,在 GitHub Actions 中运行时获取自动失败标注。
请注意,所有其他报告器在GitHub Actions上同样有效,但不提供注释功能。此外,如果使用矩阵策略运行测试,不建议使用此注释类型,因为堆栈跟踪失败会成倍增加并模糊GitHub文件视图。
import { defineConfig } from '@playwright/test';
export default defineConfig({
// 'github' for GitHub Actions CI to generate annotations, plus a concise 'dot'
// default 'list' when running locally
reporter: process.env.CI ? 'github' : 'list',
});
自定义报告器
您可以通过实现一个包含部分报告器方法的类来创建自定义报告器。了解更多关于Reporter API的信息。
import type {
FullConfig, FullResult, Reporter, Suite, TestCase, TestResult
} from '@playwright/test/reporter';
class MyReporter implements Reporter {
onBegin(config: FullConfig, suite: Suite) {
console.log(`Starting the run with ${suite.allTests().length} tests`);
}
onTestBegin(test: TestCase, result: TestResult) {
console.log(`Starting test ${test.title}`);
}
onTestEnd(test: TestCase, result: TestResult) {
console.log(`Finished test ${test.title}: ${result.status}`);
}
onEnd(result: FullResult) {
console.log(`Finished the run: ${result.status}`);
}
}
export default MyReporter;
现在通过 testConfig.reporter 使用此报告器。
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: './my-awesome-reporter.ts',
});
或者直接将报告文件路径作为 --reporter
命令行选项传递:
npx playwright test --reporter="./myreporter/my-awesome-reporter.ts"
以下是开源报告器实现的简短列表,您可以在编写自己的报告器时参考: