JUnit Attachments Support for Publish Test Results

David Paquette

We’ve recently made some improvements to the Publish Test Results task in Azure Pipelines. This task now supports file attachments when publishing test results from a JUnit report.

JUnit Attachments Report Format

The JUnit XML report format doesn’t officially have support for file attachments but there is a common convention of including attachments in the element of each test case. Attachments are specified in the format [[ATTACHMENT|/path/to/file.ext]]. For example:

<testcase name="my test case" classname="example.spec.ts" time="6.473">
  <system-out>
<![CDATA[
[[ATTACHMENT|test-results/example-has-title-chromium/test-failed-1.png]]
[[ATTACHMENT|test-results/example-has-title-chromium/trace.zip]]
]]>
  </system-out>
</testcase>

The Publish Test Results task will automatically look for any attachments listed in the element, upload those attachments to Azure DevOps, and associate them with the test results. Attachments paths can be either absolute or relative to the location of the JUnit report file. 

Publishing Playwright JUnit Test Results

If you’re using >v1.3.9 of Playwright, screenshots, recordings and trace files can automatically be associated directly with Test Results in Azure Pipelines.

In playwright.config.ts, configure the JUnit reporter and configure Playwright to save screenshots and trace files whenever a test fails:

reporter: [['list'], ['junit', {outputFile: 'results.xml'}]],
  use: {
    /* Save screenshots on failure <em>/
    screenshot: 'only-on-failure',
    /</em> Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
    trace: 'retain-on-failure',
  },

In Azure Pipelines, use the PublishTestResults@2 task to publish the JUnit report file output from Playwright.

steps:
- script: |
    npm install
  displayName: 'npm install'

- script: |
    npx playwright install
  displayName: 'Playwright install'

- script: |
    npx playwright test
  displayName: 'Run Playwright tests'

- task: PublishTestResults@2
  inputs:
    testResultsFormat: 'JUnit'
    testResultsFiles: 'results.xml'
  condition: always()
   

To review attachments, navigate to the test results for your pipeline run, click on a failed test, then click on the Attachments tab. You can preview/download images and trace files for further investigation.

Screenshot showing Playwright screenshot and trace file attached to test case results in Azure Pipelines

0 comments

Leave a comment

Feedback usabilla icon