跳到主要内容

报告器

引言

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',
});

内置报告器

所有内置报告器都会显示关于失败的详细信息,主要区别在于成功运行时的详细程度。

列表报告器

列表报告器是默认的(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 }]],
});

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

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

行报告器

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

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

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

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

点报告器

点报告器非常简洁 - 每个成功运行的测试只产生一个字符。它是 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超时
°跳过

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

环境变量名称报告器配置选项描述默认值
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当报告在浏览器中打开时,它将在此端口提供服务。9323 或当 9323 不可用时的任何可用端口。
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输出文件的完整路径。如果已定义,则忽略 outputDirfileNameundefined

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_NAMEJSON 报告打印到标准输出 (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_NAMEJUnit 报告打印到标准输出 (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 actions 中运行时使用内置的 github 报告器以获取自动失败注解。

请注意,所有其他报告器也在 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"

这里有一些在你编写自己的报告器时可以参考的开源报告器实现列表