How to Publish JUnit Test Results in a Jenkins Pipeline
The junit step parses JUnit XML into the Jenkins Tests tab and trend graph - run it in post { always }.
Have tests emit JUnit XML, then call the junit step to publish it. Put it in a post { always } block so the report appears even when the test step fails.
Publish results in post-always
The junit step runs regardless of the test outcome, so failed tests still show in the UI.
Jenkinsfile
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'npm ci && npm test -- --reporters=jest-junit'
}
}
}
post {
always {
junit 'reports/junit.xml'
}
}
}Gotchas
- Call
junitinpost { always }- if you put it only after the test step, a test failure skips publishing. - The
junitstep (JUnit plugin) marks the build unstable on failed tests, distinct from a hardfailure. - Point the glob at the actual report path; an empty match makes the step error unless you set
allowEmptyResults: true.
Related guides
How to Publish Test Results to the UI in Azure PipelinesPublish test results to the Azure Pipelines Tests tab with PublishTestResults@2, using JUnit format and condi…
How to Run Steps on Success or Failure in a Jenkins PipelineRun steps conditionally on outcome in a Jenkins pipeline with the post section - success, failure, unstable,…