0
0
Jenkinsdevops~5 mins

Failing builds on test failures in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
When running automated tests in Jenkins, it is important to stop the build if any test fails. This helps catch problems early and prevents broken code from moving forward.
When you want to ensure only code that passes all tests is deployed.
When you want to save time by stopping builds early if tests fail.
When you want to get quick feedback on code quality after each commit.
When you want to prevent broken code from affecting other developers.
When you want to maintain a stable main branch by blocking failing changes.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the project'
                // Add build commands here
            }
        }
        stage('Test') {
            steps {
                echo 'Running tests'
                // Run tests and record results
                sh 'pytest --junitxml=results.xml'
            }
            post {
                always {
                    junit 'results.xml'
                }
                failure {
                    error 'Tests failed, stopping the build.'
                }
            }
        }
    }
}

This Jenkinsfile defines a pipeline with two stages: Build and Test.

In the Test stage, it runs tests using pytest and saves results in results.xml.

The junit step publishes test results and marks the build unstable if tests fail.

The failure block stops the build immediately if any test fails.

Commands
This command triggers the Jenkins pipeline named 'example-pipeline' to start the build and test process.
Terminal
jenkins-jobs build example-pipeline
Expected OutputExpected
Started build #15 for example-pipeline Building in workspace /var/lib/jenkins/workspace/example-pipeline [Pipeline] stage [Pipeline] { (Build) [Pipeline] echo Building the project [Pipeline] // echo [Pipeline] stage [Pipeline] { (Test) [Pipeline] echo Running tests [Pipeline] sh + pytest --junitxml=results.xml ============================= test session starts ============================== collected 3 items test_sample.py ..F [100%] =================================== FAILURES =================================== ____________________________ test_example_failure _____________________________ def test_example_failure(): > assert 1 == 2 E assert 1 == 2 test_sample.py:10: AssertionError =========================== short test summary info =========================== FAILED test_sample.py::test_example_failure - assert 1 == 2 ========================= 1 failed, 2 passed in 0.12s ========================== [Pipeline] junit Recording test results [Pipeline] error ERROR: Tests failed, stopping the build. [Pipeline] // error [Pipeline] } [Pipeline] // stage [Pipeline] End of Pipeline Finished: FAILURE
This command fetches the console output of build number 15 to review test results and failure details.
Terminal
jenkins-jobs console example-pipeline 15
Expected OutputExpected
Started by user admin Building in workspace /var/lib/jenkins/workspace/example-pipeline [Pipeline] stage [Pipeline] { (Build) [Pipeline] echo Building the project [Pipeline] // echo [Pipeline] stage [Pipeline] { (Test) [Pipeline] echo Running tests [Pipeline] sh + pytest --junitxml=results.xml ============================= test session starts ============================== collected 3 items test_sample.py ..F [100%] =================================== FAILURES =================================== ____________________________ test_example_failure _____________________________ def test_example_failure(): > assert 1 == 2 E assert 1 == 2 test_sample.py:10: AssertionError =========================== short test summary info =========================== FAILED test_sample.py::test_example_failure - assert 1 == 2 ========================= 1 failed, 2 passed in 0.12s ========================== [Pipeline] junit Recording test results [Pipeline] error ERROR: Tests failed, stopping the build. [Pipeline] // error [Pipeline] } [Pipeline] // stage [Pipeline] End of Pipeline Finished: FAILURE
Key Concept

If any test fails during the Jenkins pipeline, the build should stop immediately to prevent broken code from progressing.

Common Mistakes
Not using the 'junit' step to publish test results.
Without publishing test results, Jenkins cannot detect test failures properly and may mark builds as successful even if tests fail.
Always use the 'junit' step with the test results file to let Jenkins track test outcomes.
Not adding a failure block to stop the build on test failure.
Without the failure block, the build may continue even if tests fail, allowing broken code to deploy.
Add a 'failure' post block that calls 'error' to stop the build when tests fail.
Summary
Define a Jenkins pipeline with stages for building and testing your code.
Run tests and save results in a file like 'results.xml'.
Use the 'junit' step to publish test results and detect failures.
Add a 'failure' post block to stop the build immediately if tests fail.
Check the build console output to see test failures and fix them before continuing.