5 / 87 · 02 AI-Augmented Test Design · Prompt Templates for Test Generation← prev⊞ allnext →☰ Read as one page
2.3Template 2: API Schema to Tests
Use this when you have an OpenAPI/Swagger schema and need endpoint tests.
Analyze the following OpenAPI 3.0 schema and generate a test suite for the
{endpoint_name} endpoint.
**Schema:**
```json
{paste OpenAPI schema excerpt}
Generate tests for:
- Every HTTP method defined on this endpoint
- Every required field -- test with missing, null, empty string, wrong type
- Every optional field -- test with present and absent
- Every enum value -- test each valid value plus one invalid
- Response codes -- test conditions that trigger each documented status code
- Auth -- test with valid token, expired token, missing token, wrong role
Framework: {test framework, e.g., pytest + httpx}
Base URL variable: {env var name, e.g., BASE_URL (from environment)}
Auth helper: {auth fixture, e.g., Use get_test_token(role="user"|"admin") fixture}
Output as a single {language} file with clear test class grouping.
### Pro Tips for This Template
1. **Paste only the relevant endpoint**, not the entire spec. If your OpenAPI file is 5000 lines, extract the 50-100 lines for the endpoint under test.
2. **Include the components/schemas section** for any `$ref` references. The LLM cannot resolve `$ref: '#/components/schemas/Product'` if you do not include the Product schema.
3. **Specify your auth fixture exactly.** If you have a helper function like `get_test_token(role="admin")`, name it in the prompt. Otherwise the LLM invents its own auth mechanism.
4. **Request parametrized tests** for enum fields to keep the output DRY:
For enum fields, use @pytest.mark.parametrize to test all valid values in a single test function, plus a separate test for one invalid value. ```