📖 Scenario: You are working on a software project that uses Jenkins to automate building and testing code. To ensure your code is reliable, you want to run unit tests automatically every time you update the code. This project will guide you to create a simple Jenkins pipeline script that runs unit tests.
🎯 Goal: Build a Jenkins pipeline script that defines a basic pipeline with a stage to run unit tests using a shell command.
📋 What You'll Learn
Create a Jenkins pipeline script with a pipeline block
Add an agent any directive to run on any available agent
Define a stage named Run Unit Tests
Inside the stage, run the shell command ./run_tests.sh to execute unit tests
Print the output of the test command in the pipeline logs
💡 Why This Matters
🌍 Real World
Automating unit tests in Jenkins pipelines helps catch bugs early and keeps code quality high.
💼 Career
Knowing how to write Jenkins pipelines to run tests is a key skill for DevOps engineers and automation specialists.
Progress0 / 4 steps
1
Create the basic Jenkins pipeline structure
Write a Jenkins pipeline script starting with pipeline { and add agent any inside it. This sets up the pipeline to run on any available Jenkins agent.
Jenkins
Hint
Start your Jenkinsfile with pipeline { and add agent any inside the block.
2
Add a stage to run unit tests
Inside the pipeline block, add a stages block. Then add a stage named Run Unit Tests inside stages. This stage will hold the commands to run your tests.
Jenkins
Hint
Use stages {} to group stages, then add stage('Run Unit Tests') {} inside it.
3
Add a step to run the test script
Inside the stage('Run Unit Tests') block, add a steps block. Inside steps, add a shell command to run ./run_tests.sh using sh. This will execute your unit tests.
Jenkins
Hint
Use steps { sh './run_tests.sh' } to run the test script.
4
Print the test results in the pipeline output
Add a post block inside the stage('Run Unit Tests') block. Inside post, add an always block that runs a shell command to print Test stage completed using sh. This confirms the test stage finished.
Jenkins
Hint
Use post { always { sh 'echo Test stage completed' } } inside the stage to print a message after tests run.
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
Step 1: Understand the role of unit tests
Unit tests check small parts of code to find bugs early.
Step 2: Connect unit tests to pipeline goals
Running tests in Jenkins pipeline helps maintain code quality before deployment.
Final Answer:
To catch bugs early and ensure code quality -> Option D
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
Step 1: Recall Jenkins pipeline shell syntax
Jenkins uses the sh step to run shell commands.
Step 2: Match the correct syntax
sh 'pytest tests/' is the correct way to run tests in a shell step.
Final Answer:
sh 'pytest tests/' -> Option B
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
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).
Step 2: Predict pipeline reaction to test failure
Since tests fail, the pipeline stage fails and stops further execution.
Final Answer:
Pipeline fails and stops execution -> Option A
Quick Check:
sh fails pipeline on error [OK]
Hint: Failed shell commands stop pipeline by default [OK]
Here, sh pytest tests/ lacks quotes around pytest tests/.
Final Answer:
Missing quotes around the shell command -> Option C
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
Step 1: Understand how to catch errors without failing pipeline
The catchError step can catch failures and mark the build unstable instead of failing.
Step 2: Apply catchError with proper parameters
Using catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') around the test command allows continuation but marks stage unstable.
Final Answer:
Use catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') { sh 'pytest tests/' } -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.