- In Contract Testing, we verify that the provider service adheres to the contract (interface), which is a formal agreement on the expected interactions.
- In API Testing, we validate the actual values of the API against specific test cases, often using real data and scenarios.
In Specmatic, we use matchers to assert specific values in the response. This lab teaches you how to use matchers in your API test so you can assert exact response values based on your business behavior/logic.
Real services often return a mix of stable and unstable values:
- one field may always be the same
- one field may legitimately be one of several allowed values
- one field may be generated as a fresh timestamp
- one field may follow a format without being fixed to one exact value
This lab shows how to express each of those expectations with the right matcher on the test side.
10-15 minutes.
- Docker is installed and running.
- You are in the directory
labs/quick-start-api-testing.
service/server.pyruns a small Python verification service..specmatic/repos/labs-contracts/openapi/verification/verification-api.yamldefines the contract loaded byspecmatic.yaml.specmatic.yamlpoints Specmatic at the spec and external test examples.examples/*.jsoncontains the test requests and expected responses.
.specmatic/repos/labs-contracts/openapi/verification/verification-api.yaml- OpenAPI contract for the verification service.service/server.py- Python service implementation that already satisfies the contract.examples/test_finance_user_11.json- test example you will fix usingpattern.examples/test_support_user_55.json- test example you will fix usingdataTypeandpattern.docker-compose.yaml- runs provider and contract tests.specmatic.yaml- Specmatic configuration.
- Do not edit the specs in
.specmatic/repos/labs-contracts/openapi/verification/verification-api.yaml. - Do not edit
service/server.py. - Do not edit
docker-compose.yaml. - Edit only these files:
examples/test_finance_user_11.jsonexamples/test_support_user_55.json
- Matchers: https://docs.specmatic.io/features/matchers
- Contract testing overview: https://docs.specmatic.io/documentation/contract_testing.html
Your verification service returns:
handledBy, which is alwaysverification-servicedecision, which may beapprovedorverifiedprocessedOn, which is generated at runtimereferenceCode, which follows the patternVRF-######
The service is already contract-compliant. The problem is in the test examples:
- one example is too strict about a valid enum value
- another example is too strict about a runtime timestamp and a patterned code
- another example is too strict about a runtime date and a patterned code
Run:
docker compose up api-test --build --abort-on-container-exitExpected output:
Tests run: 4, Successes: 2, Failures: 2, Errors: 0
Clean up:
docker compose down -vtest_finance_user_11.jsonexpectsdecisionto be exactlyapproved, but the service may returnapprovedorverifiedfor that request.test_support_user_55.jsonexpects one hardcoded date and one exact reference code, but the service generates fresh valid values every time.
Edit:
examples/test_finance_user_11.json
In http-response.body, change:
decisionfrom$match(exact: approved)to$match(pattern: approved|verified)
Do not change any other fields.
Run:
docker compose up api-test --build --abort-on-container-exitExpected output:
Tests run: 4, Successes: 3, Failures: 1, Errors: 0
Clean up:
docker compose down -vEdit:
examples/test_support_user_55.json
In http-response.body, change:
processedOnfrom the exact date to$match(dataType: date)referenceCodefrom the exact code to$match(pattern: VRF-[0-9]{6})
Keep handledBy and decision as exact matches.
Run:
docker compose up api-test --build --abort-on-container-exitExpected output:
Tests run: 4, Successes: 4, Failures: 0, Errors: 0
Clean up:
docker compose down -v- Baseline run shows
2failures. - After Task A, only
1failure remains. - After Task B, all
4tests pass.
- If you get a stale result after changing the test examples, rerun with
--buildand thendocker compose down -v. - If all tests pass on the first run, confirm you only edited the two allowed test files and did not save the matcher fixes already.
exactis good when one business value must stay fixed.patternis useful when a response may validly be one of several strings or must follow a format.dataTypeis useful when the exact value does not matter but the type still does.- Matchers belong in tests when you want resilient assertions against a real service.
If you are doing this lab as part of an eLearning course, return to the eLearning site and continue with the next module.
