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.
- 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。了解更多关于 使用报告器 的信息。
🌐 Now use this reporter with testConfig.reporter. Learn more about using reporters.
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.onBegin() 会被调用一次,传入包含所有其他套件和测试的根套件。了解更多关于 套件层级 的信息。
- 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()。
🌐 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.
报告器错误
Playwright 会吞掉你自定义报告器方法中抛出的任何错误。如果你需要检测或在报告器出错时使测试失败,你必须自行封装并处理这些错误。
🌐 Playwright will swallow any errors thrown in your custom reporter methods. If you need to detect or fail on reporter errors, you must wrap and handle them yourself.
合并报告 API 说明
在通过 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 对象保留。例如,如果项目“Desktop Chrome”被分片到 5 台机器上,那么在传递给 reporter.onBegin() 的配置中,将会有 5 个具有相同名称的项目实例。
方法
🌐 Methods
onBegin
Added in: v1.10在运行测试之前调用一次。所有测试已经被发现并放入 Suite 层次结构中。
🌐 Called once before running tests. All tests have been already discovered and put into a hierarchy of Suites.
用法
reporter.onBegin(config, suite);
参数
-
configFullConfig#已解决的配置。
-
包含所有项目、文件和测试用例的根套件。
onEnd
Added in: v1.10在所有测试运行完毕或测试被中断后调用。请注意,此方法可能返回一个 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.
用法
await reporter.onEnd(result);
参数
-
-
status“通过” | “未通过” | “超时” | “中断”试运行状态。
-
startTimeDate试运行开始挂墙时间。
-
durationnumber测试运行持续时间(以毫秒为单位)。
完整测试运行的结果,
status可以是以下之一:'passed'- 一切都如预期般进行。'failed'- 任何测试都失败了。'timedout'- 已达到 testConfig.globalTimeout。'interrupted'- 被用户中断。
-
返回
onError
Added in: v1.10在某些全局错误上调用,例如工作进程中未处理的异常。
🌐 Called on some global error, for example unhandled exception in the worker process.
用法
reporter.onError(error);
参数
onExit
Added in: v1.33在测试运行器退出之前立即调用。此时,所有报告器都已收到 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.
用法
await reporter.onExit();
返回
onStdErr
Added in: v1.10当某些内容写入工作进程中的标准错误时调用。
🌐 Called when something has been written to the standard error in the worker process.
用法
reporter.onStdErr(chunk, test, result);
参数
-
输出块。
-
正在运行的测试。请注意,即使没有测试在运行,也可能会有输出,在这种情况下输出将为 void。
-
resultvoid | TestResult#测试运行的结果,该对象在测试运行时被填充。
onStdOut
Added in: v1.10当某些内容写入工作进程中的标准输出时调用。
🌐 Called when something has been written to the standard output in the worker process.
用法
reporter.onStdOut(chunk, test, result);
参数
-
输出块。
-
正在运行的测试。请注意,即使没有测试在运行,也可能会有输出,在这种情况下输出将为 void。
-
resultvoid | TestResult#测试运行的结果,该对象在测试运行时被填充。
onStepBegin
Added in: v1.10当测试步骤在工作进程中启动时调用。
🌐 Called when a test step started in the worker process.
用法
reporter.onStepBegin(test, result, step);
参数
onStepEnd
Added in: v1.10当工作进程中的测试步骤完成时调用。
🌐 Called when a test step finished in the worker process.
用法
reporter.onStepEnd(test, result, step);
参数
onTestBegin
Added in: v1.10在工作进程中开始测试后调用。
🌐 Called after a test has been started in the worker process.
用法
reporter.onTestBegin(test, result);
参数
-
测试已经开始。
-
resultTestResult#测试运行的结果,该对象在测试运行时被填充。
onTestEnd
Added in: v1.10在工作进程中完成测试后调用。
🌐 Called after a test has been finished in the worker process.
用法
reporter.onTestEnd(test, result);
参数
-
测试已经完成。
-
resultTestResult#测试运行的结果。
printsToStdio
Added in: v1.10无论这个报告器是否使用 stdio 进行报告。当它不使用时,Playwright 测试可以添加一些输出以增强用户体验。如果你的报告器没有打印到终端,强烈建议返回 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.
用法
reporter.printsToStdio();
返回