pipeline {
agent any
stages {
stage('Test') {
steps {
sh './gradlew check'
}
}
}
post {
always {
junit 'build/reports/**/*.xml'
}
}
}
While testing is a critical part of a good continuous delivery pipeline, most
people don’t want to sift through thousands of lines of console output to find
information about failing tests. To make this easier, Jenkins can record and
aggregate test results so long as your test runner can output test result
files. Jenkins typically comes bundled with the junit
step, but if your test
runner cannot output JUnit-style XML reports, there are additional plugins
which process practically any widely-used test report format.
To collect our test results and artifacts, we will use the post
section.
pipeline {
agent any
stages {
stage('Test') {
steps {
sh './gradlew check'
}
}
}
post {
always {
junit 'build/reports/**/*.xml'
}
}
}
This will always grab the test results and let Jenkins track them, calculate trends and report on them. A Pipeline that has failing tests will be marked as "UNSTABLE", denoted by yellow in the web UI. That is distinct from the "FAILED" state, denoted by red.
When there are test failures, it is often useful to grab built artifacts from Jenkins to local analysis and investigation. This is made practical by Jenkins’s built-in support for storing "artifacts", files generated during the execution of the Pipeline.
This is easily done with the archive
step and a file-globbing expression, as
is demonstrated in the example below:
pipeline {
agent any
stages {
stage('Build') {
steps {
sh './gradle build'
}
}
stage('Test') {
steps {
sh './gradlew check'
}
}
}
post {
always {
archive 'build/libs/**/*.jar'
junit 'build/reports/**/*.xml'
}
}
}
Recording tests and artifacts is Jenkins is useful for quickly and easily surfacing information to various members of the team. In the next section we’ll talk about how to tell those members of the team what’s been happening in our Pipeline.