Course outline

Jenkins / Evidence, reuse, and authority / Lesson 14

Publish test evidence when a command fails

Capture a test command status, publish a synthetic JUnit report, and preserve a failing build result.

About 4 min readContent reviewed
On this page

Lesson goals

  • Separate command exit status from report publication.
  • Publish a report before deliberately failing the stage.
  • Verify missing reports and result policy in an isolated job.

Define the test evidence contract

A test command can fail before a Pipeline publishes its report, leaving reviewers without useful evidence. Decide which report the command is expected to create, where it is written, and what should happen when it is missing or malformed. The examples use a synthetic XML report and a Linux-labelled agent; they require Jenkins Pipeline and JUnit support and do not run a real test suite. Never present the synthetic report as evidence about an application.

Capture status before reporting

The first Jenkinsfile uses returnStatus so the shell step returns a nonzero status rather than immediately throwing for that command. It writes a controlled failing report, publishes it with junit, and then explicitly fails the stage. This ordering demonstrates how report publication and the command outcome can both remain visible. A real test command should produce its own report, and an infrastructure failure may prevent report creation entirely.

groovy Publish evidence then fail
pipeline {
  agent { label "linux" }
  stages {
    stage("Synthetic test") {
      steps {
        script {
          int status = sh(script: "exit 1", returnStatus: true)
          writeFile file: "report.xml", text: '<testsuite tests="1" failures="1"><testcase name="sample"><failure message="synthetic"/></testcase></testsuite>'
          junit "report.xml"
          if (status != 0) { error "Test command failed" }
        }
      }
    }
  }
}

returnStatus preserves the command status while the synthetic report is published before the explicit error.

Compare the success path

The second independent Jenkinsfile writes a synthetic passing report and publishes it. It demonstrates the report path and structure without a shell test command. Run the two files as separate test jobs or separate revisions; a Jenkinsfile contains one pipeline block. JUnit publication may influence the build result according to installed plugin behavior and configuration, so inspect the actual final result instead of relying solely on console text.

groovy Publish a synthetic success report
pipeline {
  agent { label "linux" }
  stages {
    stage("Synthetic success") {
      steps {
        writeFile file: "report.xml", text: '<testsuite tests="1" failures="0"><testcase name="sample"/></testsuite>'
        junit "report.xml"
      }
    }
  }
}

The report is a controlled fixture, not a claim that a real application was tested.

Handle missing and partial reports

A missing report is not proof that tests passed. Define whether an absent report fails the run, and distinguish no tests selected from a command that failed before producing output. Avoid publishing a stale report left in a reused workspace: use an owned workspace strategy and ensure the report belongs to the current revision and run. Preserve the original command status and report-publication errors in useful diagnostics.

Verify in an isolated job

On a suitable owned agent, run the success and controlled-failure examples and inspect the final build result and test report. Then test a missing report and malformed XML under the intended plugin configuration. Confirm that a failed command cannot produce a successful release decision, that the current run’s report is used, and that no external notification or deployment occurs during the fixture. Record the Jenkins and plugin environment used.

Common errors and fixes

A failed command prevents any report from being published

Why it happens
The shell step threw before the report step ran.
How to fix it
Capture or handle status, publish available evidence, then apply the result policy.
Prevent it next time
Test a controlled nonzero exit with a report present.

A stale report makes a run appear tested

Why it happens
A reused workspace contains output from a previous run.
How to fix it
Use an owned clean workspace strategy and identify the current report.
Prevent it next time
Test missing output after a fresh checkout or isolated workspace.

A missing report is treated as passing tests

Why it happens
Absence was confused with a zero-failure report.
How to fix it
Define and enforce a missing-report policy.
Prevent it next time
Test missing and malformed reports in the installed plugin environment.

2 practice exercises

1. Why does the first example call error after junit?

Show worked solution

It demonstrates publishing the synthetic report before marking the stage failed for the nonzero command status. Both the test evidence and the failure remain available to review.

2. Does the second report prove an application passed tests?

Show worked solution

No. It is hard-coded synthetic XML. Real evidence must come from the current test run and be tied to its revision, environment, and result.

Key takeaways

  • Keep command status and report publication visible.
  • Treat missing or stale reports as explicit result-policy cases.

Official references