Generate API Test Cases Using AI: From a Requirement to a Reviewed Suite
Give the model the contract, the rule and the output format; ask for cases, not code; then review every case against its source. A worked example on the store's order status rule, and gap fill in Shift-Left API.
This is lesson 8.5 of AI and Shift-Left Testing Strategy. Lesson 8.4 set the rules for AI in a pipeline; here you apply them to the first step, writing the cases.
How to generate API test cases using AI
To generate API test cases using AI you can trust, give the model three things: the contract (the spec for the endpoint), the rule (the requirement, with its ID and exact wording) and the format you want back. Ask for test cases as data, not test code, and require every expected outcome to quote its source. Then review the cases against those sources before any of them runs. The model is drafting; the reviewer is still the oracle.
The mechanics of how generators turn a schema into cases are in the AI API testing guide and in how AI generates API tests from OpenAPI. This lesson is the working method.
What to give the model
| Input | Why it matters | Sandbox store example |
|---|---|---|
| The contract | Fields, types, limits and declared status codes, so the model doesn't guess them | The POST /api/v1/orders excerpt of /openapi/rest.json: status is an enum, notes has maxLength: 500 |
| The rule, quoted | The business intent the schema can't express | RULE O-02: status is one of pending, processing, completed, cancelled; default "pending"; "shipped", "PENDING" and "" are rejected with 400 |
| Known test data | Real IDs, so cases don't invent records | The seeded product aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa |
| The output format | Cases you can diff, review and load | JSON with an ID, the requirement ID, the request, the expected result and the quoted source |
Leave out anything the model shouldn't use as an oracle, such as responses recorded from the current implementation. If the expected result comes from what the code does today, the case can never find a defect.
A prompt that returns reviewable cases
You are drafting API test cases for a human reviewer.
Use only the sources below. Do not invent fields, limits or status codes.
Endpoint: POST /api/v1/orders (OpenAPI excerpt attached)
Requirement O-02 (quoted): "status — optional enum: pending, processing,
completed, cancelled. Default "pending". Invalid → 400: "shipped", "PENDING", ""."
Return JSON: an array of cases, each with
id, requirement_id, title, request_body, expected_status,
expected_fields, source_quote
Rules:
- One invalid input per negative case.
- Every expected value must be supported by source_quote.
- If the sources don't say what should happen, put the case in
"open_questions" instead of guessing.
A good answer looks like this (shortened):
{
"cases": [
{ "id": "O02-01", "requirement_id": "O-02", "title": "Accepts processing",
"request_body": { "status": "processing" }, "expected_status": 201,
"expected_fields": { "status": "processing" },
"source_quote": "status — optional enum: pending, processing, completed, cancelled" },
{ "id": "O02-02", "requirement_id": "O-02", "title": "Defaults to pending",
"request_body": {}, "expected_status": 201,
"expected_fields": { "status": "pending" },
"source_quote": "Default \"pending\"" },
{ "id": "O02-03", "requirement_id": "O-02", "title": "Rejects wrong case",
"request_body": { "status": "PENDING" }, "expected_status": 400,
"expected_fields": { "error": "VALIDATION_ERROR" },
"source_quote": "Invalid → 400: \"shipped\", \"PENDING\", \"\"" }
],
"open_questions": [ "Is status: null rejected or treated as the default?" ]
}
The open question is the most valuable line in the answer. It is a gap in the requirement, found before anyone codes against it.
The review checklist
Before a generated case joins the suite, check that:
- It cites a real source. The quote exists in the spec or the requirement, word for word.
- It invents nothing. No field, status code or limit that isn't in the sources. "Quantity at most 100" is a common invention: the store's rule sets no maximum.
- The expectation comes from the source, not the server. An expected 500 almost always means the model mistook a bug for behaviour.
- Each negative case breaks one thing. A request that is wrong in two ways can fail for the wrong reason and still pass.
- The data is safe. Seeded or generated records, a non-production environment, and nothing that collides with another test.
- It isn't a duplicate. Several cases often exercise the same rule with different values; keep the ones at the edges.
What AI adds, and what you shouldn't use it for
Boundaries, required fields, enum values and declared status codes are enumerable. A deterministic generator produces them the same way every time, so spending model calls on them adds variance and cost. Use the model where judgement is needed: reading prose rules that the schema can't express, choosing realistic values, spotting cross-field implications ("a cancelled order can't be set back to processing", if your rules say so), and turning a use case into an ordered journey. Typical mistakes to watch for are invented fields, invented limits and expectations copied from the current implementation.
Playwright + AI + API testing
If your team writes API tests in Playwright, keep the AI's output as data and the test as a short loop. The review happens on the JSON file, in the same pull request:
import { test, expect } from '@playwright/test';
import { readFileSync } from 'node:fs';
const { cases } = JSON.parse(readFileSync('tests/orders-o02.cases.json', 'utf8'));
for (const c of cases) {
test(`${c.id} ${c.title}`, async ({ request }) => {
const res = await request.post('https://demo.totalshiftleft.ai/api/v1/orders', {
data: c.request_body,
});
expect(res.status()).toBe(c.expected_status);
const body = await res.json();
for (const [field, value] of Object.entries(c.expected_fields)) {
const actual = field === 'error' ? body.error : body.data?.[field];
expect(actual).toBe(value);
}
});
}
The same pattern works with pytest, REST Assured or any runner that can loop over data.
In Shift-Left API: fill coverage gaps from requirements
Requirements Intelligence in Shift-Left API applies this method at project scale. Lesson 9.2 covers uploading and reviewing the requirements document; this is the step after it. Everything below is in every edition, Free included, and needs an AI provider. On a trial licence, AI gap fill and Fix with AI share a default cap of 10 calls a day.
- Find the gaps. With the store's requirements approved, open Requirements → Coverage. The per-endpoint coverage matrix shows tests per endpoint and dimension; an empty cell names the techniques that would cover it.
- Ask for candidates. Click Fill coverage gaps. The model proposes a few candidate requirements for each open gap. Each candidate passes the same quality checks as an extracted requirement, and a message reports how many were created.
- Review them. Open Review all requirements. The new candidates are AI-extracted and wait for a person: approve, edit or reject each one. Only approved requirements drive generation.
- Generate for the gaps. On an endpoint, open its coverage panel and click Generate tests for gaps. The coverage engine plans the missing tests from the spec and the approved requirements; with AI set up, it adds a few more per endpoint, and the summary shows which came from where.
The order matters: AI drafts requirements, a person approves them, and the tests come from what was approved.
Try it yourself
With the store's requirements approved in your project, open Requirements → Coverage and click Fill coverage gaps. What does the message say? Then open Review all requirements and find the new candidates: what would you approve, and what would you reject?
Recap
- To generate API test cases using AI, give the model the contract, the quoted rule and an output format, and review every case against its source.
- An API testing with AI course should teach the review as much as the prompt: no invented fields, no expectations copied from the server.
- With Playwright + AI + API testing, keep the cases as reviewed data and the test as a loop.
Next, in lesson 8.6, you test the other direction: applications that have a language model inside them.
Try it: With the sandbox store's requirements approved in your project and an AI provider set up, open Requirements → Coverage, click Fill coverage gaps, then open Review all requirements.
Frequently asked questions
Is there a free API testing with AI course?+
This track, AI and Shift-Left Testing Strategy, is free to read and covers shift-left practice, AI test generation, LLM testing and agents. The Shift-Left API in Depth track then shows each step in the product against a public sandbox API. Labs after the second lesson of a track ask for an email address.
How do Playwright + AI + API testing fit together?+
Playwright's request fixture can send API calls and assert on the responses without a browser. Use AI to draft the test cases as a data file, review that file like any other change, and let a short Playwright test loop over it. The model never writes the assertions directly into code that nobody reads.
Can you generate API test cases using AI without sending your spec to a cloud model?+
Yes, in two ways. Plan-first tools generate the enumerable cases (boundaries, required fields, declared status codes) from the spec without any model. For the cases that need a model, point the tool at one you run yourself, such as Ollama, LM Studio or vLLM, so prompts stay on your network.
Related lessons
A spec says what's valid, not what the business needs. Upload a requirements document to Shift-Left API, review what it extracts, and generate tests that trace back to each rule.
AI can design cases, explain failures and propose repairs. It should never decide a pass or quietly weaken a test. Here is the loop, step by step, with the rules and the questions to ask any tool.
Testing checks what must always be true; evaluation scores what can vary. A QA team's split of the work, a worked example, the open-source tools, and the same standard for AI-generated tests.
Read more on the blog
AI API testing explained by practitioners: how AI generates API test cases from a spec, how failing tests get repaired, how it compares with scripted API automation, concrete workflows, the tool landscape and a 30-day adoption plan.
How to generate API tests from an OpenAPI or Swagger spec: what the spec gives a generator, how to prepare it, how planning and AI enrichment work, what still needs a human, and how to review and roll out the suite.