Skip to main content

Reporter

测试运行器通知报告者测试执行期间的各种事件。报告者的所有方法都是可选的。

¥Test runner notifies the reporter about various events during test execution. All methods of the reporter are optional.

你可以通过使用某些报告器方法实现类来创建自定义报告器。确保将此类导出为默认类。

¥You can create a custom reporter by implementing a class with some of the reporter methods. Make sure to export this class as default.

my-awesome-reporter.ts
import type {
Reporter, FullConfig, Suite, TestCase, TestResult, FullResult
} from '@playwright/test/reporter';

class MyReporter implements Reporter {
constructor(options: { customOption?: string } = {}) {
console.log(`my-awesome-reporter setup with customOption set to ${options.customOption}`);
}

onBegin(config: FullConfig, suite: Suite) {
console.log(`Starting the run with ${suite.allTests().length} tests`);
}

onTestBegin(test: TestCase) {
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 一起使用。了解有关 使用报告器 的更多信息。

¥Now use this reporter with testConfig.reporter. Learn more about using reporters.

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

export default defineConfig({
reporter: [['./my-awesome-reporter.ts', { customOption: 'some value' }]],
});

以下是报告器致电的典型顺序:

¥Here is a typical order of reporter calls:

此外,当工作进程中生成标准输出时(可能是在测试执行期间),会调用 reporter.onStdOut()reporter.onStdErr();当测试执行之外出现问题时,会调用 reporter.onError()

¥Additionally, reporter.onStdOut() and reporter.onStdErr() are called when standard output is produced in the worker process, possibly during a test execution, and reporter.onError() is called when something went wrong outside of the test execution.

如果你的自定义报告器未在终端上打印任何内容,请实现 reporter.printsToStdio() 并返回 false。这样,除了你的自定义报告器之外,Playwright 还将使用标准终端报告器之一来增强用户体验。

¥If your custom reporter does not print anything to the terminal, implement reporter.printsToStdio() and return false. This way, Playwright will use one of the standard terminal reporters in addition to your custom reporter to enhance user experience.

合并报告 API 说明

¥Merged report API notes

通过 merge-reports CLI 命令合并多个 blob 报告时,将调用相同的 Reporter API 来生成最终报告,并且所有现有报告器都应正常工作而无需进行任何更改。但有一些细微的差别可能会影响一些自定义报告器。

¥When merging multiple blob reports via merge-reports CLI command, the same Reporter API is called to produce final reports and all existing reporters should work without any changes. There some subtle differences though which might affect some custom reporters.

  • 来自不同分片的项目始终作为单独的 TestProject 对象保存。例如如果项目 '桌面 Chrome' 分布在 5 台机器上,那么在传递给 reporter.onBegin() 的配置中将有 5 个同名的项目实例。

    ¥Projects from different shards are always kept as separate TestProject objects. E.g. if project 'Desktop Chrome' was sharded across 5 machines then there will be 5 instances of projects with the same name in the config passed to reporter.onBegin().


方法

¥Methods

onBegin

Added in: v1.10 reporter.onBegin

在运行测试之前调用一次。所有测试均已被发现并放入 Suite 的层次结构中。

¥Called once before running tests. All tests have been already discovered and put into a hierarchy of Suites.

用法

¥Usage

reporter.onBegin(config, suite);

参数

¥Arguments

已解决的配置。

¥Resolved configuration.

包含所有项目、文件和测试用例的根套件。

¥The root suite that contains all projects, files and test cases.


onEnd

Added in: v1.10 reporter.onEnd

在运行所有测试或测试已中断后调用。请注意,此方法可能会返回 Promise,并且 Playwright Test 将等待它。报告者可以覆盖状态,从而影响测试运行器的退出代码。

¥Called after all tests have been run, or testing has been interrupted. Note that this method may return a Promise and Playwright Test will await it. Reporter is allowed to override the status and hence affect the exit code of the test runner.

用法

¥Usage

await reporter.onEnd(result);

参数

¥Arguments

  • status "passed" | "failed" | "timedout" | "interrupted"

    试运行状态。

    ¥Test run status.

    • startTime [Date]

试运行开始挂墙时间。

¥Test run start wall time.

测试运行持续时间(以毫秒为单位)。

¥Test run duration in milliseconds.

完整测试运行的结果,status 可以是以下之一:

¥Result of the full test run, status can be one of:

  • 'passed' - 一切都按预期进行。

    ¥'passed' - Everything went as expected.

  • 'failed' - 任何测试都失败了。

    ¥'failed' - Any test has failed.

  • 'timedout' - 已达到 testConfig.globalTimeout

    ¥'timedout' - The testConfig.globalTimeout has been reached.

  • 'interrupted' - 被用户中断。

    ¥'interrupted' - Interrupted by the user.

返回

¥Returns

  • status "passed" | "failed" | "timedout" | "interrupted"(可选)

    ¥status "passed" | "failed" | "timedout" | "interrupted" (optional)


onError

Added in: v1.10 reporter.onError

在某些全局错误上调用,例如工作进程中未处理的异常。

¥Called on some global error, for example unhandled exception in the worker process.

用法

¥Usage

reporter.onError(error);

参数

¥Arguments

错误。

¥The error.


onExit

Added in: v1.33 reporter.onExit

在测试运行程序存在之前立即调用。此时所有的报告器都收到了 reporter.onEnd() 信号,所以所有的报道都应该建立了。你可以运行此钩子中上传报告的代码。

¥Called immediately before test runner exists. At this point all the reporters have received the reporter.onEnd() signal, so all the reports should be build. You can run the code that uploads the reports in this hook.

用法

¥Usage

await reporter.onExit();

返回

¥Returns


onStdErr

Added in: v1.10 reporter.onStdErr

当某些内容写入工作进程中的标准错误时调用。

¥Called when something has been written to the standard error in the worker process.

用法

¥Usage

reporter.onStdErr(chunk, test, result);

参数

¥Arguments

输出块。

¥Output chunk.

正在运行的测试。请注意,当没有测试运行时可能会出现输出,在这种情况下,这将是 void

¥Test that was running. Note that output may happen when no test is running, in which case this will be void.

测试运行的结果,该对象在测试运行时被填充。

¥Result of the test run, this object gets populated while the test runs.


onStdOut

Added in: v1.10 reporter.onStdOut

当某些内容写入工作进程中的标准输出时调用。

¥Called when something has been written to the standard output in the worker process.

用法

¥Usage

reporter.onStdOut(chunk, test, result);

参数

¥Arguments

输出块。

¥Output chunk.

正在运行的测试。请注意,当没有测试运行时可能会出现输出,在这种情况下,这将是 void

¥Test that was running. Note that output may happen when no test is running, in which case this will be void.

测试运行的结果,该对象在测试运行时被填充。

¥Result of the test run, this object gets populated while the test runs.


onStepBegin

Added in: v1.10 reporter.onStepBegin

当测试步骤在工作进程中启动时调用。

¥Called when a test step started in the worker process.

用法

¥Usage

reporter.onStepBegin(test, result, step);

参数

¥Arguments

测试该步骤所属。

¥Test that the step belongs to.

测试运行的结果,该对象在测试运行时被填充。

¥Result of the test run, this object gets populated while the test runs.

已启动的测试步骤实例。

¥Test step instance that has started.


onStepEnd

Added in: v1.10 reporter.onStepEnd

当工作进程中的测试步骤完成时调用。

¥Called when a test step finished in the worker process.

用法

¥Usage

reporter.onStepEnd(test, result, step);

参数

¥Arguments

测试该步骤所属。

¥Test that the step belongs to.

测试运行的结果。

¥Result of the test run.

已完成的测试步骤实例。

¥Test step instance that has finished.


onTestBegin

Added in: v1.10 reporter.onTestBegin

在工作进程中开始测试后调用。

¥Called after a test has been started in the worker process.

用法

¥Usage

reporter.onTestBegin(test, result);

参数

¥Arguments

测试已经开始。

¥Test that has been started.

测试运行的结果,该对象在测试运行时被填充。

¥Result of the test run, this object gets populated while the test runs.


onTestEnd

Added in: v1.10 reporter.onTestEnd

在工作进程中完成测试后调用。

¥Called after a test has been finished in the worker process.

用法

¥Usage

reporter.onTestEnd(test, result);

参数

¥Arguments

测试已经完成。

¥Test that has been finished.

测试运行的结果。

¥Result of the test run.


printsToStdio

Added in: v1.10 reporter.printsToStdio

该报告器是否使用 stdio 进行报道。如果没有,Playwright Test 可以添加一些输出以增强用户体验。如果你的报告器没有打印到终端,强烈建议返回 false

¥Whether this reporter uses stdio for reporting. When it does not, Playwright Test could add some output to enhance user experience. If your reporter does not print to the terminal, it is strongly recommended to return false.

用法

¥Usage

reporter.printsToStdio();

返回

¥Returns