0
0
Jenkinsdevops~10 mins

JUnit test report publishing in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - JUnit test report publishing
Start Pipeline
Run mvn test
Tests Execute
Post: always
junit step parses XML
Reports visible in Jenkins UI
Build marked stable/unstable
The pipeline runs Maven tests which generate JUnit XML reports. Regardless of pass or fail, the post/always block runs the junit step to parse XML reports and display results in Jenkins UI.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        sh 'mvn test'
      }
    }
  }
  post {
    always {
      junit 'target/surefire-reports/*.xml'
    }
  }
}
This Jenkins pipeline runs Maven tests and publishes JUnit XML reports from surefire-reports directory using the post/always block.
Process Table
StepActionWorkspace StateReports StateBuild StatusNext Step
1Pipeline starts, agent allocatedEmpty workspace checked outNo reports yetIn progressRun tests
2sh 'mvn test' executesSource compiled, tests runningNo reports yetIn progressTests complete
3Maven Surefire generates XML reportstarget/surefire-reports/*.xml createdXML files on disk (3 files)In progressEnter post block
4post/always block triggersWorkspace unchangedXML files on diskIn progressParse reports
5junit step parses XML reportsWorkspace unchanged10 tests: 8 passed, 1 failed, 1 skippedUnstable (has failures)Display results
6Test results visible in Jenkins UIWorkspace unchangedReport published with trendsUnstablePipeline ends
💡 Reports published successfully. Build marked UNSTABLE due to 1 test failure.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
testsPassed00888
testsFailed00111
testsSkipped00111
reportsPublishedNoNoNoYesYes
buildStatusIn progressIn progressIn progressUnstableUnstable
Key Moments - 3 Insights
Why use post/always instead of putting junit inside the Test stage steps?
If junit is inside steps and mvn test fails (non-zero exit), the stage aborts before reaching the junit step. Using post/always ensures reports are published regardless of test pass or fail.
What is the difference between UNSTABLE and FAILURE build status?
UNSTABLE means tests ran but some failed — the junit step sets this automatically. FAILURE means the pipeline itself crashed (e.g., syntax error, agent unavailable). Test failures produce UNSTABLE, not FAILURE.
Where do the XML report files come from?
Maven Surefire plugin generates JUnit-format XML files in target/surefire-reports/ automatically when running mvn test. The junit step reads these files — it does not run tests itself.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table. At which step do XML report files first appear on disk?
AStep 1 — when the agent is allocated
BStep 2 — when mvn test runs
CStep 3 — when Surefire generates reports
DStep 5 — when junit step parses them
💡 Hint
Check the Reports State column — reports appear after Maven finishes running tests.
From the variable tracker, what is the build status after the junit step parses the reports?
ASuccess
BFailure
CUnstable
DIn progress
💡 Hint
Check the buildStatus row after Step 5 — failed tests cause UNSTABLE, not FAILURE.
What would happen if the junit path pattern 'target/surefire-reports/*.xml' matched zero files?
AJenkins publishes an empty report with 0 tests
BJenkins throws an error: No test report files found
CJenkins skips report publishing silently
DJenkins marks the build as SUCCESS
💡 Hint
The junit step requires at least one matching file — use allowEmptyResults: true to handle missing reports gracefully.
Concept Snapshot
JUnit test report publishing makes test results visible in Jenkins UI.
Maven Surefire generates XML reports in target/surefire-reports/.
The junit pipeline step parses XML files — it does not run tests.
Always use post/always to ensure reports publish even on failure.
Failed tests set build to UNSTABLE, not FAILURE.
Use allowEmptyResults: true if reports might not exist.
Full Transcript
This visual execution shows how Jenkins publishes JUnit test reports. The pipeline runs mvn test which executes unit tests. Maven Surefire plugin generates XML report files in the target/surefire-reports directory. After tests complete, whether they pass or fail, the post/always block triggers. The junit step reads and parses the XML files, extracting test counts for passed, failed, and skipped tests. Jenkins displays these results in the UI with trends over time. If any tests fail, the build is marked UNSTABLE. The key insight is that junit only parses reports — it does not run tests — and post/always ensures reports are always published.