Bird
Raised Fist0
Jenkinsdevops~20 mins

Failing builds on test failures in Jenkins - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Test Failure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What happens when a Jenkins pipeline stage runs tests that fail?

Consider a Jenkins pipeline with a test stage that runs unit tests using a shell command. If some tests fail, what will Jenkins do by default?

Jenkins
stage('Test') {
  steps {
    sh 'pytest tests/'
  }
}
AThe build will fail and stop immediately after the test command returns a non-zero exit code.
BThe build will continue to the next stage regardless of test failures.
CThe build will mark the test stage as unstable but continue running other stages.
DThe build will ignore test failures and mark the build as successful.
Attempts:
2 left
💡 Hint

Think about how shell commands signal failure in Jenkins pipelines.

❓ Configuration
intermediate
2:00remaining
How to configure Jenkins to fail build on test failures using JUnit plugin?

You want Jenkins to run tests and fail the build if any test fails, using the JUnit plugin to parse test reports. Which post-build action configuration ensures this?

APublish JUnit test result report with 'Mark build as unstable' option checked and no fail option.
BDo not publish JUnit reports; rely on shell exit codes only.
CPublish JUnit test result report with 'Fail the build if test results are unstable' option checked.
DPublish JUnit test result report without any fail or unstable options checked.
Attempts:
2 left
💡 Hint

Look for the option that causes build failure on test failures in the JUnit plugin settings.

❓ Troubleshoot
advanced
2:00remaining
Why does Jenkins build succeed even though tests fail in a pipeline?

You have a Jenkins pipeline running tests with a shell command. Tests fail, but the build still shows success. What is the most likely cause?

Jenkins
stage('Test') {
  steps {
    sh(script: 'pytest tests/', returnStatus: true)
  }
}
AThe tests actually passed, so the build is correct.
BThe shell step uses 'returnStatus: true', so it returns the exit code but does not fail the build automatically.
CThe Jenkins agent is offline, so the build status is not updated.
DThe pipeline syntax is incorrect, causing Jenkins to ignore test failures.
Attempts:
2 left
💡 Hint

Check how the shell step handles exit codes with 'returnStatus'.

🔀 Workflow
advanced
2:00remaining
How to fail a Jenkins pipeline build on test failures when using 'returnStatus' in shell step?

You run tests in a Jenkins pipeline using a shell step with 'returnStatus: true' to capture exit code. How do you fail the build if tests fail?

Jenkins
stage('Test') {
  steps {
    script {
      def status = sh(script: 'pytest tests/', returnStatus: true)
      // What to do here?
    }
  }
}
AAdd 'echo "Tests failed"' without failing the build.
BAdd 'return status' to propagate the exit code automatically.
CAdd 'sh "exit $status"' to retry the tests.
DAdd 'if (status != 0) { error("Tests failed") }' to explicitly fail the build.
Attempts:
2 left
💡 Hint

Think about how to make the pipeline fail explicitly based on a variable.

✅ Best Practice
expert
3:00remaining
Best practice to ensure Jenkins build fails on test failures and reports results clearly

Which approach best ensures Jenkins build fails on test failures and provides clear test reports in the UI?

ARun tests with shell step capturing exit code, then publish JUnit reports with 'Fail the build if test results are unstable' enabled.
BRun tests with shell step and rely on console output only, no JUnit reports.
CRun tests with shell step ignoring exit code, then only echo test results without publishing reports.
DRun tests with shell step and mark build unstable regardless of test results.
Attempts:
2 left
💡 Hint

Consider both failing the build and showing test results in Jenkins UI.

Practice

(1/5)
1. What happens to a Jenkins build if a test command returns a failure exit code?
easy
A. The build restarts the test command automatically.
B. The build ignores the test failure and continues.
C. The build pauses and waits for manual approval.
D. The build fails automatically to indicate test failure.

Solution

  1. Step 1: Understand Jenkins build behavior on command failure

    Jenkins treats any shell or batch command that returns a non-zero exit code as a failure.
  2. Step 2: Relate test command failure to build status

    If tests fail and return a failure code, Jenkins marks the entire build as failed automatically.
  3. Final Answer:

    The build fails automatically to indicate test failure. -> Option D
  4. Quick Check:

    Test failure exit code causes build failure [OK]
Hint: Non-zero exit code from tests fails the build [OK]
Common Mistakes:
  • Thinking Jenkins ignores test failures by default
  • Assuming manual intervention is needed on test failure
  • Believing Jenkins retries failed test commands automatically
