
QA and Autotests, Part 2: API Tests and the Testing Pyramid
🌐 Machine-translated from Russian. Show original
In this part:
Testing pyramid#
| Level | How much | Speed | What checks |
|---|---|---|---|
| Unit tests | many | milliseconds | individual functions |
| Integration and API | average | seconds | components working together, contracts, |
| E2E through the | interface | key user scenarios |
A common mistake is the "inverted pyramid": hundreds of slow and fragile UI tests and almost no quick ones. API tests provide the best balance of benefit and stability.
What to check in the API#
- Response status : 200, 201, 400, 401, 403, 404.
- Answer scheme : fields, types, mandatory.
- Business logic: correct amount, status, balance.
- Rights: someone else's token does not receive someone else's data (403/404).
- Validation: incorrect data gives a clear error of 400, not 500.
- Idempotency: a repeated request does not create a duplicate order.
Example on Playwright (API tests)#
import { test, expect } from "@playwright/test";
test("создание заказа и запрет чужого доступа", async ({ request }) => {
const res = await request.post("/api/orders", {
headers: { Authorization: `Bearer ${process.env.TOKEN_USER_A}` },
data: { productId: "p-1", qty: 2 },
});
expect(res.status()).toBe(201);
const order = await res.json();
expect(order.qty).toBe(2);
const foreign = await request.get(`/api/orders/${order.id}`, {
headers: { Authorization: `Bearer ${process.env.TOKEN_USER_B}` },
});
expect([403, 404]).toContain(foreign.status());
});
The same can be achieved in Postman/Newman, REST Assured, pytest with requests — choose a tool for the command stack.
Test data#
- Each test generates its own data and is independent of other tests and their order.
- A separate test base, not a copy of a product with real personal data.
- Unique values (e.g., e-mail with launch number) so that tests do not conflict during parallel run.
Contract tests#
If there are several services or a mobile application, fix the API contract (OpenAPI scheme, contract tests). Then the API change that breaks the client will be detected before release.
Next — E2E interface and CI tests.
Comments · 0
No comments yet.Sign in to comment, rate and see all comments.