Feb 11, 2021

API Testing 'A Beginners View': Data-Driven Testing In Postman

Hi Everyone!!! Let's check out some details about Data-Driven Testing with Postman using CSV and JSON files๐Ÿ˜Š
Data-driven testing can be a very effective approach in testing an API against different data-sets. Postman supports CSV and JSON files to get data for the test scripts. 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. 
To explain the same I’ll use a sample request which I use in my daily work.
Steps to do this:-
  1. Now, in this example, I want to refer Row_No, UCI_Number, and Entity_ID from a CSV file. So, as a pre-requisite, we need to define these in a variable. In order to do so in the Pre-request Script tab we need to set the variables as in the screenshot, so that going ahead values will be read from variables.
  2. Create a CSV file having these variables and values.
  3. Now go to Collection Runner, and import this data file and click on the Run button. Data File Preview:
After hitting on the run:
Run can be stopped any time of execution:

With JSON data file: Now, let us do the same thing with the JSON file. Create a new JSON file, and in Collection, Runner uses this JSON file.
Sample JSON File:
After a run of a JSON data file:
That’s it, and now we have some insight into the Data-Driven Testing with Postman using CSV and JSON files.
Some interesting yet important notes related to the Data-Driven approach:
Note: We can randomly retrieve data from CSV data files in a data-driven approach. 
The Approach to Note: Recently in my daily work had a scenario, wherein I had to randomly fetch data from the CSV data file and perform my test. Then found a way out to solve it. Here is what I did! The explanation is below:
  • In Pre-request I fetched the total records and used in the Test script to do my testing

    //Finding the total number of iterations that are scheduled to run since Total number of iterations === Number of rows in your CSV File

    var list = pm.info.iterationCount;

    console.log(list);

    pm.environment.set(“Total_Records”, list);

  • In Test script I checked the modulo value and did the tests as needed >> IF ($rownumber%(square root of the total number of records in file))=0 {do you test} else {postman.setNextRequest(null);}

Hope this article was interesting. Let me know if this was helpful.
Happy Learning ๐Ÿ˜‡

1 comment:

Tanay patil said...

check out our guide on blog promotion. Hopefully, you will get some more ideas to help you promote your blog.
more info