2. Which of the following Jenkins pipeline syntax snippets correctly fails the build if tests fail?
easy
A. sh 'run_tests.sh & wait' // runs tests in background
B. sh 'run_tests.sh || true' // ignores test failures
C. sh 'run_tests.sh' // fails build if script exits non-zero
D. sh 'run_tests.sh; echo done' // always succeeds

Solution

  1. Step 1: Analyze each shell command's effect on build status

    sh 'run_tests.sh' // fails build if script exits non-zero runs the test script normally; if it exits non-zero, Jenkins fails the build.
  2. Step 2: Identify commands that ignore failure

    sh 'run_tests.sh || true' // ignores test failures uses '|| true' which forces success, ignoring failures; C runs in background, not failing build; D always succeeds due to echo.
  3. Final Answer:

    sh 'run_tests.sh' // fails build if script exits non-zero -> Option C
  4. Quick Check:

    Normal shell command fails build on error [OK]
Hint: Avoid '|| true' to let Jenkins catch test failures [OK]
Common Mistakes:
  • Adding '|| true' to shell commands
  • Running tests in background without waiting
  • Appending commands that always succeed after tests
3. Given this Jenkins pipeline snippet, what will be the build result if pytest finds failing tests?
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        sh 'pytest'
      }
    }
  }
}
medium
A. Build will succeed regardless of test results.
B. Build will fail if pytest returns a failure exit code.
C. Build will be unstable but not fail.
D. Build will skip the test stage.

Solution

  1. Step 1: Understand Jenkins behavior on shell step failure

    The sh step fails the build if the command exits with a non-zero code.
  2. Step 2: Recognize pytest exit codes

    pytest returns non-zero exit code if any test fails, causing Jenkins to fail the build.
  3. Final Answer:

    Build will fail if pytest returns a failure exit code. -> Option B
  4. Quick Check:

    Failing tests cause non-zero exit, fail build [OK]
Hint: Test commands returning non-zero fail build [OK]
Common Mistakes:
  • Assuming build passes despite test failures
  • Confusing unstable with failed build
  • Thinking Jenkins skips failing test stages
4. You have a Jenkins pipeline where tests fail but the build still shows as SUCCESS. What is the most likely cause?
medium
A. The test command is run with '|| true' which ignores failures.
B. The pipeline is missing the 'agent any' declaration.
C. The Jenkins server is offline during the test stage.
D. The post section is configured to always mark build as success.

Solution

  1. Step 1: Identify why build ignores test failures

    Using '|| true' after test commands forces success regardless of test results.
  2. Step 2: Exclude other causes

    Missing 'agent any' causes pipeline failure, server offline stops build, post section can't override success if test fails.
  3. Final Answer:

    The test command is run with '|| true' which ignores failures. -> Option A
  4. Quick Check:

    '|| true' masks test failures causing success [OK]
Hint: Check for '|| true' masking failures in shell steps [OK]
Common Mistakes:
  • Blaming missing agent for test failure ignoring
  • Assuming Jenkins server offline lets build succeed
  • Misconfiguring post section to override failure
5. How can you configure a Jenkins declarative pipeline to always run tests but mark the build as UNSTABLE instead of FAILED when tests fail?
hard
A. Use post { always { junit '**/test-results.xml' } } and catchError(buildResult: 'UNSTABLE') around test step.
B. Add sh 'run_tests.sh || true' to ignore failures.
C. Set options { skipStagesAfterUnstable() } in pipeline.
D. Use post { failure { echo 'Tests failed' } } without changing build status.

Solution

  1. Step 1: Use catchError to catch test failures and mark build unstable

    Wrapping test commands with catchError(buildResult: 'UNSTABLE') prevents build failure but marks unstable.
  2. Step 2: Use post section to publish test results

    Publishing test results with junit reports test failures and sets build status accordingly.
  3. Final Answer:

    Use post { always { junit '**/test-results.xml' } } and catchError(buildResult: 'UNSTABLE') around test step. -> Option A
  4. Quick Check:

    catchError + junit marks unstable, not failed [OK]
Hint: Use catchError(buildResult: 'UNSTABLE') to mark unstable [OK]
Common Mistakes:
  • Ignoring test failures with '|| true' instead of marking unstable
  • Misusing post failure block without changing status
  • Skipping test stages after unstable status
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard