Postman test script generator

Input
Output

Postman Test Script Generator

Writing pm.expect lines for every field of a response is tedious. Paste the response and copy a script for the request's Tests (Post-response) tab: a status test and one test with an assertion for each field, ids and times by type.

Example: a user's profile

A user from GET /users/7. The script checks the status, the user's id by type and every other field exactly, with to.be.null for the null:

The response

{
  "id": 7,
  "name": "Ananya Rao",
  "email": "ananya@example.com",
  "role": "admin",
  "lastLogin": null
}

The request

https://api.example.com/users/7

Output

pm.test("Status is 200", function () {
  pm.response.to.have.status(200);
});

const body = pm.response.json();
pm.test("The body is as expected", function () {
  pm.expect(body.id).to.be.a("number");
  pm.expect(body.name).to.eql("Ananya Rao");
  pm.expect(body.email).to.eql("ananya@example.com");
  pm.expect(body.role).to.eql("admin");
  pm.expect(body.lastLogin).to.be.null;
});

How do I use the Postman test script generator?

  1. Send the request in Postman and copy the response body.
  2. Paste it here, and set the status the request should return.
  3. Copy the script into the request's Tests (Post-response) tab.

What does the Postman test script generator do?

  • pm.test for the status and the body
  • to.eql for stable values, to.be.a for ids and times
  • Runs in Bracely's API tests too, without Postman

When to use the Postman test script generator

Postman's Tests tab runs JavaScript after every response, and pm.test with pm.expect is how a collection says what a good response looks like. Those tests are what Newman, the Collection Runner and monitors report on, but writing one assertion per field takes a while.

The script has two tests: the status, then the body, with to.eql for values that stay the same, to.be.a("string") or to.be.a("number") for ids and times, to.have.lengthOf for lists and to.be.null for nulls. Bracely's API tests reads these same assertions, so a collection with this script runs in the browser too, without Postman.

Frequently asked questions

Where do I paste it?
In Postman, open the request, then the Scripts tab, then Post-response (called Tests in older versions). It runs every time you send the request.
Can it save a value for the next request?
Add a line yourself: pm.environment.set("orderId", body.id); then use {{orderId}} in the next request. API tests in Bracely understands that too.
Is my data uploaded?
No. Bracely runs entirely in your browser, so nothing you paste or open leaves your device.

More JSON tools

Bracely has 104 JSON tools in one page. Related ones:

Runs entirely in your browser. Nothing you paste is uploaded. Made by Saurabh Pagrut · About · Privacy policy
Last updated · Version 6.1 · Changelog
Saurabh Pagrut on GitHub · LinkedIn · X · YouTube · Instagram