How to Publish Test Results in Jenkins: Step-by-Step Guide
To publish test results in Jenkins, use the
Publish JUnit test result report post-build action or the junit step in a pipeline. This collects test report files (usually XML) and displays results in the Jenkins UI for easy tracking.Syntax
In a Jenkins freestyle job, you add the Publish JUnit test result report post-build action and specify the path to your test result XML files.
In a Jenkins pipeline, use the junit step with the path to test result files.
junit 'path/to/results/*.xml'- collects and publishes test results.
groovy
post {
always {
junit 'target/surefire-reports/*.xml'
}
}Example
This example shows a simple Jenkins pipeline that runs tests and publishes the results using the junit step.
groovy
pipeline {
agent any
stages {
stage('Test') {
steps {
// Run tests here, e.g., using Maven
sh 'mvn test'
}
}
}
post {
always {
junit 'target/surefire-reports/*.xml'
}
}
}Output
Started by user
[Pipeline] Start of Pipeline
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
+ mvn test
... (test output) ...
[Pipeline] }
[Pipeline] // stage
[Pipeline] post
[Pipeline] {
[Pipeline] junit
Recording test results
[Pipeline] }
[Pipeline] End of Pipeline
Finished: SUCCESS
Common Pitfalls
- Incorrect path to test result XML files causes Jenkins to find no results.
- Not generating test reports in XML format compatible with Jenkins.
- Forgetting to add the
junitstep or post-build action. - Using wildcard patterns that do not match any files.
Always verify your test tool outputs XML reports and the path matches what Jenkins expects.
groovy
post {
always {
// Wrong path example - no files found
junit 'reports/*.xml'
// Correct path example
junit 'target/surefire-reports/*.xml'
}
}Quick Reference
Summary tips for publishing test results in Jenkins:
- Use
Publish JUnit test result reportin freestyle jobs. - Use
junit 'path/*.xml'in pipelines. - Ensure test reports are in JUnit XML format.
- Verify file paths and wildcards carefully.
- Check Jenkins console output for errors locating test files.
Key Takeaways
Use the Jenkins JUnit plugin to publish test results from XML files.
In pipelines, add the junit step with the correct path to test reports.
Ensure your test tool generates JUnit-compatible XML reports.
Double-check file paths and wildcards to avoid missing results.
Review Jenkins console logs if test results do not appear.