Test Reporters
Relevant source files
The following files were used as context for generating this wiki page:
- CONTRIBUTING.md
- packages/playwright/src/reporters/markdown.ts
- packages/playwright/types/testReporter.d.ts
- tests/playwright-test/reporter-blob.spec.ts
- tests/playwright-test/reporter-html.spec.ts
- tests/playwright-test/reporter-junit.spec.ts
- tests/playwright-test/reporter-markdown.spec.ts
- tests/playwright-test/reporter.spec.ts
- utils/generate_types/overrides-test.d.ts
Test reporters in Playwright are responsible for processing and presenting the results of a test run. They provide a flexible mechanism to generate reports in various formats, such as HTML, JSON, JUnit XML, and Markdown, or to implement custom reporting logic. Reporters are notified of events during the test execution lifecycle, allowing for real-time feedback and detailed post-run analysis.
The system is designed to be extensible, allowing users to create their own reporters by implementing the Reporter interface. A key feature is the blob reporter, which facilitates distributed testing by creating intermediate reports from sharded test runs that can be merged into a single, comprehensive report. This is particularly useful for large-scale CI/CD pipelines.
Core Concepts
The foundation of the reporting system is the Reporter interface and the data structures it interacts with.
Reporter Interface and Lifecycle
A reporter is a class that implements the Reporter interface. It listens to events emitted by the test runner and acts upon them. All methods are optional.
The following diagram illustrates the typical sequence of events a reporter receives during a test run.
This diagram shows the lifecycle of a reporter, from the start of the test run to its completion.
Sources: packages/playwright/types/testReporter.d.ts:140-230
The key methods of the Reporter interface are:
| Method | Description |
|---|---|
onBegin(config, suite) | Called once before running tests. Provides the full configuration and the root suite of all tests. |
onTestBegin(test, result) | Called when a test case is about to be run. |
onStepBegin(test, result, step) | Called when a test step starts. |
onStepEnd(test, result, step) | Called when a test step finishes. |
onTestEnd(test, result) | Called when a test case has finished. The result object is fully populated. |
onStdOut(chunk, test, result) | Called when stdout is written in the worker process. |
onStdErr(chunk, test, result) | Called when stderr is written in the worker process. |
onError(error) | Called on a global error, like an unhandled exception in a worker. |
onEnd(result) | Called once after all tests have been run. |
onExit() | Called immediately before the test runner process exits. Useful for asynchronous cleanup. |
printsToStdio() | Indicates whether the reporter prints to stdio. If false, Playwright may add a default terminal reporter. |
Sources: packages/playwright/types/testReporter.d.ts:140-230, tests/playwright-test/reporter.spec.ts:32-111
Key Data Structures
Reporters receive detailed information through a set of data structures.
This diagram shows the relationship between the Reporter interface and the main data structures it uses.
Sources: packages/playwright/types/testReporter.d.ts
- Suite: A group of tests, which can contain other suites and test cases, forming a hierarchy (root -> project -> file -> describe).
- TestCase: Represents a single test defined with
test(...). It includes metadata like title, location, and all of its execution results. - TestResult: The result of a single run of a
TestCase. A test case can have multiple results due to retries. It contains the status, duration, errors, attachments, and steps. - TestStep: A step within a test run, created via
test.step(). Steps can be nested and contain their own errors and duration. - FullResult: The overall result of the entire test run.
Sources: packages/playwright/types/testReporter.d.ts
Configuration
Reporters are configured in playwright.config.ts via the reporter property. You can specify one or more reporters, and provide options for each.
// playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
// A single reporter
reporter: 'list',
// Multiple reporters
reporter: [
['list'],
['json', { outputFile: 'results.json' }],
['html', { open: 'never' }],
['./my-custom-reporter.ts', { customOption: 'value' }]
],
});
Sources: utils/generate_types/overrides-test.d.ts:20-28, tests/playwright-test/reporter.spec.ts:121-125
Built-in Reporters
Playwright includes several built-in reporters for common use cases.
HTML Reporter
The HTML reporter generates a self-contained web page that provides a rich, interactive view of the test results.
Features:
- Filtering: Results can be filtered by status (passed, failed, flaky, skipped), by test title, file path, or by tags (
@tag). - Detailed View: Clicking on a test reveals detailed information, including steps, errors with code snippets,
stdout/stderr, and attachments. - Trace Viewer: If tracing is enabled, the report includes an embedded trace viewer for debugging test execution step-by-step.
- Image & Video: Screenshots, videos, and image diffs are displayed inline.
- Metadata: Can display Git commit information and other custom metadata.
Configuration Options:
| Option | Type | Description |
|---|---|---|
outputFolder | string | Folder to write the report to. Defaults to playwright-report. |
open | 'always' | 'never' | 'on-failure' | When to open the report automatically. |
host | string | Host to serve the report on. Defaults to localhost. |
port | number | Port to serve the report on. Defaults to 9323. |
attachmentsBaseURL | string | Base URL for attachments, useful for hosting them on a separate server. |
title | string | Custom title for the HTML report. |
noSnippets | boolean | Disables the display of code snippets for test steps. |
Sources: utils/generate_types/overrides-test.d.ts:18, tests/playwright-test/reporter-html.spec.ts:46-51, 1373-1379
Blob Reporter and Merging Reports
The blob reporter is designed for sharded or distributed test execution. Each shard run produces a .zip file containing the test results for that shard. These individual blob reports can then be aggregated into a single report using the playwright merge-reports CLI command.
This process is essential for getting a unified view of a test run that was parallelized across multiple machines.
The following diagram shows the workflow for merging blob reports.
This flowchart illustrates how individual blob reports from sharded test runs are collected and then merged to produce final reports in various formats.
Sources: tests/playwright-test/reporter-blob.spec.ts:107-124, 219-232
Configuration Options:
| Option | Type | Description |
|---|---|---|
outputDir | string | Directory to save blob report files. Defaults to blob-report. Can be overridden by PLAYWRIGHT_BLOB_OUTPUT_DIR. |
fileName | string | Name of the blob report file. Can include placeholders. |
outputFile | string | A specific path for the output file, which takes precedence over outputDir and fileName. Can be overridden by PLAYWRIGHT_BLOB_OUTPUT_FILE. |
Sources: utils/generate_types/overrides-test.d.ts:14, tests/playwright-test/reporter-blob.spec.ts:1688-1725
Markdown Reporter
The markdown reporter generates a concise summary of the test run in a report.md file. It's useful for quick overviews, such as in GitHub Actions summaries or pull request comments.
The report includes:
- A list of failed tests.
- A collapsible section for flaky tests.
- A summary line with counts of passed, skipped, and not-run tests.
Configuration Options:
| Option | Type | Description |
|---|---|---|
outputFile | string | Path to the output Markdown file. Defaults to report.md. |
Sources: packages/playwright/src/reporters/markdown.ts:21, tests/playwright-test/reporter-markdown.spec.ts:70-76
JUnit Reporter
The junit reporter produces an XML file in the JUnit format, which is widely supported by CI/CD systems (like Jenkins, Azure DevOps, etc.) for displaying test results.
Features:
- Outputs test results in a standard XML format.
- Can include
stdoutandstderrfor each test case. - Can embed test annotations as custom properties in the XML.
- Supports including the project name in the test case name for clarity in multi-project runs.
Configuration Options:
| Option | Type | Description |
|---|---|---|
outputFile | string | Path to the output XML file. Defaults to results.xml. Can be overridden by PLAYWRIGHT_JUNIT_OUTPUT_NAME. |
stripANSIControlSequences | boolean | Whether to remove ANSI color codes from stdout and stderr. |
includeProjectInTestName | boolean | Prepends the project name to the test name in the report. |
Sources: utils/generate_types/overrides-test.d.ts:16, tests/playwright-test/reporter-junit.spec.ts:200-211
Custom Reporters
You can create a custom reporter by defining a class that implements the Reporter interface and exporting it. This allows for full control over how test results are processed and presented.
A simple custom reporter might look like this:
// small-reporter.js
const path = require('path');
class Reporter {
onBegin(config, suite) {
console.log(`Starting run with ${suite.allTests().length} tests.`);
}
onTestEnd(test, result) {
const titlePath = test.titlePath().slice(1).join(' > ');
console.log(`Finished test ${titlePath}: ${result.status}`);
}
onEnd(result) {
console.log(`Run finished with status: ${result.status}`);
}
}
module.exports = Reporter;
Sources: tests/playwright-test/reporter.spec.ts:11-53
This reporter can then be used in the configuration file:
// playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: './small-reporter.js',
});