Failing builds on test failures in Jenkins - Time & Space Complexity
We want to understand how the time to run a Jenkins build changes when tests are involved.
Specifically, how does the build time grow as the number of tests increases?
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
def testResults = runTests() // runs all tests
if (testResults.hasFailures()) {
error('Tests failed, stopping build')
}
}
}
}
}
}
This code runs a set of tests and fails the build if any test fails.
Look for repeated actions that take time.
- Primary operation: Running each test in the test suite.
- How many times: Once per test, repeated for all tests.
As the number of tests grows, the time to run them grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Runs 10 tests |
| 100 | Runs 100 tests |
| 1000 | Runs 1000 tests |
Pattern observation: The time grows roughly in direct proportion to the number of tests.
Time Complexity: O(n)
This means the build time increases linearly as the number of tests increases.
[X] Wrong: "Failing early on the first test failure makes the build time constant regardless of test count."
[OK] Correct: Even if the build stops early, in the worst case all tests run before failure, so time still grows with test count.
Understanding how test execution time affects build time helps you design efficient pipelines and explain build performance clearly.
What if we parallelize running tests? How would the time complexity change?