Structuring Prompts for API Contract Testing
Use an AI coding assistant to draft API contract tests, check them against the response schema, and prove the assertions catch drift.
Give an AI coding assistant the exact response contract and tell it how your test client supplies an actual response. Ask for assertions that would fail on known violations. A test that defines a fixed successful payload and then checks the same payload can exercise the assertions, but it cannot detect drift in your API.
Supply the endpoint’s actual requirements
For a fictional endpoint named GET /items/17, a successful response has status 200 and a JSON object. The contract requires an integer item_id and a string name. It permits additional fields. Keep those details in the prompt and specify the language and test runner. Include the way your test client makes the request and reads the response.
Ask the assistant to return a test for the successful endpoint, plus a separate test or fixture for invalid response shapes. Instruct it to use your existing test client and avoid replacing the response with a hard-coded success fixture in the endpoint test. Python’s standard test framework provides fixtures and assertion methods that can structure these checks Python unittest documentation.
Exercise the assertions on a known input
The example below checks a parsed payload. The fixed values exercise the assertion helper. In the endpoint test, call the helper with the status and JSON returned by the app’s test client.
def assert_item_response(status, payload):
if status != 200:
raise AssertionError(f"Expected 200, got {status}")
if type(payload.get("item_id")) is not int:
raise AssertionError("item_id must be an integer")
if not isinstance(payload.get("name"), str):
raise AssertionError("name must be a string")
assert_item_response(200, {"item_id": 17, "name": "Notebook"})
Use the precise integer check here because Python treats bool as a subclass of int. If the contract accepts additional fields, pass a payload with an extra field and confirm the helper still accepts it.
Prove that a bad response fails
Change item_id from the number 17 to the string "17". The helper should raise an assertion with the field name. Try removing name and sending status 500 with an otherwise valid payload. Both should fail for the reason you expect. If a known violation passes, correct the assertion before relying on the endpoint test.
Review how the draft handles optional and nullable fields. A missing optional field and an explicit null value can have different meanings. Match your schema rather than assuming every field is required or that null is always acceptable.
Run the test against a controlled instance
Use synthetic data and your team’s normal isolated test environment. Seed item 17, call the route through the test client, and pass the observed response to the helper. Review the generated diff and run the test along with adjacent endpoint tests. The result should tell you which contract changed if the route, status, or response shape later drifts.