Script testing strategies in Bash Scripting - Time & Space Complexity
Testing a script often involves running it multiple times with different inputs. Understanding how the time to test grows as you add more tests helps plan your work.
We want to know: How does the testing effort increase when we add more test cases?
Analyze the time complexity of the following bash script testing loop.
#!/bin/bash
inputs=("input1" "input2" "input3" "input4")
for input in "${inputs[@]}"; do
./my_script.sh "$input"
done
This script runs another script multiple times, once for each input in the list.
Look at what repeats in the script.
- Primary operation: Running
./my_script.shfor each input. - How many times: Once per input in the
inputsarray.
Each new input adds one more run of the script.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 script runs |
| 100 | 100 script runs |
| 1000 | 1000 script runs |
Pattern observation: The total testing time grows directly with the number of inputs.
Time Complexity: O(n)
This means if you double the number of test inputs, the total testing time roughly doubles.
[X] Wrong: "Running tests in a loop is instant no matter how many inputs there are."
[OK] Correct: Each test run takes time, so more inputs mean more total time spent testing.
Knowing how testing time grows helps you plan and explain your testing approach clearly. It shows you understand how your work scales as projects grow.
"What if we ran tests in parallel instead of one after another? How would the time complexity change?"