Reporter
测试运行器会在测试执行期间向报告器通知各种事件。报告器的所有方法都是可选的。
您可以通过实现一个包含部分报告器方法的类来创建自定义报告器。请确保将此类作为默认导出。
- TypeScript
- JavaScript
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;
// @ts-check
/** @implements {import('@playwright/test/reporter').Reporter} */
class MyReporter {
constructor(options) {
console.log(`my-awesome-reporter setup with customOption set to ${options.customOption}`);
}
onBegin(config, suite) {
console.log(`Starting the run with ${suite.allTests().length} tests`);
}
onTestBegin(test) {
console.log(`Starting test ${test.title}`);
}
onTestEnd(test, result) {
console.log(`Finished test ${test.title}: ${result.status}`);
}
onEnd(result) {
console.log(`Finished the run: ${result.status}`);
}
}
module.exports = MyReporter;
现在通过testConfig.reporter使用此报告器。了解更多关于using reporters的信息。
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: [['./my-awesome-reporter.ts', { customOption: 'some value' }]],
});
以下是典型的报告器调用顺序:
- reporter.onBegin() 会被调用一次,传入一个包含所有其他套件和测试的根套件。了解更多关于suites hierarchy的信息。
- reporter.onTestBegin() 会在每个测试运行时被调用。它接收一个正在执行的TestCase和一个几乎为空的TestResult。测试结果会在测试运行过程中被填充(例如,包含步骤和标准输入输出),并在测试完成后获得最终的
status
状态。 - reporter.onStepBegin() 和 reporter.onStepEnd() 会在测试中每个执行的步骤时被调用。当步骤执行时,测试运行尚未完成。
- reporter.onTestEnd() 在测试运行结束时被调用。此时,TestResult 已完成,您可以使用 testResult.status、testResult.error 等属性。
- reporter.onEnd() 在所有应运行的测试完成后会被调用一次。
- reporter.onExit() 会在测试运行器退出前立即被调用。
此外,当工作进程中产生标准输出时(可能在测试执行期间),会调用reporter.onStdOut()和reporter.onStdErr();当测试执行之外出现问题时,会调用reporter.onError()。
如果您的自定义报告器没有在终端打印任何内容,请实现reporter.printsToStdio()并返回false
。这样,Playwright将在您的自定义报告器之外使用一个标准终端报告器来增强用户体验。
合并报告API说明
当通过merge-reports
CLI命令合并多个blob
报告时,会调用相同的Reporter API来生成最终报告,所有现有报告器都应无需任何更改即可工作。不过存在一些细微差异,可能会影响某些自定义报告器。
- 来自不同分片的项目始终作为独立的TestProject对象保存。例如,如果项目"Desktop Chrome"被分片到5台机器上,那么在传递给reporter.onBegin()的配置中,将会有5个同名的项目实例。
方法
onBegin
Added in: v1.10在运行测试之前调用一次。所有测试已经被发现并放入Suite的层次结构中。
用法
reporter.onBegin(config, suite);
参数
-
config
FullConfig#已解析的配置。
-
包含所有项目、文件和测试用例的根套件。
onEnd
Added in: v1.10在所有测试运行完毕或测试被中断后调用。请注意,此方法可能返回一个Promise,Playwright Test将等待它。报告器可以覆盖状态,从而影响测试运行器的退出代码。
用法
await reporter.onEnd(result);
参数
result
Object#-
status
"passed" | "failed" | "timedout" | "interrupted"测试运行状态。
-
startTime
Date测试运行的开始实际时间。
-
duration
number测试运行的持续时间,以毫秒为单位。
status
can be one of:'passed'
- 一切按预期进行。'failed'
- 任何测试失败时。'timedout'
- 已达到testConfig.globalTimeout设置的时间限制。'interrupted'
- 被用户中断。
-
返回
onError
Added in: v1.10在某些全局错误时调用,例如工作进程中的未处理异常。
用法
reporter.onError(error);
参数
onExit
Added in: v1.33在测试运行器退出前立即调用。此时所有报告器都已接收到reporter.onEnd()信号,因此所有报告应该都已生成。您可以在此钩子中运行上传报告的代码。
用法
await reporter.onExit();
返回
onStdErr
Added in: v1.10当工作进程中的标准错误有内容写入时调用。
用法
reporter.onStdErr(chunk, test, result);
参数
-
输出数据块。
-
正在运行的测试。请注意,输出可能在没有测试运行时发生,在这种情况下,这将是void。
-
result
void | TestResult#测试运行的结果,这个对象会在测试运行时被填充。
onStdOut
Added in: v1.10当工作进程中有内容写入标准输出时调用。
用法
reporter.onStdOut(chunk, test, result);
参数
-
输出块。
-
正在运行的测试。请注意,输出可能在没有任何测试运行时发生,在这种情况下,这将是void。
-
result
void | TestResult#测试运行的结果,这个对象会在测试运行时被填充。
onStepBegin
Added in: v1.10当测试步骤在工作进程中开始时调用。
用法
reporter.onStepBegin(test, result, step);
参数
onStepEnd
Added in: v1.10当测试步骤在工作进程中完成时调用。
用法
reporter.onStepEnd(test, result, step);
参数
onTestBegin
Added in: v1.10在worker进程中测试启动后被调用。
用法
reporter.onTestBegin(test, result);
参数
-
已启动的测试用例。
-
result
TestResult#测试运行的结果,该对象会在测试运行时被填充。
onTestEnd
Added in: v1.10在worker进程中测试完成后调用。
用法
reporter.onTestEnd(test, result);
参数
-
已完成的测试用例。
-
result
TestResult#测试运行的结果。
printsToStdio
Added in: v1.10该报告器是否使用标准输入输出进行报告。当不使用标准输入输出时,Playwright Test可能会添加一些输出来增强用户体验。如果您的报告器不打印到终端,强烈建议返回false
。
用法
reporter.printsToStdio();
返回