JUnit reports for API tests, explained

JUnit XML started as the report format of a Java test framework. Today it's the closest thing CI has to a universal test results format: GitHub test-report actions, GitLab, Jenkins, Azure DevOps, CircleCI and Bitbucket Pipelines all read it. If your API tests can write JUnit, every CI system can show them properly, with pass and fail counts, trends and links to the failing test.

What the format looks like

A JUnit file is a list of test suites, each with test cases. A failed case has a <failure> with a message; a case that couldn't run has an <error>:

XML
<testsuites>
  <testsuite name="Orders API/Create order" tests="2" failures="1" errors="0" time="0.087">
    <testcase name="status is 201" time="0.087"/>
    <testcase name="returns an id" time="0.087">
      <failure message="expected undefined to be a string"/>
    </testcase>
  </testsuite>
</testsuites>

How API tests map onto it

There's no single standard for mapping HTTP requests to JUnit, so it's worth knowing what a tool does. The Requestarium CLI uses this mapping:

Your collectionJUnit
Each request runA <testsuite>
Each pm.test in that request's scriptsA <testcase>
A request with no testsOne test case that checks the status code is below 400
A request that couldn't be sent (DNS, timeout, TLS)An <error>

That last two rules matter. A request with no tests still counts, so a 500 never slips through silently. And "couldn't connect" is reported as an error, not a failure, so you can tell an outage from a regression.

One command, three reports

Terminal
requestarium run-collection "Orders API" --env Staging \
  --junit results.xml --html report.html --out report.json
  • --junit for the CI test view.
  • --html for humans: a self-contained file with totals, then each request with its tests, headers and response. Failed requests start expanded. Upload it as a build artifact.
  • --out for machines: the full result as JSON, with a versioned schema (schemaVersion: 1), if you want to post-process results or feed a dashboard.

The terminal still prints the summary while all three files are written. Secret values are masked in every report.

A collection run in the app with JSON, JUnit and HTML download buttons
Runs started in the app download the same reports: JSON, JUnit and HTML

Wiring it into CI

Each CI system has its own way to pick up the file:

CIHow
GitHub ActionsA test-report action such as dorny/test-reporter, plus the job summary the CLI writes on its own
GitLab CIartifacts: reports: junit: results.xml
Jenkinsjunit 'results.xml' in a post { always { … } } block
Azure PipelinesPublishTestResults@2 with testResultsFormat: JUnit
CircleCIstore_test_results with the folder that holds the file
Bitbucket PipelinesWrite the file to test-results/, which is picked up automatically

Full examples per platform: GitHub Actions, GitLab CI, Jenkins, Azure Pipelines, CircleCI, Bitbucket Pipelines.

Write tests with good names

The test name is what shows up in your CI, so make it say what broke:

JavaScript
pm.test('Create order returns 201', () => {
  pm.expect(pm.response.code).to.equal(201);
});
pm.test('Create order returns the new order id', () => {
  pm.expect(pm.response.json().id).to.be.a('string');
});

"Create order returns the new order id" in a red row tells the reviewer what happened without opening a log. "test 2" doesn't.

Beyond a single run

A JUnit file describes one run. To see how a collection does over time, across CI and developers' laptops, add --save-run. The run goes into the collection's history in Requestarium with its branch, commit and CI link, and requestarium runs lists them. See CLI reference.