Bird
Raised Fist0
Jenkinsdevops~10 mins

Running unit tests in pipeline in Jenkins - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Running unit tests in pipeline
Start Pipeline
↓
Checkout Code
↓
Run Unit Tests
↓
Tests Pass?
No→Fail Pipeline
Yes↓
Continue Pipeline / Deploy
The pipeline starts by getting the code, then runs unit tests. If tests pass, it continues; if not, it stops.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        sh 'pytest'
      }
    }
  }
}
This Jenkins pipeline runs unit tests using pytest in the 'Test' stage.
Process Table
StepActionCommand RunResultPipeline State
1Start PipelineN/APipeline startedRunning
2Checkout Codegit checkout mainCode checked outRunning
3Run Unit TestspytestTests runningRunning
4Tests Pass?pytest exit code 0All tests passedRunning
5Continue PipelineN/AProceed to next stage or deploySuccess
💡 Pipeline stops if tests fail (non-zero exit code), here tests passed so pipeline continues.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
pipeline_stateNot startedRunningRunningRunningSuccess
tests_statusNot runNot runRunningPassedPassed
Key Moments - 2 Insights
Why does the pipeline stop if tests fail?
Because the test command returns a non-zero exit code, Jenkins marks the stage as failed and stops the pipeline (see execution_table step 4).
What does 'pytest exit code 0' mean?
It means all tests passed successfully, so the pipeline can continue (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the pipeline state after running unit tests?
ARunning
BFailed
CSuccess
DNot started
💡 Hint
Check the 'Pipeline State' column at step 3 in the execution_table.
At which step does the pipeline decide to stop if tests fail?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Tests Pass?' decision in execution_table step 4.
If pytest returns a non-zero exit code, how would the pipeline state change in the variable tracker?
Apipeline_state would be 'Success'
Bpipeline_state would be 'Failed'
Ctests_status would be 'Passed'
Dtests_status would be 'Not run'
💡 Hint
Refer to variable_tracker 'pipeline_state' changes when tests fail.
Concept Snapshot
Jenkins pipeline runs unit tests in a stage.
If tests pass (exit code 0), pipeline continues.
If tests fail (non-zero exit code), pipeline stops.
Use 'sh' step to run test commands like 'pytest'.
Check exit codes to control pipeline flow.
Full Transcript
This Jenkins pipeline example shows how unit tests run inside a pipeline stage. The pipeline starts, checks out code, then runs tests using the 'pytest' command. If tests pass with exit code 0, the pipeline continues to the next stage or deployment. If tests fail, the pipeline stops immediately. The execution table traces each step, showing pipeline state changes and test results. The variable tracker records the pipeline state and test status after each step. Key moments clarify why the pipeline stops on test failure and what exit codes mean. The quiz tests understanding of pipeline state and decision points based on the execution visuals.

Practice

(1/5)
1. What is the main purpose of running unit tests in a Jenkins pipeline?
easy
A. To monitor server performance
B. To deploy the application automatically
C. To create a backup of the code repository
D. To catch bugs early and ensure code quality

Solution

  1. Step 1: Understand the role of unit tests

    Unit tests check small parts of code to find bugs early.
  2. Step 2: Connect unit tests to pipeline goals

    Running tests in Jenkins pipeline helps maintain code quality before deployment.
  3. Final Answer:

    To catch bugs early and ensure code quality -> Option D
  4. Quick Check:

    Unit tests catch bugs early [OK]
Hint: Unit tests catch bugs early, not deploy or backup [OK]
Common Mistakes:
  • Confusing testing with deployment
  • Thinking tests create backups
  • Assuming tests monitor performance
2. Which Jenkins pipeline syntax correctly runs unit tests using a shell command?
easy
A. execute shell('pytest tests/')
B. sh 'pytest tests/'
C. run 'pytest tests/'
D. command('pytest tests/')

Solution

  1. Step 1: Recall Jenkins pipeline shell syntax

    Jenkins uses the sh step to run shell commands.
  2. Step 2: Match the correct syntax

    sh 'pytest tests/' is the correct way to run tests in a shell step.
  3. Final Answer:

    sh 'pytest tests/' -> Option B
  4. Quick Check:

    Use sh for shell commands [OK]
Hint: Use 'sh' step to run shell commands in Jenkins pipeline [OK]
Common Mistakes:
  • Using non-existent steps like 'run' or 'command'
  • Incorrect function syntax for shell commands
  • Missing quotes around command string
3. Given this Jenkins pipeline snippet, what will be the output if tests fail?
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        sh 'pytest tests/'
      }
    }
  }
}
medium
A. Pipeline fails and stops execution
B. Pipeline ignores test failures and continues
C. Pipeline retries the test stage automatically
D. Pipeline marks tests as passed regardless

Solution

  1. Step 1: Understand default behavior of sh step

    The sh step fails the pipeline if the command exits with a non-zero status (test failure).
  2. Step 2: Predict pipeline reaction to test failure

    Since tests fail, the pipeline stage fails and stops further execution.
  3. Final Answer:

    Pipeline fails and stops execution -> Option A
  4. Quick Check:

    sh fails pipeline on error [OK]
Hint: Failed shell commands stop pipeline by default [OK]
Common Mistakes:
  • Assuming pipeline continues despite test failure
  • Thinking Jenkins retries failed stages automatically
  • Believing tests pass regardless of results
4. You wrote this Jenkins pipeline stage to run tests:
stage('Test') {
  steps {
    sh pytest tests/
  }
}
But the pipeline fails with a syntax error. What is the problem?
medium
A. Incorrect stage name
B. Missing agent declaration
C. Missing quotes around the shell command
D. Using 'sh' outside steps block

Solution

  1. Step 1: Check shell command syntax in Jenkins pipeline

    The sh step requires the command as a quoted string.
  2. Step 2: Identify missing quotes causing syntax error

    Here, sh pytest tests/ lacks quotes around pytest tests/.
  3. Final Answer:

    Missing quotes around the shell command -> Option C
  4. Quick Check:

    Shell commands must be quoted in sh step [OK]
Hint: Always quote shell commands in sh step [OK]
Common Mistakes:
  • Forgetting quotes around commands
  • Confusing stage name with syntax error cause
  • Ignoring required agent declaration elsewhere
5. How can you modify a Jenkins pipeline to run unit tests but continue the pipeline even if tests fail, while still marking the test stage as unstable?
hard
A. Use catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') { sh 'pytest tests/' }
B. Use sh 'pytest tests/' || true without any catchError
C. Remove the test stage from the pipeline
D. Use sh 'pytest tests/' inside a timeout block

Solution

  1. Step 1: Understand how to catch errors without failing pipeline

    The catchError step can catch failures and mark the build unstable instead of failing.
  2. Step 2: Apply catchError with proper parameters

    Using catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') around the test command allows continuation but marks stage unstable.
  3. Final Answer:

    Use catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') { sh 'pytest tests/' } -> Option A
  4. Quick Check:

    Use catchError to continue on test failure [OK]
Hint: Wrap tests in catchError to continue but mark unstable [OK]
Common Mistakes:
  • Using '|| true' hides failures without marking unstable
  • Removing test stage skips testing
  • Timeout does not handle test failures
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