0
0
Jenkinsdevops~5 mins

JUnit test report publishing in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run tests in Jenkins, you want to see the results clearly. Publishing JUnit test reports helps Jenkins show which tests passed or failed, so you can fix problems quickly.
When you want Jenkins to show test results after running Java tests.
When you need to track test failures over time in your build pipeline.
When you want to fail a build automatically if tests do not pass.
When you want to generate a visual report of test results in Jenkins.
When you want to share test results with your team through Jenkins.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building the project'
        // Add build commands here
      }
    }
    stage('Test') {
      steps {
        echo 'Running tests'
        // Run your tests here, for example:
        sh 'mvn test'
      }
      post {
        always {
          junit 'target/surefire-reports/*.xml'
        }
      }
    }
  }
}

This Jenkinsfile defines a pipeline with two stages: Build and Test.

In the Test stage, it runs tests using Maven and then publishes JUnit test reports from the 'target/surefire-reports' directory.

The junit step tells Jenkins where to find the XML test report files to display results.

Commands
Runs the Java tests using Maven. This generates JUnit XML reports in the default directory.
Terminal
mvn test
Expected OutputExpected
[INFO] Scanning for projects... [INFO] [INFO] ------------------< com.example:my-app >------------------- [INFO] Building my-app 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ my-app --- [INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.345 s - in com.example.AppTest [INFO] [INFO] BUILD SUCCESS
Triggers the Jenkins pipeline job that includes the JUnit report publishing step.
Terminal
jenkins-jobs build my-pipeline
Expected OutputExpected
Started build #15 for job 'my-pipeline' Building remotely on agent-1 [Pipeline] stage [Pipeline] { (Test) [Pipeline] sh + mvn test [INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0 [Pipeline] junit Recording test results [Test Result] Test Result: 5 tests, 0 failures, 0 skipped [Pipeline] }
Fetches the JSON summary of the test report from Jenkins for build 15 to verify test results programmatically.
Terminal
curl -s http://jenkins.example.com/job/my-pipeline/15/testReport/api/json
Expected OutputExpected
{"totalCount":5,"failCount":0,"skipCount":0,"suites":[{"name":"com.example.AppTest","cases":[{"name":"testApp","status":"PASSED"}]}]}
Key Concept

If you remember nothing else from this pattern, remember: the Jenkins 'junit' step reads XML test reports to show test results and control build status.

Common Mistakes
Not specifying the correct path to the JUnit XML report files in the 'junit' step.
Jenkins cannot find the test reports, so it shows no test results or fails silently.
Use the exact path pattern where your test reports are generated, for example 'target/surefire-reports/*.xml'.
Running tests but forgetting to add the 'junit' step in the Jenkinsfile.
Test results are not published, so Jenkins does not show test status or fail the build on test failures.
Always add the 'junit' step in the post section of your test stage to publish reports.
Using an outdated or incompatible test report format.
Jenkins cannot parse the reports, so test results are missing or incorrect.
Ensure your tests generate standard JUnit XML reports compatible with Jenkins.
Summary
Run your tests to generate JUnit XML reports using your build tool like Maven.
Add the 'junit' step in your Jenkins pipeline to publish these test reports.
Verify test results in Jenkins UI or via Jenkins API to track test success or failure.