Contract testing helps make sure two parts of a system agree on how they talk to each other. It checks that the data sent and received matches what both sides expect.
Contract testing in Rest API
contract_test {
provider: "API Service",
consumer: "Frontend App",
interactions: [
{
request: {
method: "GET",
path: "/users/123"
},
response: {
status: 200,
body: {
id: 123,
name: "Alice"
}
}
}
]
}This is a simplified example of a contract test definition.
It describes what the consumer expects from the provider for a specific request.
contract_test {
provider: "Order API",
consumer: "Mobile App",
interactions: [
{
request: { method: "POST", path: "/orders" },
response: { status: 201, body: { orderId: 789 } }
}
]
}contract_test {
provider: "User Service",
consumer: "Web Client",
interactions: [
{
request: { method: "DELETE", path: "/users/456" },
response: { status: 204 }
}
]
}This simple Python program tests if the API returns the expected user data for user 123. It checks the status code and required fields.
import requests def test_get_user_contract(): url = "https://api.example.com/users/123" response = requests.get(url) assert response.status_code == 200 data = response.json() assert "id" in data and data["id"] == 123 assert "name" in data if __name__ == "__main__": test_get_user_contract() print("Contract test passed!")
Contract tests focus on the interface, not the internal logic of services.
They help catch mismatches early, saving time and bugs in production.
Use contract testing alongside other tests for best results.
Contract testing ensures two systems agree on data exchange.
It is useful when different teams or services communicate.
It helps catch errors early and keeps integrations stable.
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
