Why testing validates contracts in Rest API - Performance Analysis
When testing REST APIs, we want to see how the time to check the contract grows as the API or tests get bigger.
We ask: How does the testing effort increase when the API has more endpoints or rules?
Analyze the time complexity of the following test code snippet.
for endpoint in api_endpoints:
response = call_api(endpoint)
assert response.status == expected_status[endpoint]
assert response.body matches expected_schema[endpoint]
This code tests each API endpoint by calling it and checking if the response matches the expected contract.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each API endpoint to test it.
- How many times: Once for each endpoint in the API.
As the number of API endpoints grows, the number of tests grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls and checks |
| 100 | 100 calls and checks |
| 1000 | 1000 calls and checks |
Pattern observation: The testing work grows directly with the number of endpoints.
Time Complexity: O(n)
This means testing time grows in a straight line as the API gets bigger.
[X] Wrong: "Testing one endpoint is enough to prove the whole API works."
[OK] Correct: Each endpoint can behave differently, so testing all is needed to fully check the contract.
Understanding how testing scales with API size shows you can plan and write tests that keep quality as projects grow.
"What if we added nested loops to test multiple input cases per endpoint? How would the time complexity change?"