记录测试结果与中间详细过程

Recording tests and artifacts

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.

Jenkinsfile (Declarative Pipeline)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
pipeline {
agent any
stages {
stage('Test') {
steps {
sh './gradlew check'
}
}
}
post {
always {
junit 'build/reports/**/*.xml'
}
}
}

Toggle Scripted Pipeline (Advanced)
Jenkinsfile (Scripted Pipeline)

1
2
3
4
5
6
7
8
9
node {
try{
stage('Test') {
sh './gradlew check'
}
finally {
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 for 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 archiveArtifacts step and a file-globbing expression, as is demonstrated in the example below:

Jenkinsfile (Declarative Pipeline)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
pipeline {
agent any
stages {
stage('Build') {
steps {
sh './gradlew build'
}
}
stage('Test') {
steps {
sh './gradlew check'
}
}
}

post {
always {
archiveArtifacts artifacts: 'build/libs/**/*.jar', fingerprint: true
junit 'build/reports/**/*.xml'
}
}
}

Jenkinsfile (Scripted Pipeline)

1
2
3
4
5
6
7
8
9
10
node {
try{
stage('Test') {
sh './gradlew check'
}
finally {
archiveArtifacts artifacts: 'build/libs/**/*.jar', fingerprint: true
junit 'build/reports/**/*.xml'
}
}

If more than one parameter is specified in the archiveArtifacts step, then each parameter’s name must explicitly be specified in the step code - i.e. artifacts for the artifact’s path and file name and fingerprint to choose this option. If you only need to specify the artifacts’ path and file name/s, then you can omit the parameter name artifacts - e.g. archiveArtifacts 'build/libs/**/*.jar'

Recording tests and artifacts in 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.

Continue to “Cleaning up and notifications”

Donate - Support to make this site better.
捐助 - 支持我让我做得更好.