Test Data Generation with AI, and Contract Checks in Your Tests (Part 1)
Add a Response Matches Schema check to a test from the documented response, make it fail on purpose, then ask AI for sample rows built from an API's fields and review every row before saving.
This is lesson 9.29, part 1, of Shift-Left API in Depth, the product track of the learning center. It follows mock data, drafts and the contract check. Here you check what an API really sends back, then create data to test it with. Everything in this part is in every edition. Sample rows with AI also need your own AI provider.
Can you trust test data generation with AI?
Test data generation with AI can fill a data set quickly, but a row is only useful if it fits the API. A test is also more useful when it checks more than the status code. Shift-Left API handles both in a way you can inspect: a schema check on the response, and AI rows that are checked against the fields' limits and not saved until you review them.
How do you add a contract check to a test?
A contract check is an assertion called Response Matches Schema. It compares the response body with a JSON Schema, usually the one your specification documents. The video uses a project built from the sandbox store's specification.
- Open a successful "create a user" test in the workbench and go to the Assertions tab.
- Click Add Assertion, open Type and choose Response Matches Schema.
- Open Use the documented response and choose 201. The specification's schema fills the box, and you can edit it. If nothing is documented, paste your own.
- Optionally, use Only check this part to limit the check to one part of the body, such as
data.items. - Click Run, then open the check in the results to read Expected and Actual.
A field the specification marks as nullable may be null. A schema that forbids extra fields fails on one. A rule the checker does not understand is never counted as a pass: the result says what was not checked.
What does a failed schema check look like?
To see a failure, give the check a rule the API does not meet. In the video, the schema is edited to require a phone field inside data. The run fails, and the message says that data is missing the required field phone. When there are more differences, the message says how many more.
That is how drift reads when an API changes. If the specification and the API disagree, fix whichever one is wrong. In the video the rule was only a demonstration, so it is removed with Remove, and the test passes again. Tests and mocks use the same schema checker.
What does the AI see when it suggests sample rows?
Go to Project Settings, then Data for Running Tests, then Datasets, and scroll down to Suggest sample rows with AI. You need an AI model, set up as in bring your own LLM, and permission to generate test data with AI. If either is missing, the panel tells you which one.
- From an API's fields uses the body fields of the API you choose, leaving out read-only fields.
- I'll name the columns sends only the column names, so nothing holds the rows to a type or a range.
- Choose the number of rows, from the default of 10 up to 50, and add a short note such as "customers in India".
From an API's fields, the AI receives the field names, their types and limits, and your note. It receives no values from your data.
How do you review the rows before saving?
The rows come back labelled as generated by AI, with a reminder to review every row. Each one has been checked against the same limits, so a role outside the allowed list or an age above the maximum is left out, and the panel says how many and why. You cannot edit cells here: remove a row you do not want, or ask again with a better note. AI answers vary, so read the rows rather than assuming they are right. What was sent to the AI shows exactly what the model got.
Type a name and click Save as data set. Nothing is saved before that. The data set then appears in the list as an internal table, ready for data-driven testing. Each suggestion uses one of the day's AI requests on your licence.
Try it yourself
Open a test that creates a user, add the documented 201 schema check and run it. Then ask AI for sample rows from the same API, open What was sent to the AI, remove any row you dislike and save the rest as a data set. You can use the sandbox specification at https://demo.totalshiftleft.ai/openapi/rest.json.
Recap
- Response Matches Schema checks the body, and a failure names the first field that differs.
- Start from the documented response, or paste your own schema.
- With rows from an API's fields, the AI gets names, types, limits and your note, not your data.
- Rows are checked against those limits, and nothing is saved until you click save.
Next, fill your existing tests from a data set. For more ways to build data, read how to generate test data for API testing.
Try it: Open a test for creating a user, add a Response Matches Schema check from the documented 201 response and run it. Then, in Datasets, ask AI for sample rows from the same API's fields, review them and save the ones you keep.
Frequently asked questions
What does test data generation with AI send to the model?+
In this AI test data generation step, the model gets the field names, their types and limits, and your note. No values from your data are sent. You can open What was sent to the AI to see exactly what the model received.
What does a response schema check catch that a status check misses?+
A status check only reads the status code. Response schema validation reads the body, so a missing required field or a wrong type fails the test. The failure names the first field that differs.
Do I need a paid plan for these features?+
No. Contract checks in tests and AI sample rows are in every edition. Sample rows with AI need your own AI provider, and each suggestion uses one of the day's AI requests on your licence.
Related lessons
Whatever API test cases format you use, every test case needs a request and the checks its response must pass. Open a test in the Shift-Left API test workbench, add assertions without code, and fix a failing check.
A row per case, with its inputs and its expected result: that sheet is already a test suite. Import it into Shift-Left API as a dataset, bind its columns to a test, and run it once per row.
Shift-Left API doesn't ship a model of its own; you bring one. Pick a hosted provider with your own key or a self-hosted model, test the connection, and see which model reviewed your tests.
Read more on the blog
Learn how to generate test data for API testing with practical techniques. Covers spec-driven generation, boundary value analysis, combinatorial strategies, and automated tooling.
What API schema validation is, how to validate requests and responses against an OpenAPI contract in tests, in CI and at the API gateway, the tools that do it (Ajv, Schemathesis, Prism, REST Assured, Postman), and how to catch schema drift.