Bird
Raised Fist0
Jenkinsdevops~25 mins

JUnit test report publishing in Jenkins - Mini Project: Build & Apply

Choose your learning style10 modes available
Publishing JUnit Test Reports in Jenkins Pipeline
📖 Scenario: Your team runs Java unit tests using Maven. The tests generate JUnit XML reports, but developers have to SSH into the build server to check results. You want to configure the Jenkins pipeline to automatically publish test reports so the team can view results directly in the Jenkins UI.
🎯 Goal: Build a Jenkins pipeline that runs Maven tests and publishes JUnit XML reports using the junit step, with proper error handling so reports are published even when tests fail.
📋 What You'll Learn
Create a Jenkins pipeline with a pipeline block and agent any
Add a Test stage that runs mvn test
Add a post section with always to publish JUnit reports
Use the junit step with the correct report path pattern
Print a confirmation message after report publishing
💡 Why This Matters
🌍 Real World
Publishing JUnit reports in Jenkins gives teams instant visibility into test health without SSH access, enabling faster feedback loops.
💼 Career
DevOps engineers configure test report publishing as a standard part of CI/CD pipelines for every Java project.
Progress0 / 4 steps
1
Create the pipeline with a Test stage
Create a Jenkins declarative pipeline with agent any and a stages section containing one stage named Test. Inside the Test stage, add a steps block with a shell command mvn test to run the unit tests.
Jenkins
Hint

Start with pipeline { agent any stages { stage('Test') { steps { sh 'mvn test' } } } }.

2
Add environment variable for report path
Add an environment block inside the pipeline block to define a variable REPORT_PATH set to 'target/surefire-reports/*.xml'. This keeps the report path configurable.
Jenkins
Hint

Use environment { REPORT_PATH = 'target/surefire-reports/*.xml' } inside the pipeline block.

3
Add post section to publish JUnit reports
Add a post section inside the pipeline block with an always block. Inside always, use the junit step with "${REPORT_PATH}" to publish the test reports regardless of whether tests pass or fail.
Jenkins
Hint

Use post { always { junit "${REPORT_PATH}" } } after the stages block.

4
Add confirmation echo after report publishing
Inside the always block, after the junit step, add echo 'Test reports published' to confirm reports were processed.
Jenkins
Hint

Add echo 'Test reports published' after the junit step inside always.

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