跳到主要内容

报告器

简介

Playwright Test 自带一些内置的报告器,以满足不同的需求,并能够提供自定义报告器。尝试内置报告器的最简单方法是传递 --reporter 命令行选项

npx playwright test --reporter=line

为了获得更多控制,您可以在配置文件中以编程方式指定报告器。

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
reporter: 'line',
});

多个报告器

您可以同时使用多个报告器。例如,您可以使用 'list' 来获得漂亮的终端输出,并使用 'json' 来获得包含测试结果的综合 json 文件。

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
reporter: [
['list'],
['json', { outputFile: 'test-results.json' }]
],
});

CI 上的报告器

您可以在本地和 CI 上使用不同的报告器。例如,使用简洁的 'dot' 报告器可以避免过多的输出。这是 CI 上的默认设置。

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
// Concise 'dot' for CI, default 'list' when running locally
reporter: process.env.CI ? 'dot' : 'list',
});

内置报告器

所有内置报告器都显示有关失败的详细信息,并且主要在成功运行的详细程度上有所不同。

List 报告器

List 报告器是默认报告器(CI 上除外,CI 上默认使用 dot 报告器)。它为每个正在运行的测试打印一行。

npx playwright test --reporter=list
playwright.config.ts
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:

您可以通过传递以下配置选项来选择步骤渲染

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
reporter: [['list', { printSteps: true }]],
});

List 报告支持以下配置选项和环境变量

环境变量名报告器配置选项描述默认值
PLAYWRIGHT_LIST_PRINT_STEPSprintSteps是否在单独的行上打印每个步骤。false
PLAYWRIGHT_FORCE_TTY是否生成适合实时终端的输出。如果指定数字,它也将用作终端宽度。当终端处于 TTY 模式时为 true,否则为 false
FORCE_COLOR是否生成彩色输出。当终端处于 TTY 模式时为 true,否则为 false

Line 报告器

Line 报告器比 list 报告器更简洁。它使用单行报告最后完成的测试,并在失败发生时打印失败。Line 报告器对于大型测试套件很有用,它可以显示进度,但不会通过列出所有测试来垃圾邮件输出。

npx playwright test --reporter=line
playwright.config.ts
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

Line 报告支持以下配置选项和环境变量

环境变量名报告器配置选项描述默认值
PLAYWRIGHT_FORCE_TTY是否生成适合实时终端的输出。如果指定数字,它也将用作终端宽度。当终端处于 TTY 模式时为 true,否则为 false
FORCE_COLOR是否生成彩色输出。当终端处于 TTY 模式时为 true,否则为 false

Dot 报告器

Dot 报告器非常简洁 - 它只为每次成功的测试运行生成一个字符。它是 CI 上的默认报告器,适用于您不希望有大量输出的情况。

npx playwright test --reporter=dot
playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
reporter: 'dot',
});

这是测试运行中间的示例输出。失败将在最后列出。

npx playwright test --reporter=dot
Running 124 tests using 6 workers
······F·············································

为每个已运行的测试显示一个字符,指示其状态

字符描述
·通过
F失败
×失败或超时 - 将重试
±重试通过(不稳定)
T超时
°跳过

Dot 报告支持以下配置选项和环境变量

环境变量名报告器配置选项描述默认值
PLAYWRIGHT_FORCE_TTY是否生成适合实时终端的输出。如果指定数字,它也将用作终端宽度。当终端处于 TTY 模式时为 true,否则为 false
FORCE_COLOR是否生成彩色输出。当终端处于 TTY 模式时为 true,否则为 false

HTML 报告器

HTML 报告器生成一个自包含文件夹,其中包含可作为网页提供的测试运行报告。

npx playwright test --reporter=html

默认情况下,如果某些测试失败,HTML 报告会自动打开。您可以通过 Playwright 配置中的 open 属性或 PLAYWRIGHT_HTML_OPEN 环境变量来控制此行为。该属性的可能值为 alwaysneveron-failure(默认)。

您还可以配置用于提供 HTML 报告的 hostport

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
reporter: [['html', { open: 'never' }]],
});

默认情况下,报告写入当前工作目录中的 playwright-report 文件夹。可以使用 PLAYWRIGHT_HTML_OUTPUT_DIR 环境变量或报告器配置来覆盖该位置。

在配置文件中,直接传递选项

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
reporter: [['html', { outputFolder: 'my-report' }]],
});

如果要将 data 文件夹中的附件上传到其他位置,可以使用 attachmentsBaseURL 选项,让 html 报告知道在哪里查找它们。

playwright.config.ts
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 报告支持以下配置选项和环境变量

环境变量名报告器配置选项描述默认值
PLAYWRIGHT_HTML_OUTPUT_DIRoutputFolder保存报告的目录。playwright-report
PLAYWRIGHT_HTML_OPENopen何时在浏览器中打开 html 报告,选项为 'always''never''on-failure' 之一'on-failure'
PLAYWRIGHT_HTML_HOSThost当报告在浏览器中打开时,它将绑定到此主机名。localhost
PLAYWRIGHT_HTML_PORTport当报告在浏览器中打开时,它将在此端口上提供服务。93239323 不可用时的任何可用端口。
PLAYWRIGHT_HTML_ATTACHMENTS_BASE_URLattachmentsBaseURL一个单独的位置,用于上传 data 子目录中的附件。仅当您将报告和 data 分别上传到不同的位置时才需要。data/

Blob 报告器

Blob 报告包含有关测试运行的所有详细信息,以后可用于生成任何其他报告。它们的主要功能是方便合并来自分片测试的报告。

npx playwright test --reporter=blob

默认情况下,报告写入 package.json 目录或当前工作目录(如果未找到 package.json)中的 blob-report 目录。报告文件名类似于 report-<hash>.zipreport-<hash>-<shard_number>.zip,当使用分片时。哈希是从 --grep--grepInverted--project 和作为命令行参数传递的文件过滤器计算的可选值。哈希保证使用不同命令行选项运行 Playwright 将生成不同的报告名称,但在运行之间保持稳定。可以在配置文件中覆盖输出文件名,或作为 'PLAYWRIGHT_BLOB_OUTPUT_FILE' 环境变量传递。

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
reporter: [['blob', { outputFile: `./blob-report/report-${os.platform()}.zip` }]],
});

Blob 报告支持以下配置选项和环境变量

环境变量名报告器配置选项描述默认值
PLAYWRIGHT_BLOB_OUTPUT_DIRoutputDir保存输出的目录。在写入新报告之前,现有内容将被删除。blob-report
PLAYWRIGHT_BLOB_OUTPUT_NAMEfileName报告文件名。report-<project>-<hash>-<shard_number>.zip
PLAYWRIGHT_BLOB_OUTPUT_FILEoutputFile输出文件的完整路径。如果已定义,则 outputDirfileName 将被忽略。undefined

JSON 报告器

JSON 报告器生成一个包含有关测试运行所有信息的对象。

很可能您希望将 JSON 写入文件。当使用 --reporter=json 运行时,请使用 PLAYWRIGHT_JSON_OUTPUT_NAME 环境变量

PLAYWRIGHT_JSON_OUTPUT_NAME=results.json npx playwright test --reporter=json

在配置文件中,直接传递选项

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
reporter: [['json', { outputFile: 'results.json' }]],
});

JSON 报告支持以下配置选项和环境变量

环境变量名报告器配置选项描述默认值
PLAYWRIGHT_JSON_OUTPUT_DIR保存输出文件的目录。如果指定了输出文件,则忽略。cwd 或配置目录。
PLAYWRIGHT_JSON_OUTPUT_NAMEoutputFile输出的基本文件名,相对于输出目录。JSON 报告打印到 stdout。
PLAYWRIGHT_JSON_OUTPUT_FILEoutputFile输出文件的完整路径。如果已定义,则 PLAYWRIGHT_JSON_OUTPUT_DIRPLAYWRIGHT_JSON_OUTPUT_NAME 将被忽略。JSON 报告打印到 stdout。

JUnit 报告器

JUnit 报告器生成 JUnit 样式的 xml 报告。

很可能您希望将报告写入 xml 文件。当使用 --reporter=junit 运行时,请使用 PLAYWRIGHT_JUNIT_OUTPUT_NAME 环境变量

PLAYWRIGHT_JUNIT_OUTPUT_NAME=results.xml npx playwright test --reporter=junit

在配置文件中,直接传递选项

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
reporter: [['junit', { outputFile: 'results.xml' }]],
});

JUnit 报告支持以下配置选项和环境变量

环境变量名报告器配置选项描述默认值
PLAYWRIGHT_JUNIT_OUTPUT_DIR保存输出文件的目录。如果未指定输出文件,则忽略。cwd 或配置目录。
PLAYWRIGHT_JUNIT_OUTPUT_NAMEoutputFile输出的基本文件名,相对于输出目录。JUnit 报告打印到 stdout。
PLAYWRIGHT_JUNIT_OUTPUT_FILEoutputFile输出文件的完整路径。如果已定义,则 PLAYWRIGHT_JUNIT_OUTPUT_DIRPLAYWRIGHT_JUNIT_OUTPUT_NAME 将被忽略。JUnit 报告打印到 stdout。
PLAYWRIGHT_JUNIT_STRIP_ANSIstripANSIControlSequences是否从文本中删除 ANSI 控制序列,然后再将其写入报告中。默认情况下,输出文本按原样添加。
PLAYWRIGHT_JUNIT_INCLUDE_PROJECT_IN_TEST_NAMEincludeProjectInTestName是否在每个测试用例中包含 Playwright 项目名称作为名称前缀。默认情况下不包含。
PLAYWRIGHT_JUNIT_SUITE_ID<testsuites/> 报告条目上 id 属性的值。空字符串。
PLAYWRIGHT_JUNIT_SUITE_NAME<testsuites/> 报告条目上 name 属性的值。空字符串。

GitHub Actions 注解

您可以使用内置的 github 报告器在 GitHub actions 中运行时获得自动失败注解。

请注意,所有其他报告器也适用于 GitHub Actions,但不提供注解。此外,如果不使用矩阵策略运行测试,则不建议使用此注解类型,因为堆栈跟踪失败会成倍增加并模糊 GitHub 文件视图。

playwright.config.ts
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 的更多信息。

my-awesome-reporter.ts
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 一起使用。

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
reporter: './my-awesome-reporter.ts',
});

或者只是将报告器文件路径作为 --reporter 命令行选项传递

npx playwright test --reporter="./myreporter/my-awesome-reporter.ts"

以下是开源报告器实现的一个简短列表,您可以在编写自己的报告器时查看