Negative testing for APIs: send bad input on purpose

Positive tests check that the API does what it should. Negative tests check that it refuses what it shouldn't: requests without a token, requests for someone else's data, and input that's missing, malformed or absurd.

Negative tests usually find more bugs, and worse ones. A 500 on bad input is an outage waiting for a typo. A 200 on someone else's order is a data breach.

The four groups

1. Missing or bad credentials → 401

Duplicate an important request and set its Authorization to No auth. Make another with a token that's expired or simply wrong ({{token}}x).

JavaScript
pm.test('rejects missing credentials', () => pm.response.to.have.status(401));

2. Valid credentials, wrong resource → 403 or 404

This is the dangerous one, known as broken object level authorization. Log in as user B and ask for user A's things:

HTTP
GET {{baseUrl}}/orders/{{userAOrderId}}
Authorization: Bearer {{userBToken}}
JavaScript
pm.test('cannot read another user\'s order', () => {
  pm.expect(pm.response.code).to.be.oneOf([403, 404]);
});

Try it for reads, updates and deletes. An API that blocks GET but allows DELETE happens more often than you'd think.

3. Invalid input → 400 or 422

Missing fields, wrong types, wrong formats, unknown enum values. The response should name the field and the problem.

4. Limits → 400, 413 or 422

Zero, negative, huge, too long, too many items. The goal is a clean refusal, not a timeout.

One request, many bad inputs

Writing a request per bad input doesn't scale. Write the request once with variables, and put the cases in a CSV:

CSV
case,email,quantity,expectedStatus,expectedField
missing email,,1,422,email
invalid email,not-an-email,1,422,email
zero quantity,[email protected],0,422,quantity
negative quantity,[email protected],-5,422,quantity
huge quantity,[email protected],99999999,422,quantity
valid,[email protected],1,201,
Request body
{
  "email": "{{email}}",
  "quantity": {{quantity}}
}

quantity has no quotes in the body, so it's sent as a JSON number. Test a quantity sent as text in a separate request, because it changes the body's shape.

JavaScript
const expected = Number(pm.environment.get('expectedStatus'));

pm.test(`${pm.environment.get('case')}: status ${expected}`, () => {
  pm.expect(pm.response.code).to.equal(expected);
});

pm.test('error names the field', () => {
  const field = pm.environment.get('expectedField');
  if (!field) return;
  pm.expect(JSON.stringify(pm.response.json())).to.include(field);
});

Run the collection with the file, in the app (Runs → Iteration data) or from the CLI:

Terminal
requestarium run-collection "Orders API" --folder "Create order: bad input" --env Staging --data bad-orders.csv
A data-driven collection run where each row of the CSV is one iteration
Each row of the file is one iteration, labeled in the results

The test name includes the case, so a failure reads "zero quantity: status 422", not "test 4 failed". More in Data-driven API tests with CSV and JSON files.

Inputs worth trying everywhere

  • An empty body, and {}.
  • null for each field.
  • A string where a number goes, and a number where a string goes.
  • Very long strings (10,000 characters).
  • Unicode: emoji, right-to-left text, a zero-width space.
  • IDs that don't exist, IDs in the wrong format, and an ID from another tenant.
  • Dates like 2026-02-30 and 0000-00-00.

{{$randomLoremParagraph}} and the other dynamic variables help with long and random text.

A safety net for the whole collection

Whatever the input, an API should never answer with 5xx. Add that rule once: open the collection's Tests tab and add Status code, less than, 500. Every request in the collection inherits it, including all the negative tests you add later.

Read the error messages too

A 422 that says {"error": "invalid"} passes the status check but doesn't help anyone. Check that errors are consistent across the API, with the same shape, a machine-readable code and a human message. Your frontend developers will thank you.