TFS Digital
All insights

QA & AI

Automated API Testing: A Practical Starting Point for Engineering Teams

Why automated API tests give fast, stable feedback, what they should cover, and how to keep them reliable in your CI pipeline.

Gurpreet Bhamra · QA & AI Solutions Lead

4 min read

When teams start automating their testing, they often reach for the browser first. UI tests are visible and easy to demo, so that feels like the natural place to begin. In my experience, though, the API layer is usually where automation pays off soonest.

This is a short, practical guide to what API tests should cover, what a simple test looks like, and how to keep a suite trustworthy as it grows.

Why API tests earn their place

Most of the behaviour that matters to a business lives behind the API: pricing rules, permissions, validation, and data being saved correctly. Testing at this layer has some clear advantages:

  • Speed. There's no browser to launch and no page to render, so you get feedback in seconds rather than minutes.
  • Stability. API tests don't depend on selectors, animations or layout changes, which are common sources of flaky UI tests.
  • Precision. When a test fails, you know which endpoint and which rule broke. You're not left guessing from a screenshot.
  • Earlier feedback. You can test an endpoint before any UI has been built for it.

This doesn't replace UI testing. It means you can keep end-to-end tests for the critical user journeys and push the detailed checks down to a layer that's cheaper to run and maintain.

What to cover

A useful API suite goes beyond checking for a 200 OK. For each important endpoint, I look at:

  1. The happy path. Valid input should return the expected status code and response body.
  2. The response shape. Fields, types and required properties should match the agreed contract. Schema validation, for example against an OpenAPI definition, catches breaking changes early.
  3. Validation and error handling. Missing fields, wrong types and out-of-range values should return a clear 4xx response, not a 500.
  4. Authentication and authorisation. Requests with no token, an expired token, or a user who lacks permission should be rejected. These tests are easy to skip and expensive to miss.
  5. Side effects. If a POST creates a record, follow up with a GET and confirm it was actually stored and is correct.
  6. Edge cases that matter to the business. Think duplicate submissions, empty lists, pagination boundaries, and anything that has caused an incident before.

A simple example

I often use Playwright for API testing, especially when a team already uses it for UI tests, because it keeps everything in one framework. Here is a minimal example that uses the built-in request fixture. It assumes baseURL is set in playwright.config.ts.

import { test, expect } from '@playwright/test';

test('creates an order and returns it', async ({ request }) => {
  const createResponse = await request.post('/api/orders', {
    data: { productId: 'abc-123', quantity: 2 },
  });
  expect(createResponse.status()).toBe(201);

  const created = await createResponse.json();
  expect(created).toMatchObject({ productId: 'abc-123', quantity: 2 });
  expect(created.id).toBeTruthy();

  const getResponse = await request.get(`/api/orders/${created.id}`);
  expect(getResponse.ok()).toBeTruthy();
});

test('rejects an order with an invalid quantity', async ({ request }) => {
  const response = await request.post('/api/orders', {
    data: { productId: 'abc-123', quantity: 0 },
  });
  expect(response.status()).toBe(400);
});

The two tests check two things. The first confirms that a valid order is created and can be read back. The second confirms that the API rejects bad input cleanly. Even this small pair covers more risk than a single happy-path check.

Keeping the suite reliable

An API suite is only useful if people trust it. A few habits make a big difference:

  • Make tests independent. Each test should create the data it needs rather than relying on another test having run first.
  • Control your test data. Use unique identifiers, set up data through the API or seed scripts, and clean up afterwards where you can.
  • Avoid fixed waits. If something is processed asynchronously, poll for the expected state with a timeout. Don't add an arbitrary sleep.
  • Keep secrets out of the code. Load tokens and environment URLs from configuration or your CI secret store.
  • Assert on what matters. Check the fields that carry business meaning. Avoid snapshotting entire responses that change for harmless reasons.

Running them in the pipeline

API tests are fast enough to run on every pull request. That's where they deliver most of their value, because they catch regressions before code is merged rather than after a release. A common setup is to run a quick smoke set against each pull request and the full suite against a shared test environment after deployment.

Where to start

If you're an engineering lead or a business owner looking at your testing, you don't need to cover everything at once. Pick the three to five endpoints that would cause the most damage if they broke. These are usually around payments, sign-up, login and core data. Write happy-path, validation and permission tests for each, then get them running in CI. Once that safety net is in place, you can extend it steadily.

If you'd like a second pair of eyes on where API testing would help most in your stack, feel free to get in touch.