What does it mean?
API testing evaluates behavior through an application programming interface. A successful HTTP response is only part of the evidence: payload shape, permissions, state changes, and failure behavior must match the agreed contract.
Turn the contract into checks
Start with approved endpoint behavior: inputs, required fields, response types, status codes, and state transitions. Check representative invalid and boundary values as well as ordinary requests. A schema can validate structure without establishing business correctness, so include assertions about the meaning of important fields and the outcome of the action.
Test authorization as well as authentication
Authentication establishes identity; authorization determines what that identity may access or change. A valid login does not prove a user can only read their own records. Use isolated test identities to exercise permitted and denied object-level actions. Keep test credentials in an approved secret store or local environment and avoid logging them in traces or assertion messages.
Verify persistence and recovery
For a mutation, confirm the resulting state through an appropriate read or system observation. Cover duplicate requests and retry behavior only according to the documented API contract; idempotency is not automatic. Arrange cleanup for records created by the run, and separate product errors from unavailable environments or expired credentials. Match error status expectations to the real contract rather than assuming every service uses the same conventions.
A runnable response-contract example
The example starts a temporary HTTP server and tests a controlled order response. It demonstrates an observable assertion without using production data. Run it in a project with @playwright/test installed; create the shown spec and use npx playwright test api-contract.spec.ts.
- Start an isolated local fixture server on an available port.
- Request a known order and assert status, JSON content type, and business fields.
- Request a missing order and assert the fixture contract for not found.
- Close the server after the checks. For a real API, add role-denied and persistence checks against approved test environments.
import { test, expect } from '@playwright/test';
import { createServer, type Server } from 'node:http';
let server: Server;
let baseURL: string;
test.beforeAll(async () => {
server = createServer((request, response) => {
response.setHeader('Content-Type', 'application/json');
if (request.method === 'GET' && request.url === '/orders/order-test-1') {
response.end(JSON.stringify({ id: 'order-test-1', status: 'pending' }));
return;
}
response.statusCode = 404;
response.end(JSON.stringify({ error: 'not_found' }));
});
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
const address = server.address();
if (!address || typeof address === 'string') throw new Error('No TCP address');
baseURL = `http://127.0.0.1:${address.port}`;
});
test.afterAll(async () => {
await new Promise<void>((resolve, reject) => {
server.close((error) => error ? reject(error) : resolve());
});
});
test('returns the agreed order contract', async ({ request }) => {
const response = await request.get(`${baseURL}/orders/order-test-1`);
expect(response.status()).toBe(200);
expect(response.headers()['content-type']).toContain('application/json');
expect(await response.json()).toEqual({ id: 'order-test-1', status: 'pending' });
});
test('returns the agreed missing-order response', async ({ request }) => {
const response = await request.get(`${baseURL}/orders/missing`);
expect(response.status()).toBe(404);
expect(await response.json()).toEqual({ error: 'not_found' });
});Illustrative scenario, not a customer result or a universal coverage promise.
Practical checklist
- Approved contract and meaningful payload assertions.
- Allowed and denied identities and object access.
- Persisted outcomes, documented retry behavior, and error paths.
- Run-owned data, cleanup, safe secrets, and environment context.
Sources and further reading
Use the linked technical documentation for current framework details. Adapt these practices to your product requirements and approved environment scope.