top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Data Driven Testing in Postman using single json file

As part of our Postman hackathon, we were required to have a Data driven testing framework. The main challenge for us was to use a single input data file for the whole collection, as we can add only one file in the Postman runner, and we wanted to run our whole collection in one go. Also, we wanted to make sure that our collection runs fine even without the external file as well.

In this blog, we will see how we achieved using a single json file to provide input values to a Postman Collection. To understand this blog better, readers should have prior basic knowledge of Postman, like creating a new collection with some requests and executing that collection.


Let us start by first knowing, what is Data Driven Testing?

Data Driven Testing is very significant for Test Automation. Data Driven testing framework is an Automation testing framework in which input values for a test scenario/method are read from an external source and then stored in variables. This external source can be a file (excel, xml, json, csv etc) or a database. Multiple external sources can be used at any time in a framework. Below can be achieved using Data Driven testing -

  • No hard coded input values in the test code or request body.

  • One test script can be executed multiple times for multiple scenarios with different test data. This is also referred to as Iterations. Hence, reducing the code and increasing code re-usability.

There are 2 ways for implementing Data driven testing in Postman -

  • Values from response of one request are passed to second request, hence forming a chain.

  • Use an input data file as input for Postman collection.

In this blog, we will focus on running a collection using an input json file -

  • Create a new collection in Postman. Add a new POST request to it. Here, the values in the request's json body are hard coded. In this blog, https://dummy.restapiexample.com/ is referred for testing purpose.

  • Create a new json file which we will use as our external file, as discussed earlier for data driven testing. Add the POST request's data to the json file. "name" will be the request name from the Postman collection.

  • Next step would be to read this json file into the Postman collection. Some scripting will be required to achieve this. We need to add below code to the collection's Pre-request Script tab. So, this code will be executed before each request of the collection. Collection variables will be created to store the values for all the key-value pairs from the json file on the go, with the same names. Tip - Pre-request Script tab is always a good candidate when we want to generate any dynamic variables to be sent in request, as this code will be executed before the request sent to the server.

// Load data from json file to collection variable 'requestData'
if(typeof pm.variables.get('requestData') !== 'object')
{
    pm.variables.set('requestData', pm.iterationData.toObject());
}
// Read input data from collection variable 'requestData'
const requestData = pm.variables.get('requestData');

// If 'requestData'/input data is empty
if(typeof requestData != 'object' || Object.keys(requestData).length === 0)
{
    console.log('No external data provided/data set is empty');
    return;
}

// Find current request's data
const currentRequest = requestData.requests.filter(({name}) => name === pm.info.requestName)[0];

// If no data found for current request
if(!currentRequest){
    console.log('Request ${pm.info.requestName} has no data.');
}

// If data found for current request
if(currentRequest){
    // Expose variables
    const variables = currentRequest.data.shift();

    Object.entries(variables).forEach(([key, value]) => {
        pm.variables.set(key, value);
    });
    pm.variables.set('requestData', requestData);

    // Declare next request
    if(currentRequest.data.length > 0){
        postman.setNextRequest(pm.info.requestName);
    }
}		
  • Modify POST request's body as below. We will use the collection variables as created in previous step. Please pay attention on the name of the variables, or simply copy from json file.

  • In order to run this request for multiple scenarios, we can add more test data to the input json file. This can be done as below-

  • All we have to do now, is Run the collection. To run the collection, click on ... on collection and click on 'Run Collection'. Add the test data file and click on Run. Tip - Check the 'Persist response for a session' checkbox before running the collection. This will keep the Responses visible corresponding to the requests in the Results.

  • We will have below in test results. We can see that same request is executed three times, as per the test data in json file.

  • Now, we can add as many requests in our collection, and add the respective test data required for whichever request. If we do not need test data for a request, just don't add it in the json file. Also, It will work fine if we want to run just a few requests or just a specific folder from our collection, we can do that without modifying our test data file.

  • Lastly, let us also make sure that our collection execution does not break if we miss adding the file. To achieve that, we can add the variables at collection level with the same names.

  • We can also add the error messages or validation messages to the json file, that we want to validate. These can be used in the Tests tab of the request for Response validations.

And we are done. The framework can further be customized as per the requirements.


Conclusion - We now have a Data driven testing framework for Postman, which reads a single json file for the input data. Adhering to this framework, we can run one request for multiple scenarios, which can be a combination of positives and negatives too. Also, we can make sure that there are no hard codes in our project, which is always a good practice.


Happy Learning!!






649 views1 comment

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2022 by NumPy Ninja

  • Twitter
  • LinkedIn
bottom of page