Engineering API Testing Test Generation

API Test Prompt — the Contract Is the Subject

Status codes, response shapes, 401 vs 403, idempotency — API tests that test the contract, not the implementation.

Overview

API tests that only check 200-and-some-JSON miss what APIs break: the 400 that comes back as a 500, the 401/403 confusion that leaks resource existence, the error body whose shape nobody asserted. This setup generates the API contract test suite: every response asserting status code, body shape, and meaningful headers; auth as behavior (missing credentials → 401, wrong permissions → 403 — different tests); error responses held to their contract as strictly as success; idempotency of repeated requests; and payload limits enforced at the documented boundary.

How to use this resource

  1. Test the contract, not the code

    Request in, response out — the suite holds the documented behavior, whatever the implementation does inside.

  2. Separate the auth tests

    401 for who-are-you, 403 for you-can't — one test each, never merged.

  3. Repeat the dangerous requests

    The idempotency scenario: send the completed request again and assert the documented behavior, not luck.

Why This Works

  • Contract framing tests what consumers depend on instead of implementation details
  • The 401/403 distinction encodes the auth correctness most suites blur
  • Error-shape assertions protect the clients that parse failures programmatically

Best for

  • Public and partner-facing endpoints
  • APIs whose error contract is part of the product
  • Teams that learned the 401-vs-403 lesson in production

Not for

  • Writing the API documentation — that's the Markdown Output Builder
  • Defining the response format itself — that's the JSON Output Prompt Builder

Use cases

  • Testing REST endpoints before they go public
  • Catching the invalid payload that 500s instead of 400s
  • Asserting error response shapes that clients parse

FAQ

Why does this prompt insist 401 and 403 be separate tests instead of one auth check?

They assert different behavior, so the prompt writes one test each: 401 covers missing, expired, or malformed credentials, while 403 covers a valid caller whose role is forbidden. Merging them hides the case where a wrong-permission request leaks whether the resource even exists, and a single failure message would no longer tell you which auth rule broke.

How do I make the generated tests catch a 400 that comes back as a 500?

The FAILURE SCENARIOS block already targets this: 'Invalid payloads return 400 with the documented error shape — not 500,' so a swallowed 500 fails the test instead of passing quietly. Paste the endpoint into CODE CONTEXT with its documented error body, and the generated tests assert both the status code and that error shape.

How does the idempotency scenario in this prompt actually get tested?

The FAILURE SCENARIOS marker 'a duplicate of a completed request behaves as specified' becomes at least one test, but 'as specified' is only as good as what you supply. Define your endpoint's idempotency contract in CODE CONTEXT — same response, no second charge, a 409, whatever applies — and the prompt writes the repeat-request assertion around it.

Do the generated API tests prove my endpoint's contract is correct once they pass?

Not on their own. This resource produces a test-writing prompt via the Test Case Prompt Generator; you run it in ChatGPT, Claude, or Gemini to get a runnable test file, then run that file in CI. Passing tests confirm only the assertions they hold — check the prompt's ASSUMPTIONS and GAPS output for unspecified behavior it flagged rather than covered.

More resources from Test Case Prompt Generator

Resources that pair well

Related tools

Guides for this resource

Tip: Save time by exploring related resources and tools that integrate with this resource.