pm.test and pm.expect: an API test assertion cheat sheet

Most API tests are a handful of the same checks: status code, a header, a field in the body, the length of a list. This is a cheat sheet of those checks in the pm.test / pm.expect style that Postman made popular. Everything here runs in Requestarium's script runtime, in the app, in collection runs and in the CLI.

Put these in a request's post-response script (the Scripts tab).

A response with the Tests tab showing three passed tests
Each pm.test shows up as one result next to the response

The shape of a test

JavaScript
pm.test('name that says what broke', () => {
  pm.expect(actual).to.equal(expected);
});

A test passes when the function runs without a failed expect. Each pm.test shows up as one test in the app, in reports and as one test case in JUnit.

Status code

JavaScript
pm.test('returns 200', () => {
  pm.expect(pm.response.code).to.equal(200);
});

// Postman's response assertions work too
pm.test('returns 201', () => {
  pm.response.to.have.status(201);
});

pm.test('is a success', () => {
  pm.expect(pm.response.code).to.be.within(200, 299);
});

pm.test('is one of the accepted codes', () => {
  pm.expect(pm.response.code).to.be.oneOf([200, 204]);
});

Headers

JavaScript
pm.test('returns JSON', () => {
  pm.expect(pm.response.headers.get('Content-Type')).to.include('application/json');
});

pm.test('sets a cache header', () => {
  pm.response.to.have.header('Cache-Control');
});

JSON body: values

JavaScript
const body = pm.response.json();

pm.test('returns the right user', () => {
  pm.expect(body.id).to.equal(42);
  pm.expect(body.email).to.include('@');
});

pm.test('role is admin or owner', () => {
  pm.expect(body.role).to.be.oneOf(['admin', 'owner']);
});

pm.test('email matches the pattern', () => {
  pm.expect(body.email).to.match(/^[^@]+@example\.com$/);
});

JSON body: structure

JavaScript
pm.test('has the expected fields', () => {
  pm.expect(body).to.have.property('id');
  pm.expect(body).to.have.keys('id', 'email', 'role');
});

pm.test('nested field (a dotted path walks into objects)', () => {
  pm.expect(body).to.have.property('address.country', 'DE');
});

pm.test('deep equality', () => {
  pm.expect(body.tags).to.eql(['vip', 'beta']);
});

Types

JavaScript
pm.test('types are right', () => {
  pm.expect(body.id).to.be.a('number');
  pm.expect(body.email).to.be.a('string');
  pm.expect(body.items).to.be.an('array');
  pm.expect(body.deletedAt).to.be.null;
});

Arrays

JavaScript
pm.test('returns a page of 20', () => {
  pm.expect(body.items).to.have.lengthOf(20);
});

pm.test('list is not empty', () => {
  pm.expect(body.items).to.not.be.empty;
});

pm.test('includes the new order', () => {
  pm.expect(body.items.map((o) => o.id)).to.include(pm.environment.get('orderId'));
});

pm.test('every item is open', () => {
  body.items.forEach((o) => pm.expect(o.status).to.equal('open'));
});

Numbers

JavaScript
pm.test('total is positive', () => {
  pm.expect(body.total).to.be.above(0);
  pm.expect(body.discount).to.be.at.most(body.total);
});

Negation

Any check can be flipped with .not:

JavaScript
pm.test('no internal fields leak', () => {
  pm.expect(body).to.not.have.property('passwordHash');
});

Error responses

JavaScript
pm.test('rejects a bad id with a useful error', () => {
  pm.expect(pm.response.code).to.equal(400);
  pm.expect(pm.response.json().error).to.include('id');
});

Save values for later requests

Not a test, but it lives in the same script:

JavaScript
pm.environment.set('orderId', body.id);

See How to chain API requests.

Prefer no code?

The request's Tests tab covers most of these checks as rows, without writing a script: status, header, JSON path, response time, size, regex, type and JSON schema. See API assertions without writing code.

Supported assertions

The runtime supports the chain words Postman scripts use (to, be, have, not, deep, nested) and these assertions: equal, eql, include, a/an, ok, true, false, null, undefined, empty, exist, above, below, least, most, within, property, lengthOf, match, oneOf and keys, plus pm.response.to.have.status(), .header() and .to.be.ok.

More in the scripts reference.