Writing Tests in Postman

Using postman we also can write test cases or build request using java script code. This code can use dynamic parameters, variables, or pass data between different requests. It allows us to write javascript code at two events:

  1. Before request sent to server, we call it Pre-request script and can be written in Pre-request Script
  2. After response is received from request, we call it test script. It lies under Test Script

Script can be written at Collection, Folder or request level so questions comes what will be order of execution. To understand that let’s see this picture:

Here I am describing test cases using newer PM API (pm.* API). Though Postman still supports old postman API but we should use newer one so that in case postman deprecate old API we don’t need to invest in maintenance.

In previous article I already covered logging options in Postman which can be used in both Pre Request and Test script. Use console for debugging.

Using PreRequest Script

In case request is need to required input which will be served from previous request in that case we can first check if required value is populated or not. If value is available then only request should be sent else move to next request.

As we used setNextRequest so lets discuss it in details. We can set any valid request name or null. In case of null execution will be stopped. If we pass any request name (like postman.setNextRequest(“request_name”)) then named request will be executed. There are some important facts which we need to consider before using setNextRequest.

  • setNextRequest() is always executed at the end of the current script. This means that if you put setNextRequest() before other code blocks, these blocks will still be executed.
  • setNextRequest() has a scope, which is the source of your collection run. This means that if you run a collection, you can jump to any request in the collection (even requests inside folders, using the same syntax). However, if you run a folder, the scope of setNextRequest() is limited to that folder only.

Using Test Script


This is main function used for writing the test cases. This function accepts 2 parameters, the name of the test (as a string) and a function to return a boolean value. It can be used only in the Tests tab, after the primary Postman request has been sent.


The pm.expect() assertion function was built on the shoulders of the popular JavaScript test library ChaiJS BDD. Using a similar syntax, pm.expect() makes it easy to write readable tests, and you can deal with assertions of data from a response or variables.


The pm.resonse.to.be object provides shorthands for frequently used response based checks. Using this family of assertions streamlines tests for response status types and body variations.

You can see the script result in result section under Test Results tab. In parenthesis it will show Pass Test Cases/Total count of test cases.

Postman makes the process of writing test script easier by listing commonly used snippets next to the editor. You can select the snippet you want to add and the appropriate code will be added to the test editor.

How advanced is your team when it comes to testing?

Writing tests – Most people agree that writing tests is important, but writing the first test is sometimes the biggest hurdle to testing. Once you’ve written your first test, every subsequent step becomes infinitely more manageable.

Code snippets – The first step is to use code snippets. These stubs demonstrate how a Postman test runs, how they’re structured, and show test results.

Custom tests – The next step is to write your own custom tests. Use JavaScript to address common user flows and edge cases unique to your endpoints.

Run the collection – Now it’s time to run your requests, along with their respective tests, together as a collection. There are several ways to run your tests – e.g. Postman’s collection runner, Postman’s command line tool Newman, or with a Postman scheduled monitor.

CI / CD integration – If your team is churning out code, the last thing you want to do is manually run these tests every time someone has a bug fix or feature update. When in doubt, automate!

If you haven’t progressed to Step 5, keep investing in testing.

Let’s start with understanding more about variables:

When a variable’s type is set to “string”, it ensures that Variable#get converts the value of the variable to a string before returning the data.

A boolean type of variable can either be set to true or false. Any other value set is converted to Boolean when procured from Variable#get.

A “number” type variable ensures that the value is always represented as a number. A non-number type value is returned as NaN.

A “json” type value stores JSON data format

Free-form type of a value. This is the default for any variable, unless specified otherwise. It ensures that the variable can store data in any type and no conversion is done while using Variable#get.

Here is example to verify the data type:

Here are some more examples from postman site:

Setting an environment variable

Setting a nested object as an environment variable

Getting an environment variable

Getting an environment variable (whose value is a stringified object)

Clear an environment variable

Set a global variable

Get a global variable

Clear a global variable

Get a variable

This function searches for the variable across globals and the active environment.

Check if response body contains a string

Check if response body is equal to a string

Check for a JSON value

Content-Type is present

Response time is less than 200ms

Status code is 200

Code name contains a string

Successful POST request status code

Use TinyValidator for JSON data

Another example for TinyValidator with complex JSON

TIP: You can use online tools (For ex: https://www.liquid-technologies.com/online-json-to-schema-converter) to create JSON schema from json output.

In next article we will discuss how to use collections effectively.

15 thoughts on “Writing Tests in Postman

  1. Rajendra says:

    How to pass variable data in JSON input?

  2. Satyavan Verma says:

    Pass Variable name in double quotes.

  3. Paul says:

    I have a GET request XML with parameters (no request body) which I have parameterrized and able to send the request with multiple data sets using csv file (using collection runner). But I need to save the responses for each request, but I am not sure how to do it using a script. I need to save response in XML format to local drive. Can you please help me?

  4. Ourhints says:

    Sorry for late response. This can be best accomplished within Postman’s CLI companion, Newman. You would then write a script to generate your custom reports via the following steps:

    1. Ensure that you have node and npm installed.
    2. Install newman with: npm install newman.
    Create a file with the following contents:

    Run the file with:
    node script.js # Here, script.js is the file created in the previous step

  5. jhon Smith says:

    What is this “pm”?

    pm.test(“Status code is 200”, function () {

    1. Ourhints says:

      pm is new api for PostMan. It’s same like postman in older version.For ex:


  6. Joe says:

    You show how to check for a specific JSON value.
    pm.test("Your test name", function () {
    var jsonData = pm.response.json();

    Is there a way to check for a JSON value that should match an environment variable?

  7. Ourhints says:

    You can try:

    pm.test(“Your test name”, function () {
    var jsonData = pm.response.json();

  8. Madina says:

    How to check if particular object in JSON doesnt exist for a particular data?

    My json should not contain a specific key for some types of data. How to check that obj is not present. To check for its presence i used should.not.be.null it works now i want to see that obj should not be present at all

    1. Ourhints says:

      You can try like:


  9. furtdsolinopv says:

    magnificent post, very informative. I wonder why the other experts of this sector don’t notice this. You should continue your writing. I am confident, you’ve a huge readers’ base already!

  10. Long Reichenback says:

    Hey! I know this is somewhat off topic but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having difficulty finding one? Thanks a lot!

  11. Larissa Shickendantz says:

    Nice article. Im really stumped on identifying json values in primitive json in postman. My json return response does not have a node name for every data element – they are declared in the header and then you just get values. For example in the below – I need to test if the 7’th row int he Totals node =’s a specific value. However the test calls for node[number] where it expects you to test for the 7’th total or 6’th total – rather than the 7’th line in the first total. Love if anyone who knows how to do this could let me know.

    “metadata”: {
    “columnNames”: [
    “columnTypes”: [
    “columnTotalMethods”: [
    “portfolios”: [
    “portfolioName”: “Lossless”,
    “lots”: [
    “data”: [
    “Unicorn LS”,
    “Agilent Technologies, Inc.”,
    “Unicorn LS”,
    “Agilent Technologies, Inc.”,

    1. Ourhints says:

      Not very sure about your question. Also when I validated the json posted by you it was hitting error. You can extract JSON array value and play as you wish.

      var data = JSON.parse(responseBody);
      tests [“Verify data”] = data.columnNames[6]=== “Quantity”

      Hope this helps else please reply back.

  12. Marcus Sandiford says:

    I am not certain where you are getting your information, but great topic. I must spend some time finding out more or figuring out more. Thank you for magnificent information I used to be on the lookout for this information for my mission.|

Leave a Reply

Your email address will not be published. Required fields are marked *