We have already seen How to read data from a csv file in Postman.
I have not discussed a major purpose of usage of data files in Postman yet. We are going to learn about that in this post.
The major usage of reading data from files such as JSON and CSV in Postman is to achieve Data Driven Testing. Did not understand the term Data Driven Testing? Let me explain.
Suppose we have an API to login to application and gives valid token only on successful credentials and meaningful error messages on wrong credentials. You need to test below scenarios for above API:-
- Both username and password are correct.
- Both username and password are incorrect
- Only username is incorrect
- Only password is incorrect
- Username is missing
- Password is missing
To test all above scenarios for API, you have two options:-
- Create as many requests as scenarios i.e. six different requests for different request body
- Use Data driven testing i.e run single request with different set of data.
API will behave based on data you are passing. You just need to pass different set of data to same API. This we can achieve easily using data files feature in Postman.
Let’s understand this with example:-
In How to read data from a csv file in Postman ,I purposefully created just single row of data. In fact, each row is a set of input data to API. If your data file consists of five rows, API will run five times considering each row as new set of data.
I will consider the same API used in How to read data from a csv file in Postman . Now I want to do multiple booking with different data. Let’s create a sample CSV first. Each new set of data should be a row in csv.
So my csv file will look like as below:-
So the booking API will run thrice. First data row (2) will be used for first run, second data row will be used for second run and so on.
You do not need any extra setup to run. You just need to run API using collection runner and select the csv file in Data section.
Run the collection, you will see three iteration have run with different data.
Data for iteration 1:-
Data for iteration 2:-
Data for iteration 3:-