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?"