In brief
API test automation is a set of scripts that send requests to an application's services and check the response, the database record and the side effects, without clicking through the user interface. You pick the tool by the team's language: a library in code or collections run from the command line. Success depends on data and CI, though: every test creates its own data and does not depend on order, and the pipeline runs a fast suite on every change and the full suite every night.
What to check in an API test
A test that only checks for a 200 status lets most defects through. A good API test has several layers of assertions, from the cheapest to the most valuable.
| Layer | Example for an endpoint that creates an order | What it catches |
|---|---|---|
| Status and headers | 201 and a Location header with the order URL | wrong error codes, missing redirects |
| Response structure | matches the OpenAPI description: types, required fields, date formats | renamed fields, null instead of a number |
| Business rules | gross value equals the sum of lines with VAT, discount applied above a threshold | logic errors, rounding |
| Side effects | a database record, an event on the queue, a stock reservation | data saved only in the response, missing event |
| Errors | a missing currency field gives 400 naming the field, nothing is saved | half-saved records, 500 instead of 400 |
| Authorization | user A cannot read user B's order | data leaking between customers |
Response structure is worth checking automatically against the OpenAPI specification instead of writing an assertion for every field. Agreement on expectations between teams is better left to contract tests, described in API contract testing.
Tools: how to choose
The choice of tool matters less than online comparisons suggest. What matters is whether the developers will read and fix the tests.
| Approach | Examples | When it fits |
|---|---|---|
| A library in the application's language | REST Assured for Java, pytest with an HTTP client for Python | tests live in the application repository and are written by developers and test engineers |
| An end-to-end framework with API support | Playwright, which has a built-in HTTP request context | the team already uses Playwright for UI tests and prepares data through the API |
| Collections run from the command line | Postman collections run with Newman | a quick start, testers without programming experience, simpler APIs |
Collections work well at the start. With several hundred tests you start to miss code review, shared functions and a readable change history, so many teams later move them into code. If you know there will be many tests, start in code right away.
An example Playwright test that creates an order and checks a business rule:
test("order calculates gross value", async ({ request }) => {
const res = await request.post("/api/orders", {
data: { currency: "PLN", lines: [{ sku: "A-100", qty: 2, netPrice: 50, vat: 23 }] },
});
expect(res.status()).toBe(201);
const order = await res.json();
expect(order.grossValue).toBe(123);
});Test data: the most common reason for failure
API tests rarely fail because of the tool. They fail because they share data: one test changes a customer that another one is reading, or a test assumes a specific product exists in the database and someone has deleted it. A few rules prevent most of the problems:
- Every test creates the data it needs, ideally through the same API, and assumes nothing about the state of the database.
- Unique identifiers in test data (for example a suffix with the run number), so parallel tests do not get in each other's way.
- No dependency on order. A test must pass when run alone and at any position in the suite.
- Cleanup or isolation: the test deletes its data, or each run gets a fresh database from a container.
- No production data in scripts. Personal data in tests needs a legal basis, as covered in Test data and the GDPR.
External systems (payments, carriers, registries) are replaced with stubs that respond predictably, including with errors. The test then checks your service, not the availability of someone else's.
Running tests in CI
Tests run manually "when there is time" stop being run. In the pipeline, split them into three suites:
| Suite | When | Duration | What it contains |
|---|---|---|---|
| Fast | every change on a branch and before merge | a few minutes | response structure, key rules, contracts |
| Smoke after deployment | after every deployment to an environment | 1–2 minutes | the most important endpoints, login, dependencies |
| Full | every night or before a release | up to an hour | all cases, errors, authorization, larger data |
Tests should write results in a format the CI system understands (usually JUnit XML), with the request and response body for every failure. Keep tokens and passwords for environments in CI secrets, never in the repository. Mark a flaky test and fix it or remove it, because one ignored red result teaches the team to ignore them all, as described in flaky tests.
Authorization: the test that most often finds a serious defect
The first place on the OWASP API Security Top 10 2023 list is broken object level authorization: a user swaps an identifier in the URL and gets someone else's data. Functional tests miss this because they always log in as the right user. The automated test is simple: create a resource as user A, try to read, change and delete it as user B, and check that every attempt is refused. It is worth adding such a test for every endpoint with an identifier in the URL. More on this in API security.
Once the API suite works, the next step is testing processes that run through several services, described in system integration testing and testing microservices. At B2Bnetwork we start test automation with APIs and contracts, because those tests are the fastest and most stable, and the framework and scripts stay in the client's repository.
Questions and answers
Do API tests replace user interface tests?
No, but they reduce how many you need. Business rules, validation and authorization are cheaper and more stable to check through the API. UI tests remain for a few of the most important customer journeys and for what the API cannot see: screen layout, messages, behavior in the browser. The test pyramid describes the proportions.
Postman or tests in code?
Postman with Newman gets you started quickly and works well for a smaller API or a team without developers in testing. For a large suite, code works better: change review in the repository, shared functions, refactoring and the same tools the application team uses. The deciding question is who will maintain the tests a year from now.
How long does it take to build API test automation?
A skeleton with CI runs, a report and a dozen or so tests usually takes a few weeks. Progress after that depends on the API documentation, environment availability and how hard it is to prepare data. Most of the time usually goes not into writing tests but into solving the data and dependent systems problem.
How do you test an API that depends on external systems?
In the tests that run on every change, replace the external system with a stub that returns predictable responses and errors. Check the real integration less often, in a separate suite on an environment with access to the partner's sandbox. That way a failure in someone else's environment does not block your team.