Build and test stages

JUnit reports, and why FAILURE and UNSTABLE are different promises.

Intermediate13 min · lesson 8 of 15
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (22 files, 7 KB): buildtest.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-jenkins/buildtest.tar.gz && tar -xzf buildtest.tar.gz, which creates ~/lab/buildtest/. SHA-256: e522fef8f65bd75a7f61268dfb2edaf244ac1974c88a220ea6e150aea8306f2e

A team's release pipeline has been green for weeks. Then a customer reports a bug that a test has been catching the whole time. Someone had added || true to the test command months earlier, to get past a flaky test during a deadline. The test kept failing and the pipeline kept going. In this lesson you'll build the honest version and then the dishonest one, and learn to tell them apart from a build's result alone.

Build, test, report

app/Jenkinsfile
pipeline {
agent { label 'linux' }
stages {
stage('Build') {
steps { sh 'python3 -m compileall -q .' }
}
stage('Test') {
steps { sh 'python3 -m pytest -q --junitxml=reports/junit.xml' }
post {
always { junit 'reports/junit.xml' }
}
}
stage('Package') {
steps { echo 'packaging' }
}
}
}

For this Python app, "build" means compileall, which finds syntax errors quickly without running anything. In a compiled language this is your compiler. The stage is first because it's cheap, and a syntax error makes the tests pointless. --junitxml writes results in the JUnit XML format nearly every test tool can produce, and the junit step reads them. It's in post { always } so results are published when tests fail, which is precisely when you need them.

ubuntu@secopslog-cicd:~/lab/buildtest · Jenkins 2.580.2
$ ./up.sh
agent lab-agent: online Jenkins 2.580.2 is up on http://127.0.0.1:8080
$ ./seed.sh app app/ && ./newjob.sh strict app && ./newjob.sh tolerant app Jenkinsfile.tolerant && ./run-job.sh strict
pushed app: 78d1acd first commit job strict reads Jenkinsfile from git://git/app.git job tolerant reads Jenkinsfile.tolerant from git://git/app.git Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/strict + python3 -m compileall -q . + python3 -m pytest -q --junitxml=reports/junit.xml ... [100%] -- generated xml file: /home/jenkins/agent/workspace/strict/reports/junit.xml -- 3 passed in 0.00s Post stage [Pipeline] junit Recording test results build #1 result: SUCCESS
$ ./jk.sh '/job/strict/1/testReport/api/json?tree=passCount,failCount,skipCount' | jq -c 'del(._class)'
curl: (22) The requested URL returned error: 404

Jenkins now holds results it can query, track across builds and show in the UI. [Checks API] No suitable checks publisher found is only telling you no GitHub or GitLab plugin is reporting status back, and it's harmless here.

Two kinds of red

Push a syntax error first:

ubuntu@secopslog-cicd:~/lab/buildtest · Jenkins 2.580.2
$ cp app/calc.py /tmp/calc.ok; echo 'def broken(:' >> app/calc.py; ./commit.sh app/ 'typo'
pushed: 6f3849e typo
$ ./run-job.sh strict
Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/strict + python3 -m compileall -q . *** Error compiling './calc.py'... File "./calc.py", line 7 def broken(: ^ SyntaxError: invalid syntax Stage "Test" skipped due to earlier failure(s) Stage "Package" skipped due to earlier failure(s) ERROR: script returned exit code 1 Finished: FAILURE build #2 result: FAILURE

The build stage caught it, and both later stages were skipped. No time went to tests that couldn't pass anyway. Now fix that and push a logic bug instead, one that compiles fine:

ubuntu@secopslog-cicd:~/lab/buildtest · Jenkins 2.580.2
$ cp /tmp/calc.ok app/calc.py; sed -i 's|a / b|a // b + 1|' app/calc.py; ./commit.sh app/ 'off by one'
pushed: 12fca52 off by one
$ ./run-job.sh strict
Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/strict + python3 -m compileall -q . + python3 -m pytest -q --junitxml=reports/junit.xml .F. [100%] =================================== FAILURES =================================== ___________________________________ test_div ___________________________________ def test_div(): > assert div(6, 2) == 3 E assert 4 == 3 E + where 4 = div(6, 2) test_calc.py:10: AssertionError -- generated xml file: /home/jenkins/agent/workspace/strict/reports/junit.xml -- =========================== short test summary info ============================ FAILED test_calc.py::test_div - assert 4 == 3 + where 4 = div(6, 2) 1 failed, 2 passed in 0.01s Post stage [Pipeline] junit Recording test results [Checks API] No suitable checks publisher found. Stage "Package" skipped due to earlier failure(s) ERROR: script returned exit code 1 Finished: FAILURE build #3 result: FAILURE
$ ./jk.sh '/job/strict/lastBuild/testReport/api/json' | jq -r '.suites[].cases[] | select(.status != "PASSED") | "\(.status) \(.name)"'
FAILED test_div

pytest exited 1, so the sh step failed, the post block still published the report, Package was skipped, and the result is FAILURE. You can also list which test failed from the API, without reading the log. Nothing got packaged from broken code, and that's the behaviour you want from a pipeline.

The || true trap

Here's the shortcut from the opening, in a separate job on the same broken commit:

app/Jenkinsfile.tolerant
pipeline {
agent { label 'linux' }
stages {
stage('Test') {
steps { sh 'python3 -m pytest -q --junitxml=reports/junit.xml || true' }
post {
always { junit 'reports/junit.xml' }
}
}
stage('Package') {
steps { echo 'packaging' }
}
}
}
ubuntu@secopslog-cicd:~/lab/buildtest · Jenkins 2.580.2
$ ./run-job.sh tolerant
Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/tolerant + python3 -m pytest -q --junitxml=reports/junit.xml .F. [100%] =================================== FAILURES =================================== ___________________________________ test_div ___________________________________ def test_div(): > assert div(6, 2) == 3 E assert 4 == 3 E + where 4 = div(6, 2) test_calc.py:10: AssertionError - generated xml file: /home/jenkins/agent/workspace/tolerant/reports/junit.xml - =========================== short test summary info ============================ FAILED test_calc.py::test_div - assert 4 == 3 + where 4 = div(6, 2) 1 failed, 2 passed in 0.01s + true Post stage [Pipeline] junit Recording test results [Checks API] No suitable checks publisher found. packaging Finished: UNSTABLE build #1 result: UNSTABLE
$ ./down.sh
lab stopped and its data removed

The same test failed. But || true made the shell exit 0, so Package ran on code with a known bug. The junit step saw the failure in the XML and set the result to UNSTABLE, yellow rather than red. Here's how the two results compare:

ResultSet byLater stagesMeans
FAILUREA step failingSkippedThe pipeline stopped
UNSTABLEjunit finding failed tests in a reportRunThe pipeline carried on with failing tests

If a step may fail without stopping the build, say so in the Jenkinsfile instead of hiding it in the shell. catchError catches the failure and lets you choose two results: one for the stage and one for the build. Here a scan fails, its stage is marked failed, and the build stays green and continues:

app/Jenkinsfile.catch
pipeline {
agent { label 'linux' }
stages {
stage('Scan') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh 'echo "scan found 2 issues"; exit 1'
}
}
}
stage('Package') { steps { echo 'packaging' } }
}
}
ubuntu@secopslog-cicd:~/lab/buildtest · Jenkins 2.580.2
$ ./newjob.sh catch app Jenkinsfile.catch && ./run-job.sh catch
job catch reads Jenkinsfile.catch from git://git/app.git Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/catch [Pipeline] catchError + echo scan found 2 issues scan found 2 issues + exit 1 ERROR: script returned exit code 1 packaging Finished: SUCCESS build #1 result: SUCCESS

