QA Interview Hub · Knowledge base · Quizzes · Українська

API › Checklist: what to test in an endpoint

Positive and negative cases

A positive case proves the endpoint does what it promises on valid data, and a negative case proves it rejects invalid data correctly. There are usually more negative cases, because each field can be broken in several ways: missing, null, empty, wrong type, out of range. Boundaries are tested in pairs, on the edge and just beyond it, and the status is checked exactly rather than as any 4xx.

API · Checklist

A sign-up form accepts age 17 but rejects 18. Which set of cases would have found it?

The endpoint POST /v1/users requires an age from 18 to 120. The developer wrote the condition age > 18 instead of age >= 18, so eighteen-year-old users now get rejected. A test with age 30 is green. Only a case on the boundary finds the bug, and for such cases not to depend on inspiration you need a list to walk through for every new endpoint.

Below: what goes into positive and negative cases, why there are usually more negative ones, and how to write them down in a table so that a new case costs one row.

How it is built

A positive case proves that the endpoint does what it promises when the data is right. A negative case proves that the endpoint rejects correctly when the data is wrong. For every negative request there are two questions: which exact status is expected, and whether the error has a form a client can use.

Positive cases:

  • a valid request gives the right status, schema and field values;
  • the record really was saved: a repeated GET or a database query;
  • only the required fields and all fields together: two different sets;
  • pagination, filters, sorting; an empty result is [], not an error;
  • response time within the SLA.

Negative cases:

  • a required field is missing, the value is null, an empty string;
  • a wrong type: a string instead of a number, an array instead of an object;
  • boundaries: 0, minimum minus one, the minimum, the maximum, maximum plus one, very long strings;
  • invalid JSON and a wrong Content-Type (400 and 415);
  • a non-existent id (404), an unsupported method (405);
  • a duplicate of a unique value (409);
  • in every case a 4xx with a clear message and no 500.

What positive and negative cases prove

Positive cases

They prove: the endpoint does what it promises. A few sets (minimal, full), a check that the data was saved. They find logic errors and response format errors.

Negative cases

They prove: the endpoint rejects correctly and safely. There are more of them because wrong inputs are endless. They find validation errors, boundary errors and 500 instead of 4xx.

Figure 1. The two kinds of cases complement each other. Neither replaces the other.

Why there are more negative cases. There are usually one or two right inputs and endless wrong ones: each field can be broken in several ways (missing, null, empty, wrong type, out of range, special characters). Besides, validation often has "one step aside" errors such as > instead of >=, and they show only on the boundaries.

Example: a table of cases and a bug on the boundary

Before: one test with age 30. After: a table where each row describes one input and the expected status. Adding a case means adding a row.

const mail = () => 'u' + crypto.randomUUID().slice(0, 8) + '@shop.test';

const cases = [
  { name: 'age 18 (minimum)', body: { email: mail(), age: 18 }, status: 201 },
  { name: 'age 120 (maximum)', body: { email: mail(), age: 120 }, status: 201 },
  { name: 'age 17', body: { email: mail(), age: 17 }, status: 422 },
  { name: 'age 121', body: { email: mail(), age: 121 }, status: 422 },
  { name: 'age as a string', body: { email: mail(), age: '30' }, status: 422 },
  { name: 'email is null', body: { email: null, age: 30 }, status: 422 },
  { name: 'email missing', body: { age: 30 }, status: 422 },
  { name: 'email is empty', body: { email: '', age: 30 }, status: 422 },
];

for (const c of cases) {
  test('POST /v1/users: ' + c.name, async ({ request }) => {
    const res = await request.post('/v1/users', { data: c.body });
    expect(res.status()).toBe(c.status);
    if (c.status === 422) {
      expect(res.headers()['content-type']).toContain('json');
      expect(await res.text()).not.toMatch(/Exception|SELECT /);
    }
  });
}

The negative rows also check the error format: a JSON body without an exception or SQL mention. Suppose the row "age 18 (minimum)" fails with 422 instead of 201: the condition on the boundary is written too strictly. A test with age 30 would never have seen that.

Cases that do not fit a table with a body (broken JSON, a duplicate) are written as separate tests:

test('malformed JSON gives 400, not 500', async ({ request }) => {
  const res = await request.post('/v1/users', {
    headers: { 'Content-Type': 'application/json' },
    data: Buffer.from('{"email": "a@shop.test", "age": '),
  });
  expect(res.status()).toBe(400);
});

test('duplicate email gives 409', async ({ request }) => {
  const body = { email: 'dup-' + crypto.randomUUID().slice(0, 8) + '@shop.test', age: 30 };
  expect((await request.post('/v1/users', { data: body })).status()).toBe(201);
  expect((await request.post('/v1/users', { data: body })).status()).toBe(409);
});

In the first test data is a Buffer: Playwright sends such bytes as is, and the server receives truncated JSON. With a JSON Content-Type, a string that does not parse as JSON is JSON-encoded, and the server would receive valid JSON. In the second, the first request is enough to create a record, and the second one with the same email must give 409. The unique suffix makes the test repeatable.

Limits and alternatives

  • The table does not replace equivalence class analysis: first the input is split into classes, then values on the boundaries of each are taken. That is the topic of a separate test-design chapter.
  • The exact status for each case comes from the contract. If one team returns 400 and another 422 for the same error, that is a question for API design, not for the test.
  • Field combinations (two fields, each valid alone but not together) are only partly covered by a "one input per row" table: they are added as separate rows.
  • Property-based tools (fast-check, Schemathesis) generate wrong inputs themselves. They complement a table well but do not know which code is right for a business rule.

Common mistakes

  • Checking "any 4xx". A test status >= 400 lets a swap of 422 for 500 or of 404 for 403 through, so the status is checked exactly.
  • Skipping boundaries. The minimum and the maximum are checked, and so are the values next to them: just outside and just inside.
  • Not checking the error body. A valid 4xx with a stack trace or HTML breaks the client and reveals details.
  • Shared data between cases. A duplicate needs a record known in advance, so each case creates its own data and does not depend on the run order.

Check yourself

The field quantity accepts integers from 1 to 10. Which values do you check on the boundaries and why these?

Answer

0, 1, 10 and 11. 1 and 10 must pass, 0 and 11 must be rejected: a "one step aside" error (> instead of >=) shows up exactly on these four values. Additionally minus one, a fractional value (1.5) and a string: those are other classes of wrong input.

A negative case returns 500 with the text of an exception. The test checked only status >= 400 and was green. What is wrong?

Answer

The right behaviour here is a 4xx (the client sent bad data), while 500 means an unexpected server error and often a leak of details. A check for "any 4xx" does not distinguish it from the needed 422 or 400, so the status must be checked exactly and the body checked for the absence of exceptions.

In an interview. Junior: First the positive cases: status, schema, values and a check that it was saved through GET. Then the negative ones: missing fields, wrong types, boundaries, invalid JSON, a non-existent id, duplicates. In every case I expect a 4xx with a clear message, not a 500. Middle/Senior: There are more negative cases because there are more wrong inputs, and they find validation errors. I keep the cases in a table: input, the exact expected status, a check of the error format. I take boundaries in pairs, just outside and on the boundary, because a > instead of >= shows up only there. For field combinations and wrong inputs I did not foresee I add generation from the schema.

More articles in this chapter