What is the purpose of running unit tests in a Jenkins pipeline?
Running unit tests in a Jenkins pipeline helps catch code errors early by automatically checking if small parts of the code work correctly before moving forward.
Click to reveal answer
beginner
Which Jenkins pipeline step is commonly used to run shell commands like unit tests?
The sh step is used to run shell commands, such as executing unit test scripts or commands in a Jenkins pipeline.
Click to reveal answer
intermediate
How do you fail a Jenkins pipeline if unit tests fail?
If the unit test command returns a non-zero exit code, Jenkins marks the pipeline as failed automatically, stopping further steps.
Click to reveal answer
beginner
What is the benefit of using a 'stage' for unit tests in a Jenkins pipeline?
Using a separate 'stage' for unit tests organizes the pipeline clearly, making it easy to see test results and isolate test failures.
Click to reveal answer
beginner
Show a simple Jenkins pipeline snippet that runs unit tests using a shell command.
pipeline {
agent any
stages {
stage('Unit Tests') {
steps {
sh 'python -m unittest discover'
}
}
}
}
Click to reveal answer
What happens if unit tests fail in a Jenkins pipeline stage?
AThe pipeline skips the tests and moves on
BThe pipeline ignores the failure and continues
CThe pipeline retries the tests automatically
DThe pipeline stage fails and stops further execution
✗ Incorrect
If unit tests fail (non-zero exit code), Jenkins marks the stage as failed and stops the pipeline unless configured otherwise.
Which Jenkins pipeline syntax is used to run a shell command?
Ash
Bcmd
Crun
Dexecute
✗ Incorrect
The sh step runs shell commands in Jenkins pipelines.
Why is it good practice to have a separate stage for unit tests in Jenkins?
ATo speed up the pipeline by skipping tests
BTo organize pipeline and clearly see test results
CTo run tests only after deployment
DTo avoid running tests automatically
✗ Incorrect
Separating unit tests into their own stage helps organize the pipeline and makes test results easy to find.
What command might you use in a Jenkins pipeline to run Python unit tests?
Apython -m unittest discover
Bnpm test
Cmvn test
Dgradle build
✗ Incorrect
The command python -m unittest discover runs Python unit tests.
In Jenkins, what does a non-zero exit code from a test command indicate?
APipeline success
BTest success
CTest failure
DNo tests run
✗ Incorrect
A non-zero exit code means the command failed, indicating test failures.
Explain how to add unit tests to a Jenkins pipeline and what happens if tests fail.
Think about pipeline stages and shell commands.
You got /3 concepts.
Describe the benefits of running unit tests automatically in a Jenkins pipeline.
Consider how automation helps in software quality.
You got /3 concepts.
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.