Postman API testing practice: validate a resource and its missing case

Practice assertions against a live training API. You do not need a Fun API account or a secret token. These examples read sample data in your own sandbox.

Before you start

Fetch one character

Create GET {{baseUrl}}/friends/v1/characters/1. Add the script below in the post-response test editor and send the request.

pm.test('Existing resource returns JSON', () => {
  pm.response.to.have.status(200);
  pm.expect(pm.response.headers.get('Content-Type')).to.include('application/json');
  const body = pm.response.json();
  pm.expect(body.id).to.eql(1);
  pm.expect(body.name).to.be.a('string').and.not.empty;
});

Check a missing resource

Duplicate the request, change the id to 999999, and replace the tests. A 404 is the intended result of this case.

pm.test('Missing resource returns a useful error', () => {
  pm.response.to.have.status(404);
  pm.expect(pm.response.json().error).to.eql('Not Found');
});

Run the collection

Run both requests once in order. Save or export your collection so another learner can reproduce the assertions with their own sandboxId.

Expected result

Both tests pass: the existing character returns 200 with id 1 and a name; the unknown character returns 404 with an error object.

Common pitfalls

Download this practice guide · Download the practice Postman collection

Last updated