0
0
Jenkinsdevops~5 mins

Running unit tests in pipeline in Jenkins - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Running unit tests in pipeline
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As the number of tests increases, the total time grows because each test runs separately.

Input Size (n)Approx. Operations
1010 test runs
100100 test runs
10001000 test runs

Pattern observation: The total time grows roughly in direct proportion to the number of tests.

Final Time Complexity

Time Complexity: O(n)

This means if you double the number of tests, the total time roughly doubles too.

Common Mistake

[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.

Interview Connect

Understanding how test execution time grows helps you design efficient pipelines and shows you can think about scaling tasks in automation.

Self-Check

"What if we ran all tests in parallel instead of one by one? How would the time complexity change?"