API tests

Why hundreds of UI tests cannot replace good API tests

Use API tests when you need to check server rules without clicking through screens. Keep user interface tests, or UI tests, for behaviour the customer sees and controls. If your existing suite takes too long or fails frequently, divide its checks according to the defects they need to detect. Changing the tool alone will not solve the problem.

When a large UI suite stops helping

Suppose you check every discount variation by signing in, finding a product, adding it to the basket and going through checkout. Most steps repeat even though only the calculation rule changes. A failure while loading the basket then prevents the discount check from running, leaving you unable to tell whether the calculation itself works.

A direct API test sends a request to the server and checks the response or stored state. It skips page rendering and interaction with controls. This can reduce runtime and simplify diagnosis, but the test still depends on its environment, data and downstream services. A slow database or unstable payment integration does not disappear when you move below the UI.

Investigate intermittent failures too. Incorrect waits and fragile selectors still need fixing in the UI tests you retain. If parallel tests overwrite the same order, they need separate data at both layers.

Choose according to the rule, rather than the test name

Ask where you can reliably trigger and observe the defect. A pure price or rounding calculation often belongs in a unit test, which checks a small piece of code without a network connection. An API check then adds evidence that the server applies the right rule for a particular customer and saves the result.

API and UI describe how a test enters the application. Integration and end-to-end describe how much of the system participates. An API test might check one service with controlled dependencies or exercise the real backend, including its database and other services. The latter can be an end-to-end test of the server workflow even though it does not check the browser.

A UI test can also exercise an isolated component with a prepared server response. It is not automatically a full end-to-end test. Ham Vocke explains this distinction in Practical Test Pyramid. Our article on unit, integration and end-to-end tests covers the different scopes. Do not turn the pyramid metaphor into a mandatory ratio of tests for every project.

An example order: which layer should check what?

In this illustrative online shop, a customer buys a product, selects delivery and applies a discount code. Break down “ordering works” into specific expectations. Each needs an input and a result that lets the test determine whether it passed.

Expectation Suitable place to check What else to cover
Rounding the amount according to the agreed rule Unit test of the calculation The API applies the rule to the order
Rejecting a negative quantity and an invalid discount API test The UI shows the customer the correct error
Preventing access to another customer’s order API test with different accounts The UI does not offer an inappropriate action
Submitting the delivery option selected in the form UI test that inspects the request The server accepts and stores the value correctly
Completing checkout and showing confirmation End-to-end journey through the UI The resulting order has the correct content and state

This table proposes a division of checks, rather than a required architecture. If a rule depends on several services, its API test may need their real integration. If you only need to check how an error message looks, there is no reason to create an actual payment.

What a direct API test must check

A successful HTTP status is not enough. When creating an order, check the items, price, currency, delivery and owner against the requirements. Read the order back to check that the expected state was saved. If the server returns success but stores the wrong delivery option, the test should fail.

Add requests the form would not normally submit. A negative quantity, missing value or another customer’s identifier shows whether the server enforces rules beyond the screen. When an order is rejected, check both the response and the absence of an unwanted order or reservation. The exact status and error format come from your API contract.

Do not calculate the expected price using the same production code you are testing. Derive it from the agreed rule for the example case. Otherwise, the test may repeat the same defect and remain green. The article on REST API testing explains the technical procedure.

If order processing happens in the background, distinguish accepting the request from completing the operation. The test should wait for the agreed state within a bounded time, rather than assume an immediate result or add an arbitrary pause. Record the last observed state when it fails. Apply the same expectation to API and UI checks, or you will be comparing different customer situations.

What an API test can miss

The server can work correctly while the form submits a different delivery option from the one the customer selected. A manually constructed, correct API request will not reveal this defect. In the UI test, inspect the actual request sent by the page and the result displayed. Playwright supports monitoring browser network requests.

Keep separate checks for signing in, signing out and returning after a session expires. An API test with a prepared token does not check whether the browser obtained and used the right credentials. Sharing cookies with a test client does not replace the real user journey either.

Browser restrictions are another difference. A successful direct call does not prove that the page can use a response from another origin. The CORS protocol in the Fetch standard governs that access. Check supported browsers and cookie behaviour under the application’s deployment conditions.

The API also cannot assess keyboard navigation, the readability of an error message or form field labels. Accessibility checks belong at the interface. Combine automated checks with manual assessment, because an automated scan cannot cover every issue.

Migrate the existing suite gradually

Start with one group of repeated scenarios, such as discount variations. For each, record the rule, current assertion, necessary data and risk whose coverage would be lost if you removed the UI test. A long scenario can contain several checks, only some of which belong at a lower layer.

  1. Record the selected group’s baseline results and failures.
  2. Prepare smaller unit or API tests with the same material expectations, including negative cases.
  3. Temporarily run both the original and new checks. Investigate differing results before retiring the original test.
  4. Verify that the checks work by deliberately breaking the rule in a safe test environment.
  5. Remove only confirmed duplication and keep UI checks of data submission and the resulting display.

If migration reveals an undocumented rule, clarify it with the product owner first. Neither the original test name nor current application behaviour is sufficient evidence of the requirement. For each retired scenario, make clear which check replaces it and who maintains it.

API calls can also help the UI tests you keep

If a test checks the display of order history, you can create the previous order through the API. There is no need to go through checkout every time. If you are testing the customer’s actual creation of an order, however, do not bypass that part with an API call. The Playwright API testing documentation shows both data setup and state verification after a UI action.

Choose the authentication context deliberately when combining the two. In Playwright, page.request and browserContext.request share the browser’s cookies, while a standalone APIRequestContext created with request.newContext() has its own storage. The APIRequestContext documentation describes this behaviour. A permissions test must not accidentally use the administrator session created during data setup.

Choose a tool that fits your existing stack. Playwright supports API and browser checks in one test project. Python with pytest needs an HTTP client and your own assertions. Bruno works with requests and test scripts. Your choice of tool does not determine which risks the suite covers.

How to tell whether the migration helped

Compare the same group of rules before and after the change. Use comparable data, environments, parallel worker counts and retry settings. Include data setup and cleanup, rather than measuring the HTTP call alone. Measure both the group’s total runtime and the time needed to identify a failure’s cause.

Separate first-attempt passes, passes after a retry and persistent failures. Playwright retries distinguish these outcomes. Passing on a retry does not prove that a defect has disappeared. Track API test failures too, or migration may simply hide an environment problem. Our article on measuring flaky tests explains this further.

For diagnosis, record the expected and actual result, input and order identifier without tokens or sensitive data. A recorded Playwright trace can help with UI failures. Aim for a suite that lets the team understand what failed and what remains unchecked. Start with one repeated rule and expand the migration according to measured benefits and retained coverage.

Use the results to adjust execution in CI, the automated checks for code changes. Short, independent checks can run on pull requests, with broader journeys after deployment to the test environment. Choose according to risk and the feedback you need. Do not defer an important user journey merely because it uses a browser, or run an expensive API test more often simply because of its name.

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.