0
0
Rest APIprogramming~10 mins

Why testing validates contracts in Rest API - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why testing validates contracts
Define API Contract
Write Tests Based on Contract
Run Tests Against API Implementation
Tests Pass?
NoFix Implementation
Yes
Contract Validated
Confidence in API Behavior
Testing checks if the API implementation follows the agreed contract by running tests based on that contract and confirming they pass.
Execution Sample
Rest API
GET /users/123 returns 200 with JSON {"id":123, "name":"value"}
Test: call GET /users/123
Assert status == 200
Assert response.id == 123
This test calls the API endpoint and checks if the response matches the contract.
Execution Table
StepActionTest InputExpected ResultActual ResultPass/Fail
1Send GET request to /users/123GET /users/123Status 200, JSON with id=123Status 200, JSON with id=123Pass
2Check response statusStatus 200200200Pass
3Check response body idJSON id123123Pass
4All tests passed?N/AYesYesPass
💡 All tests passed, so the API contract is validated.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
response_statusN/A200200200200
response_body.idN/A123123123123
test_passedfalsetruetruetruetrue
Key Moments - 2 Insights
Why do we check both status and response body in tests?
Because the contract includes both the status code and the data format; checking both ensures the API behaves exactly as promised (see execution_table rows 2 and 3).
What happens if a test fails?
If any test fails, it means the API does not meet the contract and needs fixing before it can be trusted (see execution_table row 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response status after Step 1?
A200
B404
C500
D123
💡 Hint
Check the 'Actual Result' column in row 1 of the execution_table.
At which step do we verify the response body id matches the contract?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in the execution_table to find when the body id is checked.
If the response status was 404 instead of 200, what would happen in the tests?
ATests would pass
BTests would skip checking body
CTests would fail
DTests would ignore status
💡 Hint
Refer to the 'Pass/Fail' column and the explanation in key_moments about failing tests.
Concept Snapshot
Testing validates API contracts by:
- Defining expected inputs and outputs
- Writing tests that check status codes and response data
- Running tests against the API
- Passing tests confirm the API meets the contract
- Failing tests show where fixes are needed
Full Transcript
Testing validates contracts by running tests that check if the API returns the expected status codes and data. The process starts by defining the contract, then writing tests based on it. When tests run, they send requests and check responses. If all tests pass, the contract is confirmed. If any test fails, the API needs fixing. This ensures the API behaves as promised and clients can trust it.