What if your code could test itself every time you save it?
Why Running unit tests in pipeline in Jenkins? - Purpose & Use Cases
Imagine you write code and then manually run tests on your computer before sharing it with your team.
You have to remember to run all tests every time, and sometimes you forget or miss some.
Manually running tests is slow and easy to forget.
It causes bugs to sneak into the shared code because tests were skipped or done incorrectly.
This wastes time fixing problems later.
Running unit tests automatically in a pipeline means tests run every time code changes.
This catches errors early and saves time by stopping bad code from moving forward.
git push origin main
# Then remember to run tests manuallypipeline {
agent any
stages {
stage('Test') {
steps {
sh 'run-unit-tests.sh'
}
}
}
}It makes sure your code is always tested and safe before sharing with others.
A developer pushes code to GitHub, and Jenkins automatically runs all unit tests before merging to the main project.
Manual testing is slow and error-prone.
Automated tests in pipelines catch bugs early.
This keeps code quality high and saves time.
Practice
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 DQuick Check:
Unit tests catch bugs early [OK]
- Confusing testing with deployment
- Thinking tests create backups
- Assuming tests monitor performance
Solution
Step 1: Recall Jenkins pipeline shell syntax
Jenkins uses theshstep 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 BQuick Check:
Useshfor shell commands [OK]
- Using non-existent steps like 'run' or 'command'
- Incorrect function syntax for shell commands
- Missing quotes around command string
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'pytest tests/'
}
}
}
}Solution
Step 1: Understand default behavior of
Theshstepshstep 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 AQuick Check:
shfails pipeline on error [OK]
- Assuming pipeline continues despite test failure
- Thinking Jenkins retries failed stages automatically
- Believing tests pass regardless of results
stage('Test') {
steps {
sh pytest tests/
}
}
But the pipeline fails with a syntax error. What is the problem?Solution
Step 1: Check shell command syntax in Jenkins pipeline
Theshstep requires the command as a quoted string.Step 2: Identify missing quotes causing syntax error
Here,sh pytest tests/lacks quotes aroundpytest tests/.Final Answer:
Missing quotes around the shell command -> Option CQuick Check:
Shell commands must be quoted inshstep [OK]
sh step [OK]- Forgetting quotes around commands
- Confusing stage name with syntax error cause
- Ignoring required agent declaration elsewhere
Solution
Step 1: Understand how to catch errors without failing pipeline
ThecatchErrorstep can catch failures and mark the build unstable instead of failing.Step 2: Apply
UsingcatchErrorwith proper parameterscatchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE')around the test command allows continuation but marks stage unstable.Final Answer:
UsecatchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') { sh 'pytest tests/' }-> Option AQuick Check:
UsecatchErrorto continue on test failure [OK]
- Using '|| true' hides failures without marking unstable
- Removing test stage skips testing
- Timeout does not handle test failures
Ready to prove it?
Pass the test and this pattern moves unknown
