DEV Community

Cover image for Data Driven Testing using JSON file in Postman
Dilpreet Johal
Dilpreet Johal

Posted on

Data Driven Testing using JSON file in Postman

In this blog post, we’ll look at how to use a JSON file in Postman to conduct data-driven testing. This method is useful when testing a single endpoint with many sets of data. We will also look at how to access nested data in JSON using pre-requisite scripts.

Prerequisites:

  • Basic knowledge of JavaScript and Postman.
  • a JSON data file that you can use as the test’s source.

Prepare your JSON data file

To get started, gather all the data you’ll need for your tests into a single JSON data file. Here’s an example of the JSON data:

[
  {
    "email": "user1@example.com",
    "roles": ["admin", "user"],
    "profile": {
      "name": "User 1",
      "address": "123 Main St."
    }
  },
  {
    "email": "user2@example.com",
    "roles": ["user"],
    "profile": {
      "name": "User 2",
      "address": "456 Main St."
    }
  }
]
Enter fullscreen mode Exit fullscreen mode

Create a new Postman request

Once the data is ready, you can create your Postman request for your endpoint. Replace the values in the request body with variables, like this:

  {
    "email": "{{email}}",
    "roles": ["{{roles}}"],
    "profile": {
      "name": "{{name}}",
      "address": "{{address}}"
    }
  } 
Enter fullscreen mode Exit fullscreen mode

Setup Tests

Before you run the request, it’s important to set up your tests to ensure you receive the expected results. Here’s an example:

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

pm.test("Verify data", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.email).to.eql(data['email']);
    pm.expect(jsonData.profile.name).to.eql(data['profile']['name']);
    pm.expect(jsonData.profile.address).to.eql(data['profile']['address']);
    pm.expect(jsonData.profile.company).to.eql(data['profile']['company']);
});
Enter fullscreen mode Exit fullscreen mode

Run Collection

To organize your requests, add the request to a collection in Postman and then import the JSON file. Postman will automatically map the data for you. Once that’s done, hit the ‘Collection Run’ button to see the results.

You’ll notice that the nested JSON data didn’t get mapped properly as it’s not supported by Postman at the moment. To handle this issue, you’ll need to do custom mapping in the pre-request script, like this:

Handling Nested JSON Data in Postman

One issue that you may encounter while working with Postman is the mapping of nested JSON data. Unfortunately, this feature is not supported in Postman at the moment. But, don’t worry! You can easily handle it by writing a custom mapping in the pre-request script.

Here’s an example of how you can do this:

pm.collectionVariables.set("name", data["profile"]['name']);
pm.collectionVariables.set("address", data["profile"]['address']);
pm.collectionVariables.set("company", data["profile"]['company']);
Enter fullscreen mode Exit fullscreen mode

By following these steps, you can ensure that your tests run correctly and produce the expected results.


Check out the video below for a detailed explanation


👩🏻‍💻 Unleash Your Full Potential and Take Your Career to the Next Level with SDET-U Academy👇🏻
Join Academy

📧 Subscribe to my mailing list to get access to more content like this as well as be part of amazing free giveaways.

👍 You can follow my content here as well -

...

Thanks for reading!

Top comments (1)

Collapse
 
Sloan, the sloth mascot
Comment deleted