Skip to content

[Feature]: JUnit reporter option to add a file attribute to <testcase> #44099

Closed

Description

🚀 Feature Request

The JUnit reporter writes name and classname on each <testcase>, but not file. CI systems read file to find the source file of each test:

classname is relative to the config's testDir, so it cannot be used instead when testDir is not the repository root.

Proposal: two new JUnit reporter options.

Option Env var Default Description
addFileAttribute PLAYWRIGHT_JUNIT_ADD_FILE_ATTRIBUTE false Write a file attribute on each <testcase>.
fileRoot PLAYWRIGHT_JUNIT_FILE_ROOT config folder The folder that file is relative to.
  • file = path.relative(fileRoot, test.location.file), with forward slashes on every OS.
  • fileRoot resolves like outputFile / outputDir: the env var wins and resolves against the current working folder; the option resolves against the config folder.
  • With the config at the repository root, addFileAttribute: true is enough. In a monorepo with one config per package, fileRoot (or PLAYWRIGHT_JUNIT_FILE_ROOT=$CI_PROJECT_DIR for every package) gives the repository-relative path. I raised the same gap in Vitest: Add a fileTemplate option to the JUnit reporter vitest-dev/vitest#11852
  • merge-reports --reporter junit cannot pass reporter options, so merge users set the env vars, or leave out --reporter and list the reporter with its options in the merge config (-c). The default fileRoot is then the merge config's folder, or the current working folder without -c.
  • When addFileAttribute is off, the output does not change.

Prior art: the Vitest and jest-junit JUnit reporters both have an addFileAttribute option, default off.

Alternatives considered:

  1. A fixed prefix or template (like filePathPrefix, or the fileTemplate proposed for Vitest). A prefix must be set for each package. A base folder covers the same case with one value: an env var set to the repository root works for every package, at any depth.
  2. Always write file, with no option. This changes the output for every user, and the other runners keep it opt-in.

Example

Config at the repository root:

// playwright.config.ts
export default defineConfig({
  testDir: './e2e',
  reporter: [['junit', { outputFile: 'results.xml', addFileAttribute: true }]],
});

A failure in e2e/auth/login.spec.ts produces:

<testcase name="logs in" classname="auth/login.spec.ts" file="e2e/auth/login.spec.ts" time="1.2">

One config per package in a monorepo:

// packages/foo/playwright.config.ts
export default defineConfig({
  testDir: './src',
  reporter: [['junit', { outputFile: 'results.xml', addFileAttribute: true, fileRoot: '../..' }]],
});

A failure in packages/foo/src/auth/login.spec.ts produces:

<testcase name="logs in" classname="auth/login.spec.ts" file="packages/foo/src/auth/login.spec.ts" time="1.2">

Motivation

Today, users who want file paths in GitLab or CircleCI must post-process the JUnit XML to add file, and in monorepos also add the package path. With these options, Playwright's JUnit output would work with these CI tools without extra scripts.

Activity

  1. changed the title [-][Feature]:[/-] [+][Feature]: JUnit reporter: add a `file` attribute to `<testcase>`[/+] on Nov 13, 2026
  2. changed the title [-][Feature]: JUnit reporter: add a `file` attribute to `<testcase>`[/-] [+][Feature]: JUnit reporter option to add a `file` attribute to `<testcase>`[/+] on Nov 13, 2026
  3. pranav-sirnapalli commented on Nov 13, 2026

    @pranav-sirnapalli

    Hi, I'd be interested in working on this feature. I can implement the opt-in addFileAttribute and fileRoot options, including environment variable support and regression tests for relative paths, Windows compatibility, and backward compatibility.

    Would this be open for a community contribution? I'm happy to work on it if approved.

  4. microsoft-playwright-automation commented on Nov 13, 2026

    @microsoft-playwright-automation
  5. microsoft-playwright-automation commented on Nov 13, 2026

    @microsoft-playwright-automation
    Contributor

    Hi, I'm the Playwright bot and I checked the reporter and the cited CI requirements.

    The file attribute is missing in current Playwright, and neither proposed option exists. On 1.66.0-next (0ad21f1d34e40a51649a97e14da787172d68d073), both direct JUnit output and merged blob reports omit it, even with addFileAttribute: true. GitLab and CircleCI document the attribute for the uses described here.

    #40231 proposed adding file, but closed without merging. The maintainer's response identifies the same path-base and Windows separator concerns that this proposal addresses. This needs a public API decision: whether to add a configurable source-path root, with two permanent reporter options and matching environment variables. I have not introduced that API or proposed a PR.

    Acceptance check and results

    I extended the existing should render expected test locally, using:

    // playwright.config.ts
    export default {
      testDir: './e2e',
      reporter: [['junit', { addFileAttribute: true }]],
    };

    With a passing test named one in e2e/a.test.js, both direct and merged reports produce:

    <testcase name="one" classname="a.test.js" time="...">

    The test first verifies today's classname and absent file, then marks the requested assertion as expected to fail:

    expect(attributes.classname).toBe('a.test.js');
    expect(attributes.file).toBeUndefined();
    test.fail(true, 'The requested JUnit file attribute is not implemented');
    expect(attributes.file).toBe('e2e/a.test.js');

    On Linux at the commit above:

    • npm ci --no-audit --no-fund and npm run build succeeded.
    • The unmodified should render expected checks passed for direct and merged reports.
    • The extended checks reached the expected failure for both report modes.

    Command: npm run ttest -- reporter-junit --grep 'should render expected' --workers=1. These checks use no browser. Windows path normalization and fileRoot behavior were not runtime-tested because those options are not implemented.

    The source, public types and reporter documentation contain no existing equivalent. Vitest and jest-junit both document an opt-in addFileAttribute; Vitest's linked PR, vitest-dev/vitest#5659, merged. This confirms prior art, not an existing Playwright solution.

    Triaged on Linux by the Playwright bot - agent run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions