Pester testing framework basics in PowerShell - Time & Space Complexity
When using Pester to run tests, it is helpful to know how the time to run tests grows as you add more tests.
We want to see how the number of tests affects the total time Pester takes to finish.
Analyze the time complexity of the following Pester test script.
Describe 'Sample Tests' {
for ($i = 1; $i -le $n; $i++) {
It "Test $i" {
$true | Should -Be $true
}
}
}
This script runs $n simple tests that always pass.
Look at what repeats when running the tests.
- Primary operation: Running each individual test block (
Itblock). - How many times: Exactly
$ntimes, once per test.
As you add more tests, the total time grows in a simple way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 test runs |
| 100 | 100 test runs |
| 1000 | 1000 test runs |
Pattern observation: The total work grows directly with the number of tests added.
Time Complexity: O(n)
This means if you double the number of tests, the total time roughly doubles too.
[X] Wrong: "Adding more tests won't affect total run time much because each test is small."
[OK] Correct: Even small tests add up, so more tests mean more total time.
Understanding how test count affects run time helps you write efficient test suites and manage testing time well.
What if each test ran multiple assertions instead of one? How would the time complexity change?