SDET Unicorns

Data Driven Testing using JSON file in Postman

Table of Content

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."
    }
  }
]

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}}"
    }
  } 

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']);
});

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']);

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


Check out this video for more details


👩🏻‍💻 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!

My Resources

Thrive Suite: The all-in-one WordPress theme I used to create my blog.
Jasper AI: My favorite AI writing tool.
Surfer SEO:  A tool to help you rank your content on the first page of Google.

Write Content Faster

5/5

Write blog posts that rank for affiliate terms!

Join our mailing list

Unlock the Secrets to Becoming a SDET Pro in the Industry!

Stay ahead of the curve! Join our mailing list for exclusive insights, tips, and the latest industry updates delivered straight to your inbox

Table of Content

Related Post

Functional vs Regression Testing

Functional Testing vs Regression Testing: Main Differences

Testing has increasingly become a requirement in the software development lifecycle in recent years. The realization that development is no longer a focal point for stakeholders but saving costs by catching defects early in their application makes it necessary to learn different testing types.

Read More »