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

Data Driven Testing in Postman

In POSTMAN, Data Driven Testing means data are getting tested from a different file such as Excel, Notepad, WordPad, etc. This technique involves creating a table of inputs that maps to expected outputs and then running those inputs through the system under test and checking whether the outputs of the tests match the outputs in the table this test technique is also described as table-driven testing or parameterized testing.


Let's imagine that We have a list of Students Emails in a Record. We know the ID of each of the Student We want to verify that each Student in the system is Record has the correct Email. We could create a table in a .csv file that looks something like this

Student Id Email

1 Sincere@april.biz

The Student IDs in this table give us a set of inputs, and the Email is the set of outputs. Data-driven testing is merely the process of feeding those inputs into the test and checking that you get the expected outputs, it would look like this:


Creating a data-driven test in Postman

Postman provides tools for running data-driven tests, but in order to use them, we will need a test here we show you how to create an actual input file and how to create the test. It will also show you how to get data from a file and use that data to compare it.

The example will use the API provided

by JSON Placeholder (http://jsonplaceholder.typicode.com).

Before creating a data-driven test, follow these steps:

1. Add a collection called JSON API.

2. Add a request to the collection called Students.

3. Set the request URL to http://jsonplaceholder.typicode.com/

users/1 and send the request. You should get back a response with a bunch of data for a user 1


Now we are going to see how to set up a Data Driven Test.


Data-driven test takes in multiple inputs, you will need some kind of variable that changes with each input is called a Parameter. In this Example we can parameterize the StudentId in the URL we can do that by turning it into a variable with the following steps:

1. Replace the 1 in the URL with a variable called {{StudentID}}.

2. To create an environment to this variable click on the Environment option.

3. Name the environment JSON API Env and create the StudentID variable with an initial value of 1, and then click the Save button to create the environment.

4. Close the dialog and then choose JSON API Env from the environment dropdown at the top right of the Postman application.

Now, if you mouse over the StudentID parameter in the URL of your request, you should see that it has a value of 1 and if you were to send the request again, you would get back the same response as you did previously.


Creating the data input

Input files for data-driven tests are often created as .csv files. Excel or another spreadsheet program, we can easily create the data in any text editor

In this example StudentID should be the header next lines matches the parameters as 1, 2,….







1.Save the word or Excel file such as StudentsData.csv.

2.We have to create a Test in the Test tab

In the Example We can set up a check to verify the email address field has an @ symbol in it

Code for the test should look like this

pm.test("Check for @ in email", function () {

var jsonData = pm.response.json();

pm.expect(jsonData.email).to.contain("@");

});

3.Open collection runner JSON API collection and clicking the Run button.

4.Browse for the Input file, Iteration and file type will be automatically updated



5.Click Run JSONAPI

In the Output all the 5 Test cases are Passed.

Comparing responses to data from a file

We need to get that data into the test assertion that does the comparison between the actual and expected values, we can set this all up with the following steps:

1.Open the StudentData.csv file and add a new column with a header as expectedEmail.

2.Add the email address to each row that corresponds to the StudentID for that row, we may need to manually call the endpoint with each StudentID in order to find out

what the correct email address is for each row, Make sure that the file is saved, it should look similar as










3.We need to check whether the email is equal to expectedEmail

code for the test should look like this:

pm.test("Validate Email Address from File", function () {

var jsonData = pm.response.json();

pm.expect(jsonData.Email).to.eql(expectedEmail);

var expectedEmail = data.expectedEmail;

})

4.We have to save all changes to the request and then open the collection runner, you can select the modified file and run the JSON API collection. We should see the results as all 5 Tests are Passed.



Hope it was Learning Data Driven Testing was Useful,

Thank You!

599 views0 comments

+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