API assertions without writing code, including JSON Schema

Not everyone who tests an API wants to write JavaScript. And even people who do don't want to write pm.expect(pm.response.code).to.equal(200) for the four-hundredth time. Most checks are simple enough to be a row in a table: this part of the response, this comparison, this value.

That's what the Tests tab on a request is for.

One row per check

Each assertion picks a source, an operator and an expected value:

What you wantSourcePathOperatorExpected
The request succeedsStatus codeequals200
JSON comes backHeaderContent-Typecontainsjson
The invoice has an idJSON body$.idexists
The first item is a bookJSON body$.items[0].typeequalsbook
The id has the right prefixJSON body$.idmatches regex^inv_
The total is a numberJSON body$.totalis typenumber
It is fast enoughResponse time (ms)less than800
It isn't hugeSize (bytes)at most50000

Sources: status code, a header, the body text, a JSON path, response time in milliseconds and size in bytes.

Operators: equals, does not equal, contains, does not contain, exists, does not exist, less than, at most, greater than, at least, matches regex, is type (string, number, boolean, array, object, null) and matches JSON schema.

JSON paths look like $.a.b, a.b, $.items[0], $['odd key'] or $.items.length. Expected values can use {{variables}}, including ones the post-response script just set.

See results before you save

The Last response column shows each check against the response you last got, so you know immediately whether the assertion is right. Generate from response adds starter checks from that response: its status code, its content type, a response-time budget and its top-level JSON fields. Delete what you don't need and tighten the rest.

The Tests tab with assertions generated from the last response, all passing
Generate from response, then tighten what you need

Validate the shape with JSON Schema

Checking fields one by one gets tedious for large responses. Use matches JSON schema on the path $ instead:

JSON
{
  "type": "object",
  "required": ["id", "status", "items"],
  "properties": {
    "id": { "type": "string", "pattern": "^ord_" },
    "status": { "enum": ["open", "paid", "cancelled"] },
    "items": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": ["sku", "qty"],
        "properties": { "qty": { "type": "number", "minimum": 1 } }
      }
    }
  }
}

Supported keywords include type, required, properties, items, enum, const, minimum and maximum values, lengths and pattern. That covers most contract checks: a field was renamed, a type changed, an enum gained a value nobody told the frontend about.

Rules for the whole API, set once

A collection or folder has a Tests tab too. Its assertions apply to every HTTP and GraphQL request inside it, including subfolders. Good candidates:

  • Status code is less than 500.
  • Response time is less than 1000.
  • Header Content-Type contains json.

Inherited assertions run first (the collection's, then each folder's from the top down), then the request's own. A request's Tests tab lists the inherited ones read-only, with where each comes from and its result against the last response.

Where they run

Assertions run after the post-response script, on every Send, in the collection runner and in the CLI. A failed assertion fails the request in a collection run and shows up in JUnit and HTML reports, exactly like a pm.test. Results from scripts and from the Tests tab appear in the same list, failures first, with expected and actual values.

Terminal
requestarium run-collection "Orders API" --env Staging --junit results.xml

Code or no code?