Running unit tests in pipeline in Jenkins - Time & Space Complexity
When running unit tests in a Jenkins pipeline, it's important to understand how the time taken grows as the number of tests increases.
We want to know how the total test execution time changes when we add more tests.
Analyze the time complexity of the following Jenkins pipeline snippet that runs unit tests.
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
def tests = ['test1', 'test2', 'test3', '...']
for (test in tests) {
sh "run-unit-test ${test}"
}
}
}
}
}
}
This code runs each unit test one by one inside the pipeline.
Look for repeated actions that take time.
- Primary operation: Running each unit test command inside the loop.
- How many times: Once for each test in the list.
As the number of tests increases, the total time grows because each test runs separately.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 test runs |
| 100 | 100 test runs |
| 1000 | 1000 test runs |
Pattern observation: The total time grows roughly in direct proportion to the number of tests.
Time Complexity: O(n)
This means if you double the number of tests, the total time roughly doubles too.
[X] Wrong: "Running tests in a loop means the time stays the same no matter how many tests there are."
[OK] Correct: Each test runs separately, so more tests mean more total time.
Understanding how test execution time grows helps you design efficient pipelines and shows you can think about scaling tasks in automation.
"What if we ran all tests in parallel instead of one by one? How would the time complexity change?"
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
