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?"
Practice
contract testing in REST APIs?Solution
Step 1: Understand contract testing definition
Contract testing checks if two systems agree on the data format and behavior during communication.Step 2: Compare options to definition
Only To ensure two systems agree on the data they exchange matches this purpose; others relate to speed, UI, or hardware, which are unrelated.Final Answer:
To ensure two systems agree on the data they exchange -> Option CQuick Check:
Contract testing = data agreement [OK]
- Confusing contract testing with performance testing
- Thinking contract testing checks UI or hardware
- Assuming contract testing is about security
Solution
Step 1: Identify contract test elements
Contract tests verify the API response matches the agreed format, including status codes and body content.Step 2: Match options to contract test definition
Check if the API returns the expected status code and response body matches this exactly; others relate to performance, server metrics, or UI, which are not contract tests.Final Answer:
Check if the API returns the expected status code and response body -> Option DQuick Check:
Contract test = expected response check [OK]
- Confusing contract tests with performance or UI tests
- Ignoring response body content in contract tests
- Testing server hardware instead of API contract
Solution
Step 1: Understand the contract test expectation
The contract expects a 200 status code with a specific body when requesting '/user/123'.Step 2: Analyze the provider's actual response
The provider returns 404, which does not match the expected 200 status code.Final Answer:
The test will fail because the status code doesn't match the contract -> Option BQuick Check:
Status mismatch causes test failure [OK]
- Assuming body match overrides status code mismatch
- Ignoring status code in contract tests
- Thinking response time affects contract test result
email. The test fails because the provider returns emailAddress instead. What is the best way to fix this?Solution
Step 1: Identify contract responsibility
The contract defines the expected data format; the provider must follow it.Step 2: Choose the correct fix
Changing the provider to returnemailmatches the contract, ensuring stable integration.Final Answer:
Change the provider to returnemailas expected by the contract -> Option AQuick Check:
Provider must match contract fields [OK]
- Ignoring field name mismatches
- Changing contract to fit provider without agreement
- Removing important fields from contract
id, name, and price. You want to write a contract test that allows price to be optional (sometimes missing). Which approach best achieves this?Solution
Step 1: Understand optional fields in contract testing
Contract tests can use matchers to allow fields to be optional or have flexible types.Step 2: Choose the approach that supports optional
Definingpricepriceas optional with a matcher allows the provider to omit it without failing the test.Final Answer:
Define the contract sopriceis optional using a matcher that allows missing fields -> Option AQuick Check:
Use matchers for optional fields in contracts [OK]
- Forcing optional fields to be always present
- Removing important fields from contract
- Skipping contract tests and testing only post-deployment
Ready to prove it?
Pass the test and this pattern moves unknown