The failing step printed ERROR: script returned exit code 1, yet Package ran and the build finished SUCCESS. The stage itself is recorded as failed, which plugins that draw stages, such as Pipeline Graph View, show in red. This lab installs none, so here only the ERROR line shows it. Unlike || true, the failure stays visible and the intent is written down.

Some teams use UNSTABLE deliberately for a non-critical suite, with catchError(buildResult: 'UNSTABLE') or the unstable step to mark that intent. That can be reasonable for reporting. It's a bad choice ahead of anything that ships, because each later stage runs with failing tests behind it. If you see || true on a test command in a release pipeline, treat it as a bug.

Hands-on

In a fresh lab, seed and run the strict job. Add a test that always fails, and add a Deploy stage guarded by when { expression { currentBuild.currentResult == 'SUCCESS' } } to app/Jenkinsfile.tolerant. Push, run tolerant, and check whether Deploy ran. What does that tell you about guarding stages in a pipeline that has swallowed an exit code? Finish with ./down.sh. A worked version is in solutions/.

What you should see: two tests fail (test_always_fails and test_div, which the fresh lab inherited from the walkthrough's broken commit), || true makes the shell step pass, and junit in the Test stage's own post sets the build to UNSTABLE at that moment. Package still runs, and Deploy is skipped due to the when conditional, because currentBuild.currentResult was already UNSTABLE. The guard worked only because the result was set before Deploy. If the junit step ran in the pipeline-level post instead, the result would still have been SUCCESS when the guard ran. A guard is no substitute for letting the test step fail.

Remember

Let the test command's exit status fail the build, and publish reports from post { always }. A yellow UNSTABLE build still ran Package on the failing commit, so treat || true on a test command as a bug.

Quick check
01The junit step sits in the steps block after sh 'pytest ...'. Tests fail. What do you see in the build?
Incorrect — A failed step stops the block, so the next step does not run.
Incorrect — junit never ran here, so it cannot affect the result.
Correct — That's why it goes in post { always }, which runs after a failed step.
Incorrect — It is valid in steps. It's just in the wrong place to be useful.
02A build's result is UNSTABLE, and its Package stage ran. What most likely happened?
Correct — A swallowed exit code such as || true, or a deliberate marking, produces exactly this.
Incorrect — A lost agent normally fails the step, giving FAILURE.
Incorrect — A timeout ends the build as ABORTED, which is not this result.
Incorrect — Syntax problems fail at compile time. There is no such warning state.
03Why run a cheap compile or syntax check as a stage before the tests?
Incorrect — The reports come from the test tool and do not depend on a prior stage.
Incorrect — Stage names are free-form, and none is required.
Incorrect — It finds syntax errors only. The logic bug in the lab passed it.
Correct — In the lab, both later stages were skipped when compilation failed.

Related