“Data-Driven Testing in Postman” is when we have one test that we run multiple times with different data variables.
Data-driven Testing is nothing but getting testing data from a different file. Data can be stored in different data storage applications such as Excel, notepad, Word pad, etc. Data is not generally stored in a single format but in different formats.
Data-driven testing can be done in two different ways.
1)Use the data from a file.
2)Use the data from a different request-response.
The data stored in the file can be used for different applications such as CSV, JSON, HTML, etc. To get the data from a different file, first, we have to create variables and then the data stored in the file can be referred to.
Let’s see an example how data driven testing can be done using Postman
Url used :http://www.dummy.restapiexample.com/
Creating an employee record dynamically in Data Driven Testing Collection in Postman
Create a new collection in Postman and give the name of the collection as Data Driven testing
Select POST request and give the url as https://dummy.restapiexample.com/api/v1/create
Click on body with the data as given below
{"name":"test",
"salary":"123",
"age":"23"}
Click on Save in Data Driven Collection and
Click Send
Output Shows that one employee is created dynamically with id:6763
200 OK message as displayed
Status as success as shown below
How to add Validations in Tests?
Go to Tests and click
Click on body and give the following validations as
To check the status code we give
tests["check status code"]=responsecode.code==200
To check if the status value is success we give
tests["check status in the response"]=responseBody.has("success")
In the above line it searches if it finds a value success in status code
Write the java script as given which gets the whole body of the response
var response = JSON.parse(responseBody);
To check the status as success in the response body we give
response.status=="success"
To test the validation point in the response in exact position we give as
tests["check status in the response in exact position"]=response.status=="success"
Click on headers in the response at the bottom as shown in the screenshot above.
The information generated in headers will be generated by the server automatically.
Let’s see some of the validations in Headers:
You can verify any header part for which you have to write a simple java script function as given below to capture the content Type:
pm.test("check content-Type header",function()
{
pm.response.to.be.header("Content-Type","application/json")
});
Instead of requesting multiple requests for multiple sets of data. We can create a test data for 1 request with multiple sets and can use a data driven approach to import the data from the file.
Create multiple records by importing from .csv or json files:
In Postman we can import the test data in .csv format or json format as shown below
Save the test data in .csv format
Save the test data in JSON format
Change the body of the existing data as shown above to import the test data with respective variables.
Click on three dots right side of the Data Driven Testing collection and
Click Run Collection
Open Runner
Click Run manually
Give the iterations of how many records you want to display as 3
Give the Delay as time taken to display the records as 5
In data select the json format file first to import and
Click preview to see if the json format imported is correctly aligned as shown below.
Click on Run Data Driven Testing button
Then it displays the result as the number of test cases passed as we have given only 3 iterations and executed the first 3 records. It takes 5ms time for each iteration and the file is in JSON format file as shown below.
Similarly, if you want to import the .csv file in the postman in the Data Driven Collection for the test data.
Write the test data in excel sheet and save the sheet with .csv extension and
Select the given .csv file as shown and give the required iterations as 3 and delay time as 5ms as per example and click on preview to see if the data is chosen correctly and
In the Data input field see if the .csv file is selected correctly.
Click Preview
Below is the screenshot for the preview pop up to view if the data is aligned correctly as required.
Click on Run Data Driven Testing.
If you see in the above example. The result will be displayed 3 test cases passed with the three records executed as required as we have given 3 iterations only.
How to run single collection(which includes multiple requests) with multiple test cases:
Click on the three dots beside the new Collection you have which has multiple requests.
Click on run collection option
Check if all the requests in the collection are selected as shown above.
Select the number of iterations as 5 and delay time as 5ms as required for each iteration.
Click on Run New Collection.
The above screenshot shows the results of 1 iteration with 4 requests or 5 test cases which have been executed at the same time.
Similarly, the 2 iteration executed with 5 multiple requests.
Similarly, the 3 iteration executed with 5 multiple requests.
Similarly, iteration 4 executed with 5 multiple requests.
Similarly, iteration 5 executed with 5 multiple requests as shown above.
Conclusion:
Instead of using multiple sets of data requests again and again using the resources and its time consuming process we can use the data driven approach through which we can use the test data which can execute multiple requests(test cases) at once saves time and effort. Similarly, we can also execute a collection at once in Postman.
The data-driven approach is useful when we need to execute a test with multiple sets of Data. Also, modifying or adding any data fields will just need updating the data files which is easier than going to test script and updating test data.