Reporter
测试运行器在测试执行期间通知报告器各种事件。报告器的所有方法都是可选的。
你可以通过实现一个包含部分报告器方法的类来创建自定义报告器。请确保将此类作为默认导出。
- TypeScript
- JavaScript
// my-awesome-reporter.ts
import { Reporter } from '@playwright/test/reporter';
class MyReporter implements Reporter {
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}`);
}
}
export default MyReporter;
// my-awesome-reporter.js
// @ts-check
/** @implements {import('@playwright/test/reporter').Reporter} */
class MyReporter {
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 配置此报告器。了解更多关于 使用报告器 的信息。
- TypeScript
- JavaScript
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
reporter: './my-awesome-reporter.ts',
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
reporter: './my-awesome-reporter.js',
};
module.exports = config;
以下是报告器调用的典型顺序:
- reporter.onBegin(config, suite) 在包含所有其他套件和测试的根套件中被调用一次。了解更多关于 套件层次结构 的信息。
- reporter.onTestBegin(test, result) 为每个测试运行调用。它接收被执行的 TestCase 和一个几乎为空的 TestResult。测试结果将在测试运行时填充(例如,使用步骤和 stdio),并在测试完成后获得最终的
status。 - reporter.onStepBegin(test, result, step) 和 reporter.onStepEnd(test, result, step) 为测试内执行的每个步骤调用。当步骤执行时,测试运行尚未完成。
- reporter.onTestEnd(test, result) 在测试运行完成时调用。此时,TestResult 已完成,你可以使用 testResult.status、testResult.error 等。
- reporter.onEnd(result) 在所有应该运行的测试完成后被调用一次。
此外,当工作进程中产生标准输出时(可能在测试执行期间),将调用 reporter.onStdOut(chunk, test, result) 和 reporter.onStdErr(chunk, test, result),当测试执行之外发生错误时,将调用 reporter.onError(error)。
- reporter.onBegin(config, suite)
- reporter.onEnd(result)
- reporter.onError(error)
- reporter.onStdErr(chunk, test, result)
- reporter.onStdOut(chunk, test, result)
- reporter.onStepBegin(test, result, step)
- reporter.onStepEnd(test, result, step)
- reporter.onTestBegin(test, result)
- reporter.onTestEnd(test, result)
- reporter.printsToStdio()
reporter.onBegin(config, suite)
Added in: v1.10在运行测试之前调用一次。所有测试都已被发现并放入 Suite 层次结构中。
reporter.onEnd(result)
Added in: v1.10status<"passed"|"failed"|"timedout"|"interrupted">
* `'passed'` - 一切按预期进行。 * `'failed'` - 有测试失败。 * `'timedout'` - 已达到 [testConfig.globalTimeout](/playwright/docs/api/class-testconfig#test-config-global-timeout)。 * `'interrupted'` - 被用户中断。
在所有测试运行完毕或测试被中断后调用。请注意,此方法可能返回 Promise,Playwright Test 将等待它。
reporter.onError(error)
Added in: v1.10在发生某些全局错误时调用,例如工作进程中未处理的异常。
reporter.onStdErr(chunk, test, result)
Added in: v1.10chunk<string|Buffer> 输出块。#test<void|TestCase> 正在运行的测试。请注意,输出可能在没有测试运行时发生,在这种情况下,这将是 void。#result<void|TestResult> 测试运行的结果,此对象在测试运行时填充。#- returns: <void>#
当工作进程中的标准错误有输出时调用。
reporter.onStdOut(chunk, test, result)
Added in: v1.10chunk<string|Buffer> 输出块。#test<void|TestCase> 正在运行的测试。请注意,输出可能在没有测试运行时发生,在这种情况下,这将是 void。#result<void|TestResult> 测试运行的结果,此对象在测试运行时填充。#- returns: <void>#
当工作进程中的标准输出有输出时调用。
reporter.onStepBegin(test, result, step)
Added in: v1.10test<TestCase> 步骤所属的测试。#result<TestResult> 测试运行的结果,此对象在测试运行时填充。#step<TestStep> 已开始的测试步骤实例。#- returns: <void>#
当工作进程中的测试步骤开始时调用。
reporter.onStepEnd(test, result, step)
Added in: v1.10当工作进程中的测试步骤完成时调用。
reporter.onTestBegin(test, result)
Added in: v1.10在工作进程中开始测试后调用。
reporter.onTestEnd(test, result)
Added in: v1.10在工作进程中完成测试后调用。
reporter.printsToStdio()
Added in: v1.10此报告器是否使用 stdio 进行报告。如果不使用,Playwright Test 可能会添加一些输出以增强用户体验。