API tests

REST API testing: key checks and CI/CD integration

A REST API is an interface through which applications exchange data using HTTP requests. In a test, you call its endpoints and check both the response and what actually changed after the request. Start with one complete scenario and prepared data, then add invalid inputs, permissions and request retries. This produces a suite that can run when the application changes and whose results the team understands.

First, establish what the API should do

A working form does not confirm that the API correctly serves another client. An integration may send an invalid value, retry a request after an outage or fetch another page of a list. A typical browser-based test may never exercise these situations. Direct API tests also help distinguish an interface defect from a problem with user interaction.

Collect the documentation, product rules and list of API clients. For each important operation, define inputs, required permissions, the expected response and the effect on data. If the documentation and implementation disagree, establish the correct behaviour with the API owner. A test should not automatically approve whatever the server currently returns.

OpenAPI can describe operations, parameters, data schemas, responses and security requirements. Use a description that matches the version under test. Schema conformance checks structure, but does not by itself confirm the correct order price or a user’s permissions. Begin coverage with operations whose failure would stop a purchase, alter someone else’s data or break an integration.

Check the meaning of methods and responses

Under HTTP semantics, GET should read a resource without requesting a change to its state. Check, for example, that fetching an order does not modify it. Normal request logging does not violate this rule. For PUT and DELETE, check idempotence: repeating an identical request should have the same intended effect as performing it once. The responses need not be identical.

PATCH has no general guarantee of idempotence. It depends on the operation: setting a value and incrementing it are different changes, for example. For POST, check processing and any retries against the design of the particular endpoint.

Check the status code together with the response body and headers. 201 indicates creation of a resource, 202 acceptance for processing without confirmation of completion, and 204 a response without content. Do not allow any successful code if the interface promises a particular one. For a JSON response, also check the agreed Content-Type, required fields, their types and values. An invalid request body or unsupported format should receive an appropriate error response.

A complete scenario from creation to cleanup

The following illustrative example assumes a test API that allows you to create an order, change its address and finally remove the test record. Adapt the paths and expectations to your interface.

  1. Prepare a test account and an available product with a known price. Choose a quantity of two units for this scenario and establish the expected amount from the product rules.
  2. Call POST /orders. In this example, expect 201, a new order identifier, the correct quantity and the correct amount. Take the identifier from the response.
  3. Call GET /orders/{id}. Compare the stored items, quantity and owner with what should have been created. The existence of the identifier is not enough.
  4. Change the address using a supported PATCH request. Fetch the order again and check the new address, along with unchanged items and amount.
  5. Remove the test record through the agreed process and check the outcome. Design cleanup to work even if an earlier assertion fails.

Distinguish fields you know in advance from values generated by the server. Do not compare a creation timestamp or a new ID against one fixed saved response. Check their format and relationship to the created resource. If the endpoint returns data later, use the waiting rules described below.

Invalid inputs and permissions need their own tests

Add a missing required field, an incorrect value type, a nonexistent product and a boundary quantity to the normal scenario. Try invalid JSON and an unsupported Content-Type too. Determine the expected code and error format from HTTP rules and the API contract. Distinguish, for example, a bad request through 400 from a conflict with the resource’s current state through 409. After rejection, check that no order or partial data change was created if the interface promises to perform the entire operation or reject it.

401 indicates that the request lacks valid authentication credentials and also requires a WWW-Authenticate header. 403 means the server understands the request but refuses to carry it out. An API can use 404 to conceal the existence of a protected resource. Set the precise expectation from the agreed interface instead of accepting all these codes interchangeably.

For permission checks, prepare two ordinary accounts and an administrator if needed. As the second user, try to fetch, modify and delete the first user’s order. Check both the rejection and that the data remains untouched. OWASP highlights permission checks for the specific object. Signing in alone is insufficient. These checks form part of security coverage and do not replace a complete security assessment.

Request retries and background processing

A connection failure can prevent a client from reading the response even though the server performed the operation. When creating a payment or order, establish whether and how the client may retry it. If the API supports an idempotency key, a key that identifies a repeated operation, check that repeating the same input with the same key produces one resulting record.

Also check reuse of the key with different input and the rules for its validity. Stripe is an example of an API with specific rules for this mechanism. Do not automatically apply its behaviour to another interface. With repeated DELETE requests, the first response may report deletion and the next the resource’s absence. A different code alone does not disprove idempotence, which concerns the effect.

For 202, also check the final processing outcome through the mechanism documented by the API. A test can repeatedly read the status until success or failure, but with a defined interval and a finite time limit. A fixed long pause does not confirm completion. When the limit expires, record the last known status and distinguish an unfinished operation from an incorrect response.

Lists and interface changes

For pagination, prepare more records than fit on one page. With a stable dataset, check the first, subsequent and last pages, completeness of the result and repeated IDs. Filtering should return only matching records, and sorting should follow the agreed order. Include records with equal values in the sort field. If data changes while it is being read, agree on completeness expectations for the pagination method in use.

When changing an API, compare required fields, data types, error responses and the meaning of values. Even a seemingly small change can affect a client that relies on the old behaviour. Contract tests help check the agreed interface between services. Schema and existing-client checks complement tests of business behaviour.

Data and tools for a repeatable suite

Each run should be able to prepare its own data. Use test accounts, synthetic data and identifiers that separate concurrent runs. A test should not wait for an order created by another test without an explicit dependency. Restrict cleanup to records belonging to the run. Record cleanup failures separately so they do not conceal the original defect.

In Python, shared setup and cleanup steps can use pytest fixtures. Bruno CLI runs file-based collections, while Playwright provides direct API calls that can be combined with browser tests. Choose a tool the team can maintain. Store files, configuration without secrets and execution instructions in Git.

CI/CD integration and interpreting failures

CI/CD is an automated process for checking changes and preparing or carrying out deployment. Short checks of critical operations can run when changes are made, with broader regression runs scheduled and performed before a release. Define which failures stop further progress and who investigates them. Supply secrets through CI secret management. Check network access, certificates, API readiness and dependency versions.

When a run fails, follow the available evidence:

Result Next check
No connection was established Environment availability, address, network and certificate.
An unexpected 401 arrived Token validity and how it was supplied.
The code matches, but the data does not Inputs, data setup, response and stored state.
The processing outcome does not arrive within the limit Last operation status and related server logs.

Preserve the scenario name, application version, expectation and actual result. If the API uses a request identifier to locate entries in logs, include it. Passwords, tokens and personal data do not belong in the report. A rerun can help diagnosis, but must not replace an explanation of the original failure.

Check that the tests can detect a defect

Run the suite from a clean environment by following the documentation, and ask a colleague to repeat the process. In a trial test, deliberately set an incorrect expectation. Both the test and CI should report a failure so a green result is not merely the consequence of incorrect configuration.

The result should be a repeatable check with a failure cause that can be traced. A passing suite confirms only the behaviour it covers, including the data and conditions used. Start with one critical scenario, check its setup, outcome and cleanup in CI, and then expand coverage.

Related topics

You might also be interested in

You can detect errors in the business logic more quickly directly through the API

Functional, integration and contract testing of REST, SOAP or GraphQL interfaces with the possibility of connection to the CI/CD pipeline.