Integration testing in Rest API - Time & Space Complexity
When we run integration tests on REST APIs, we want to know how the time it takes grows as we add more tests or more API calls.
We ask: How does the total test time change when the number of API calls increases?
Analyze the time complexity of the following integration test code snippet.
for (let endpoint of endpoints) {
const response = await fetch(endpoint);
console.assert(response.status === 200);
}
This code tests multiple API endpoints one by one, checking if each returns a successful response.
Look at what repeats in the code:
- Primary operation: Sending a request to each API endpoint and checking the response.
- How many times: Once for each endpoint in the list.
As the number of endpoints grows, the total test time grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 API calls and checks |
| 100 | 100 API calls and checks |
| 1000 | 1000 API calls and checks |
Pattern observation: Doubling the number of endpoints roughly doubles the total test time.
Time Complexity: O(n)
This means the total time grows in direct proportion to the number of API endpoints tested.
[X] Wrong: "Running more tests won't affect total time much because each test is fast."
[OK] Correct: Even if each test is quick, running many tests adds up, so total time grows with the number of tests.
Understanding how test time grows helps you design efficient test suites and shows you think about real-world software quality.
"What if we ran all API calls in parallel instead of one by one? How would the time complexity change?"