0
0
Jenkinsdevops~10 mins

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

Choose your learning style9 modes available
Process Flow - Running unit tests in pipeline
Start Pipeline
Checkout Code
Run Unit Tests
Tests Pass?
NoFail 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.