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.
0
0
CI pipeline test stage in JUnit
Introduction
After writing new code to make sure it does not break existing features.
Before merging code changes to the main project branch.
When you want to automatically verify code quality on every update.
To run unit tests that check small parts of your program.
To ensure your software behaves as expected after fixes or improvements.
Syntax
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.
Examples
Publishes test reports from the
reports folder.JUnit
stage('Test') { steps { junit 'reports/*.xml' } }
Allows the test results to be empty without failing the build.
JUnit
stage('Test') { steps { junit allowEmptyResults: true, testResults: '**/test-results/*.xml' } }
Sample Program
This Jenkins pipeline has two stages: Build and Test. The Test stage publishes JUnit test results.
JUnit
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building the project...'
}
}
stage('Test') {
steps {
junit '**/target/surefire-reports/*.xml'
}
}
}
}OutputSuccess
Important Notes
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.
Summary
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.