Parameter combinations, resource states and CRUD flows
Parameter combinations are covered with pairwise testing: a minimal set where every pair of values of two parameters appears at least once. It relies on most defects coming from the interaction of one or two parameters, but full enumeration is still justified for money and permissions. A resource with states gets a transition table covering allowed and forbidden transitions (usually 409), and after a refusal the state is checked to be unchanged.
API · Test design
Three sort and filter parameters give 12 combinations. How many tests do you really need?
Each field has been checked on its own, and a bug still arrives from production: GET /products?sort=rating&order=desc&inStock=true returns a list that is not sorted. Each parameter works alone, their combination breaks. The second source of such defects is time: an order was cancelled after shipping, and the money came back. Below: parameter combinations, resource states and CRUD chains.
How it works and why
Parameter combinations. The endpoint GET /products with parameters sort (price, name, rating), order (asc, desc) and inStock (true, false) has 3 × 2 × 2 = 12 combinations. The count explodes as parameters grow, and pagination and filters add more. Pairwise testing takes a minimal set in which every pair of values of two parameters appears at least once. Here 6 tests are enough instead of 12:
| # | sort | order | inStock |
|---|---|---|---|
| 1 | price | asc | true |
| 2 | price | desc | false |
| 3 | name | asc | false |
| 4 | name | desc | true |
| 5 | rating | asc | false |
| 6 | rating | desc | true |
The approach rests on an empirical observation: most defects are caused by the interaction of one or two parameters, not three or more. So the saving grows with the number of parameters (for 10 parameters with 3 values each, the full product is 59,049 combinations, while a pairwise set takes about 15–20 rows). Such a set is generated by tools (PICT, AllPairs), and dangerous combinations such as dependent parameters (from greater than to) are added by hand.
Resource states. Many resources have a life cycle: an order created → paid → shipped → delivered plus separate cancelled, refunded. It is a finite state machine: only certain transitions are allowed, and the rest must be rejected.
Order states and a forbidden transition
Build a table of transitions and check both sides: every allowed transition works and the new state is visible through GET, every forbidden one gives an error (usually 409 Conflict) and the state does not change. A separate case is repeating the same transition, because paying twice must not charge twice.
Example: a transition table in a test
Before: a test "an order can be paid" and a test "an order can be cancelled". What happens when cancelling after shipping, nobody checked. After: one table with both allowed and forbidden transitions, and the final state is checked after each.
const transitions = [
{ from: 'created', action: 'pay', status: 200, to: 'paid' },
{ from: 'paid', action: 'ship', status: 200, to: 'shipped' },
{ from: 'created', action: 'cancel', status: 200, to: 'cancelled' },
{ from: 'shipped', action: 'cancel', status: 409, to: 'shipped' },
{ from: 'cancelled', action: 'pay', status: 409, to: 'cancelled' },
{ from: 'paid', action: 'pay', status: 409, to: 'paid' }, // no double charge
];
for (const t of transitions) {
test(t.from + ' + ' + t.action, async ({ request }) => {
const id = await createOrderInState(request, t.from); // a helper
const res = await request.post('/v1/orders/' + id + '/' + t.action);
expect(res.status()).toBe(t.status);
const order = await (await request.get('/v1/orders/' + id)).json();
expect(order.status).toBe(t.to); // state after
});
}
The helper createOrderInState prepares an order in the needed state through the API (create, pay, ship) rather than reaching into the database. The row shipped + cancel must give 409 and leave shipped: that is the "a rejection did not change the state" check. Without a state check you can miss a bug where the response is 409 but the order is already cancelled.
A CRUD chain. Create, Read, Update, Read, Delete, Read (404). Each step must see the effects of the previous one.
test('CRUD chain: every step sees the previous one', async ({ request }) => {
const created = await request.post('/v1/orders', { data: { sku: 'A1', quantity: 1 } });
expect(created.status()).toBe(201);
const { id } = await created.json();
await request.put('/v1/orders/' + id, { data: { sku: 'A1', quantity: 5 } });
expect((await (await request.get('/v1/orders/' + id)).json()).quantity).toBe(5);
expect((await request.delete('/v1/orders/' + id)).status()).toBe(204);
expect((await request.get('/v1/orders/' + id)).status()).toBe(404);
expect((await request.delete('/v1/orders/' + id)).status()).toBe(404); // per spec
});
A repeated DELETE also needs a decision from the spec: 404 (the resource is already gone) or 204 (the end state is the same, since the operation is idempotent). Both are acceptable, the test pins what is promised.
Limits and alternatives
- Dependence on order and existence. An operation on a non-existent resource (
PUT /orders/999), on one already deleted, a link to a non-existent parent (POST /orderswith a non-existentcustomerId), deleting a parent that has children (cascade or409). - Parallel requests. Two simultaneous updates of one resource (the last wins, or a version check through
If-Matchfires) and a simultaneous create with the same unique value, where only one must pass:
test('two parallel creates with one unique value: only one wins', async ({ request }) => {
const data = { name: 'Olena', email: 'race-' + crypto.randomUUID() + '@test.example' };
const [a, b] = await Promise.all([
request.post('/v1/users', { data }),
request.post('/v1/users', { data }),
]);
expect([a.status(), b.status()].sort()).toEqual([201, 409]);
});
- Pairwise is not a cure-all. It does not catch a defect that appears only with three parameters at once and knows nothing about dependencies between values. For critical combinations (money, permissions) the full product is justified.
- For states with dozens of transitions, keep the table as data (an array in a file) rather than as dozens of separate tests.
Common mistakes
- Testing only the happy path of states. Forbidden transitions are the source of defects: the balance changed while the order was not cancelled.
- Not checking the state after a rejection. A 409 without checking that the resource did not change proves nothing yet.
- Preparing the state directly in the database. The test becomes tied to the schema and bypasses the business logic it was meant to check.
- One big chain for everything. When it fails on step three, the rest is unchecked and the cause is lost. Keep chains short and transitions in the table.
Check yourself
An endpoint has parameters with 4, 3 and 2 values. How many tests does the full product give, and what is the lower bound for pairwise?
Answer
The full product gives 4 × 3 × 2 = 24. For pairwise the lower bound is the product of the two largest parameters, 4 × 3 = 12, because all pairs of those two must appear. A real set may be slightly larger, the generator produces it.
A test cancels a shipped order and sees 409. What else should be checked to make the case complete?
Answer
That the order state stayed shipped (a GET after the rejection) and that side effects did not happen: no refund, the goods did not return to stock. A 409 alone does not prove the rejection was clean.
If-Match) are separate cases: that is where races hide.