The test stage in a CI pipeline checks if your code works correctly by running automated tests. This helps catch mistakes early before the code is shared with others.
CI pipeline test stage in JUnit
stage('Test') { steps { junit '**/target/surefire-reports/*.xml' } }
This example is for a Jenkins pipeline using the junit step to publish test results.
The pattern **/target/surefire-reports/*.xml matches JUnit test report files.
reports folder.stage('Test') { steps { junit 'reports/*.xml' } }
stage('Test') { steps { junit allowEmptyResults: true, testResults: '**/test-results/*.xml' } }
This Jenkins pipeline has two stages: Build and Test. The Test stage publishes JUnit test results.
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building the project...'
}
}
stage('Test') {
steps {
junit '**/target/surefire-reports/*.xml'
}
}
}
}Make sure your tests generate JUnit XML reports for the junit step to find.
If tests fail, the pipeline will usually stop and show errors in the test report.
Use the allowEmptyResults option if your tests might not run every time.
The test stage runs automated tests to check code correctness.
JUnit reports are used to show test results in the pipeline.
Failing tests help catch problems early before code is shared.
Practice
Solution
Step 1: Understand the role of the test stage
The test stage runs automated tests to check if the code works as expected.Step 2: Identify the correct purpose
Running tests helps catch errors early before code is shared or deployed.Final Answer:
To run automated tests and verify code correctness -> Option DQuick Check:
Test stage = run tests [OK]
- Confusing test stage with deployment
- Thinking test stage merges code
- Assuming test stage creates docs
Solution
Step 1: Identify the build tool for JUnit tests
JUnit tests are commonly run using Maven with the commandmvn test.Step 2: Check other options
npm run testis for Node.js projects,docker buildbuilds images, andgit commitcommits code.Final Answer:
mvn test -> Option AQuick Check:
JUnit test command = mvn test [OK]
- Using npm commands for Java projects
- Confusing build commands with test commands
- Running git commands instead of tests
steps:
- name: Run tests
run: mvn test
continue-on-error: falseWhat happens if a JUnit test fails during this step?
Solution
Step 1: Understand 'continue-on-error: false'
This setting means the pipeline will stop if the command fails.Step 2: Recognize test failure effect
If a JUnit test fails,mvn testreturns an error, so the pipeline stops and marks failure.Final Answer:
The pipeline stops and marks the test stage as failed -> Option AQuick Check:
continue-on-error false + test fail = stop pipeline [OK]
- Assuming pipeline continues despite failure
- Thinking tests auto-retry on failure
- Believing test stage is skipped on failure
Solution
Step 1: Check test report generation
JUnit reports must be generated and saved as artifacts for the UI to display them.Step 2: Understand impact of missing reports
If reports are missing or not saved, the UI cannot show test results even if tests ran.Final Answer:
JUnit report files are not generated or not saved as artifacts -> Option CQuick Check:
Missing reports = no UI display [OK]
- Assuming syntax errors cause missing reports
- Thinking pipeline skipping always hides reports
- Believing missing config file causes this specific issue
Solution
Step 1: Ensure tests run and fail build on failure
Usingmvn testwithcontinue-on-error: falsestops the build on test failure.Step 2: Save test reports as artifacts
Saving JUnit reports as artifacts allows review of test results even if build fails.Final Answer:
Run tests with mvn test, save reports as artifacts, and set continue-on-error: false -> Option BQuick Check:
Fail build + save reports = mvn test + artifacts + continue-on-error false [OK]
- Setting continue-on-error true hides failures
- Not saving reports loses test details
- Skipping tests but expecting reports
Ready to prove it?
Pass the test and this pattern moves unknown
