Bird
Raised Fist0
Jenkinsdevops~5 mins

JUnit test report publishing in Jenkins - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of publishing JUnit test reports in Jenkins?
easy
A. To run tests faster on Jenkins agents
B. To display test results clearly in Jenkins for easy review
C. To compile Java code before testing
D. To deploy the application automatically after tests

Solution

  1. Step 1: Understand JUnit report publishing

    Publishing JUnit reports in Jenkins helps show test results in a readable format.
  2. Step 2: Identify the main goal

    The main goal is to review test outcomes easily, not to run tests or deploy code.
  3. Final Answer:

    To display test results clearly in Jenkins for easy review -> Option B
  4. Quick Check:

    JUnit reports show test results [OK]
Hint: JUnit reports = test results display in Jenkins [OK]
Common Mistakes:
  • Confusing report publishing with running tests
  • Thinking it compiles code
  • Assuming it triggers deployment
2. Which Jenkins pipeline syntax correctly publishes JUnit test reports stored in target/surefire-reports/*.xml?
easy
A. post { always { junit 'target/surefire-reports/*.xml' } }
B. steps { junit 'target/surefire-reports/*.xml' }
C. post { junit 'target/surefire-reports/*.xml' }
D. publishJUnit 'target/surefire-reports/*.xml'

Solution

  1. Step 1: Identify correct placement of junit step

    The junit step must be inside a post block, usually with always to run after build.
  2. Step 2: Check syntax correctness

    post { always { junit 'target/surefire-reports/*.xml' } } uses post { always { junit 'path' } } which is correct syntax for publishing reports.
  3. Final Answer:

    post { always { junit 'target/surefire-reports/*.xml' } } -> Option A
  4. Quick Check:

    junit inside post always block [OK]
Hint: Put junit step inside post { always { ... } } [OK]
Common Mistakes:
  • Placing junit directly under steps without post
  • Using wrong step name like publishJUnit
  • Omitting the always block inside post
3. Given this Jenkins pipeline snippet, what will happen after the build if the test reports are saved in build/test-results/*.xml?
post {
  always {
    junit 'build/test-results/*.xml'
  }
}
medium
A. JUnit reports will be ignored because path is incorrect
B. Tests will rerun automatically after the build
C. Build will fail if no test reports are found
D. JUnit test results will be published and visible in Jenkins UI

Solution

  1. Step 1: Understand junit step behavior

    The junit step publishes test results from the given XML path after the build.
  2. Step 2: Check the path and effect

    The path build/test-results/*.xml matches where reports are saved, so results will be published and shown.
  3. Final Answer:

    JUnit test results will be published and visible in Jenkins UI -> Option D
  4. Quick Check:

    Correct path + junit step = results published [OK]
Hint: Correct path in junit step shows results in Jenkins [OK]
Common Mistakes:
  • Thinking tests rerun automatically
  • Assuming build fails if reports missing
  • Believing reports are ignored without error
4. You added this to your Jenkinsfile but test reports do not appear in Jenkins UI:
post {
  always {
    junit 'reports/*.xml'
  }
}
What is the most likely cause?
medium
A. The junit step must be inside the steps block, not post
B. The junit step requires a full absolute path
C. The report files are not saved in the 'reports' directory
D. The pipeline must use 'archiveArtifacts' to publish reports

Solution

  1. Step 1: Verify report file location

    If reports are not saved in 'reports/*.xml', junit cannot find them to publish.
  2. Step 2: Understand junit step requirements

    junit works with relative paths and does not require absolute paths or archiveArtifacts for publishing test results.
  3. Final Answer:

    The report files are not saved in the 'reports' directory -> Option C
  4. Quick Check:

    Wrong report path = no results shown [OK]
Hint: Check report file path matches junit pattern [OK]
Common Mistakes:
  • Placing junit outside post block
  • Using absolute paths unnecessarily
  • Confusing junit with archiveArtifacts
5. You want Jenkins to publish JUnit reports only if tests run successfully, but still archive reports even if tests fail. Which Jenkinsfile snippet achieves this?
hard
A. post { success { junit 'test-results/*.xml' } always { archiveArtifacts 'test-results/*.xml' } }
B. post { always { junit 'test-results/*.xml' archiveArtifacts 'test-results/*.xml' } }
C. post { failure { junit 'test-results/*.xml' } success { archiveArtifacts 'test-results/*.xml' } }
D. post { success { archiveArtifacts 'test-results/*.xml' } always { junit 'test-results/*.xml' } }

Solution

  1. Step 1: Understand conditions for junit and archiveArtifacts

    To publish JUnit reports only on success, place junit inside success block. To archive always, place archiveArtifacts inside always.
  2. Step 2: Match snippet to requirements

    post { success { junit 'test-results/*.xml' } always { archiveArtifacts 'test-results/*.xml' } } correctly uses success { junit } and always { archiveArtifacts }, meeting the goal.
  3. Final Answer:

    post { success { junit 'test-results/*.xml' } always { archiveArtifacts 'test-results/*.xml' } } -> Option A
  4. Quick Check:

    junit on success + archive always [OK]
Hint: Use success for junit, always for archiveArtifacts [OK]
Common Mistakes:
  • Putting junit in always block causing publish on failure
  • Swapping archiveArtifacts and junit blocks
  • Using failure block incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard