Contract testing in Rest API - Time & Space Complexity
When we test contracts between services in a REST API, we want to see how the time to verify these contracts changes as the number of API calls grows.
We ask: How does the testing time grow when we add more API endpoints or more test cases?
Analyze the time complexity of the following contract testing code snippet.
for (const endpoint of apiEndpoints) {
const contract = getContractFor(endpoint);
const response = callApi(endpoint);
verifyContract(contract, response);
}
This code tests each API endpoint by fetching its contract, calling the API, and verifying the response matches the contract.
- Primary operation: Looping through each API endpoint to test its contract.
- How many times: Once for every endpoint in the list.
As the number of API endpoints increases, the total testing time grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 contract checks |
| 100 | 100 contract checks |
| 1000 | 1000 contract checks |
Pattern observation: Doubling the number of endpoints doubles the work.
Time Complexity: O(n)
This means the testing time grows directly in proportion to the number of API endpoints tested.
[X] Wrong: "Testing one endpoint means testing all endpoints at once, so time stays the same no matter how many endpoints there are."
[OK] Correct: Each endpoint requires its own call and check, so more endpoints mean more work and more time.
Understanding how testing time grows with the number of API endpoints helps you design efficient tests and explain your reasoning clearly in interviews.
"What if we added nested contract checks inside each endpoint test? How would the time complexity change?"