Skip to main content

Reporter

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

你可以通过实现一个包含部分报告器方法的类来创建自定义报告器。请确保将此类作为默认导出。

// 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;

现在使用 testConfig.reporter 配置此报告器。了解更多关于 使用报告器 的信息。

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

const config: PlaywrightTestConfig = {
reporter: './my-awesome-reporter.ts',
};
export default config;

以下是报告器调用的典型顺序:

此外,当工作进程中产生标准输出时(可能在测试执行期间),将调用 reporter.onStdOut(chunk, test, result)reporter.onStdErr(chunk, test, result),当测试执行之外发生错误时,将调用 reporter.onError(error)

reporter.onBegin(config, suite)

Added in: v1.10
  • config <TestConfig> 解析后的配置。#
  • suite <Suite> 包含所有项目、文件和测试用例的根套件。#
  • returns: <void>#

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

reporter.onEnd(result)

Added in: v1.10
  • result <Object> 完整测试运行的结果。#

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

    * `'passed'` - 一切按预期进行。 * `'failed'` - 有测试失败。 * `'timedout'` - 已达到 [testConfig.globalTimeout](/playwright/docs/api/class-testconfig#test-config-global-timeout)。 * `'interrupted'` - 被用户中断。
  • returns: <Promise<void>>#

在所有测试运行完毕或测试被中断后调用。请注意,此方法可能返回 Promise,Playwright Test 将等待它。

reporter.onError(error)

Added in: v1.10

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

reporter.onStdErr(chunk, test, result)

Added in: v1.10
  • chunk <string|Buffer> 输出块。#
  • test <void|TestCase> 正在运行的测试。请注意,输出可能在没有测试运行时发生,在这种情况下,这将是 void#
  • result <void|TestResult> 测试运行的结果,此对象在测试运行时填充。#
  • returns: <void>#

当工作进程中的标准错误有输出时调用。

reporter.onStdOut(chunk, test, result)

Added in: v1.10
  • chunk <string|Buffer> 输出块。#
  • test <void|TestCase> 正在运行的测试。请注意,输出可能在没有测试运行时发生,在这种情况下,这将是 void#
  • result <void|TestResult> 测试运行的结果,此对象在测试运行时填充。#
  • returns: <void>#

当工作进程中的标准输出有输出时调用。

reporter.onStepBegin(test, result, step)

Added in: v1.10
  • test <TestCase> 步骤所属的测试。#
  • result <TestResult> 测试运行的结果,此对象在测试运行时填充。#
  • step <TestStep> 已开始的测试步骤实例。#
  • returns: <void>#

当工作进程中的测试步骤开始时调用。

reporter.onStepEnd(test, result, step)

Added in: v1.10

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

reporter.onTestBegin(test, result)

Added in: v1.10
  • test <TestCase> 已开始的测试。#
  • result <TestResult> 测试运行的结果,此对象在测试运行时填充。#
  • returns: <void>#

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

reporter.onTestEnd(test, result)

Added in: v1.10

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

reporter.printsToStdio()

Added in: v1.10

此报告器是否使用 stdio 进行报告。如果不使用,Playwright Test 可能会添加一些输出以增强用户体验。