Postman Beginner Tutorial 13 | Data Driven Testing | How to get data from CSV and JSON files
Summary
TLDRThis tutorial by Raghav introduces data-driven testing with Postman, guiding viewers step-by-step to create API requests, parameterize them with data from CSV and JSON files, and execute data-driven API tests. It also covers referencing data in test scripts, validating JSON data, and running tests with Postman's collection runner, ensuring a comprehensive understanding of the process.
Takeaways
- 😀 The tutorial is about learning data-driven testing with Postman from scratch.
- 🔍 Raghav, the instructor, guides through the process of setting up data-driven API requests using Postman.
- 📝 The script covers how to get data from both CSV and JSON files for testing purposes.
- 💻 Postman can be accessed either through a browser or a desktop application.
- 📥 The process includes creating an API request, modifying it, and saving it in a collection for organization.
- 🔑 Variables are parameterized within the request to refer to data from external files.
- 📈 Raghav demonstrates how to create and use a CSV file with sample data for the API request.
- 📊 The tutorial also shows how to create and validate a JSON file for similar purposes.
- 🔄 Postman's Collection Runner is used to execute API requests with data from the files.
- 📋 The script explains how to refer to data from the files within test scripts using specific syntax.
- 🛠 The importance of validating JSON data for correctness is highlighted to avoid errors in testing.
Q & A
What is the main topic of the video?
-The main topic of the video is how to perform data-driven testing with Postman, starting from scratch.
What are the data sources mentioned for data-driven testing in Postman?
-The data sources mentioned for data-driven testing in Postman are CSV and JSON files.
How does one create an API request in Postman as shown in the video?
-In the video, an API request is created by clicking the plus button in Postman, selecting an API from the 'req res dot in' website, copying the endpoint, and pasting the request body as JSON.
What is the purpose of saving the API request in a collection?
-Saving the API request in a collection helps organize and manage the requests, making it easier to run multiple related requests together and maintain a clean structure.
How can you parameterize parts of the API request in Postman?
-Parts of the API request can be parameterized by using double curly brackets and the variable name, such as '{{endpoint}}', '{{email}}', and '{{password}}'.
What tool can be used to create a CSV file on a Mac system?
-On a Mac system, the Numbers application can be used to create a CSV file, which is similar to Excel.
How does the video demonstrate the use of a CSV file for data-driven testing?
-The video demonstrates the use of a CSV file by showing how to create it in Numbers, fill it with data corresponding to the API request parameters, and then use it in the Postman collection runner.
What is the significance of the '201 Created' status code mentioned in the script?
-The '201 Created' status code signifies that a new resource has been successfully created in response to the POST request in the API.
How can you verify the validity of JSON data before using it in Postman?
-You can verify the validity of JSON data by using online JSON formatters or validators, which check the syntax and structure of the JSON file.
What is the difference between using data variables in Postman directly and using them in data files?
-In Postman, using data variables directly involves referencing them with double curly brackets. However, when using data files, you must reference the data with 'data' followed by the variable name or header name in square brackets, such as 'data[email]'.
How can you run tests within the API request in Postman?
-Tests can be run within the API request in Postman by writing test scripts in the 'Tests' tab of the request. These scripts can validate the response, such as checking if the status code is '201'.
What is the importance of clearing the console before running a new collection in Postman?
-Clearing the console before running a new collection in Postman ensures that the output is clean and only displays the results of the latest run, making it easier to analyze and understand the results.
Outlines
🚀 Introduction to Data-Driven Testing with Postman
Raghav introduces the tutorial on data-driven testing with Postman, outlining the process of learning from scratch. The session includes extracting data from CSV and JSON files and executing API requests with this data. Raghav demonstrates how to create an API request in Postman, using a sample 'register user' POST request from a website, and explains how to modify the request method, URL, and body. He also shows how to save the request in a new or existing collection and how to parameterize the request using variables that will later be populated from data files.
📝 Creating and Using Data Files for API Testing
This paragraph explains how to create test data files in CSV format using a Mac application similar to Excel or by using a text editor on Windows. Raghav details the steps to input data corresponding to the variables used in Postman, such as endpoint, email, and password, and how to save the file with a '.csv' extension. He also describes how to run a collection in Postman with the data file selected, which automatically updates the iterations based on the number of data sets in the file. The importance of previewing data to ensure proper format and the steps to correct any discrepancies are also covered.
🔍 Validating and Using JSON Data Files in Postman
Raghav demonstrates how to create and validate JSON data files for use in Postman. He explains the process of manually creating a JSON file using a text editor and emphasizes the importance of validation using online tools like JSON formatters and validators. The tutorial includes a step-by-step guide on how to structure the JSON data with proper syntax and how to download or copy the validated JSON data into a file. Raghav then shows how to select this JSON file in Postman's collection runner and how it updates the iterations for data-driven testing.
📚 Referencing Data in Test Scripts and Pre-Request Scripts
The paragraph discusses how to reference data from data files within test scripts or pre-request scripts in Postman. Raghav clarifies the difference in syntax required for data references when using data files compared to variables within Postman. He provides an example of creating a test to verify the email in the response body and explains the correct syntax for accessing data from the data file during test execution. The summary includes a demonstration of running the collection with the test script and confirms that the test passes when the email from the data file matches the response.
🎓 Conclusion and Encouragement for Further Learning
In the concluding paragraph, Raghav wraps up the tutorial by summarizing the process of using data files for data-driven API testing in Postman. He encourages hands-on practice and invites questions from the audience, emphasizing the importance of continuous learning in the field. The paragraph serves as a final reminder of the value of the tutorial and an invitation for further engagement with the content.
Mindmap
Keywords
💡Data-driven testing
💡Postman
💡CSV file
💡JSON file
💡API request
💡Parameterization
💡Collection
💡Test scripts
💡Environment variables
💡Validation
💡Console
Highlights
Introduction to data-driven testing with Postman.
Learning how to get data from a CSV file for testing.
Exploring how to get data from a JSON file for testing.
Running data-driven API requests in Postman.
Referencing values from data files in test scripts.
Creating an API request in Postman.
Using sample APIs from reqres.in for demonstration.
Copying the endpoint and request body for the API request.
Saving the API request in a new or existing collection.
Creating a folder within a collection for organization.
Running the API request to check the response.
Observing response changes with different endpoint inputs.
Parameterizing the API request using variables.
Creating a CSV file for test data using a spreadsheet application.
Ensuring case sensitivity and correct variable names in the CSV file.
Saving the CSV file and preparing it for use in Postman.
Running a collection with a CSV data file in Postman.
Previewing data from the CSV file within Postman.
Addressing issues with extra columns in the CSV file.
Adding tests to check the status code in Postman.
Running the collection with tests to validate the API response.
Using a JSON file as an alternative to a CSV file for data.
Creating a JSON file with valid JSON data using a text editor.
Validating JSON data using an online formatter and validator.
Running a collection with a JSON data file in Postman.
Referring to data from data files in scripts within Postman.
Conclusion and invitation for hands-on practice and questions.
Transcripts
hello and welcome i am raghav and today
we are going to learn step by step from
scratch how can we do data driven
testing with postman so this will be
very easy and very interesting we will
learn how do we get data from a csv file
we will also see how to get data from a
json file and then we will run our data
driven api requests i will also show you
how you can refer the values from the
data files in the test scripts so let's
get started i will discuss some useful
tips with you as well during the
sessions so here step number one is we
will create our api request first so i'm
going to my postman i have opened my
postman on the browser if you like you
can continue on the browser or if you
are using a postman on your desktop you
can use that
so here uh i'll take i'll create a new
api request so i'm going to click on
this plus button and i will create let
us say i take a
i go to this req res dot in website and
i will select a api request from here
it has a lot of sample apis you can use
any of your api requests so i have this
register user which is a post request i
will click on this
and
here is the
end point this is the request body this
is the response that we get and the
response body so to get the complete url
you can click on this url and you will
get the complete url here
so i'll copy this and first i will
change the method to post
this is the url and i will go to the
body section
and click on draw
and just copy this
body here and this is a json so i will
go here and say json
and this is done and that's it i will
save
and here
i can save it in a new collection or any
of the existing collections first let me
give it a name i will say this is
register user
and here
i will go to the
let us
go to the new collection folder you can
use any collection
and i will save it
so you can see this is saved here
and i can also create a new folder
inside this new collection i can say
add folder
and i will name this folder as
data driven demo
and i'll just put this api inside this
folder this is optional i'm just doing
this so that
we have a clean structure and you can
understand it easily you can keep this
api in any collection and now this is my
api request let us run and check it i
will
click on send once you have saved your
request you can run the request and yes
you can see we are getting the response
we are also getting
the response code 200 okay and
everything is fine now one thing i have
observed here is if i give
some
if i add something to this endpoint let
us say qa
and then i run this
in this case in the response
i get 201
the status quo 201 created and then i
also get the email and the password that
i have sent in the request so if i go
back to the request body this was the
email i sent and the same email i have
received let us say if i change this
and
password
and i
save and send it again
you can see i'm getting the same email
and password
so i can use this functionality to
actually check
that i am getting the same
values in the response so this will be
easy for validation so i can use this we
have created a api request now we will
create the variables and refer in the
request so here
i can
parameterize wherever i want to refer
the data from the data files so if you
like like if you want to you can create
variables like global variables or
environment variables and def then refer
them in the request
in this case because i want to refer
them directly from the data files i will
skip the step of creating the variables
i can just refer the variables directly
within this request so let us say i want
to uh get some data for the end point
here so let us say this
this part should come
from a variable
or from a data
file
and this i will name as let us say
endpoint so this is how i do it i give
double curly brackets and then give the
variable name
so this is now parameterized similarly
let us say the email i want to get it
from the data file so i will add a
variable here as well and this i will
call as email so within double curly
brackets you give the variable name and
the same goes for
let us say password as well and
here i will give the variable name as
password
so now these three variables or these
three
data endpoint email and password should
come from a data file like a csv or a
json file so for that we will create a
csv file or a json file and add these
values so that these can be referred
from there so let us do that now
we have to go to run collection and then
refer the data file so before doing that
before going to run collection let us
first create the
files the test data files so here uh
i'm on my mac system so i can go to
xlr numbers application to create a csv
file if you are on windows you can just
open an your
excel file
or excel worksheet
add the data
and save as
dot csv extension
or the other way is you can just
open any text editor or notepad on
windows and add data
in a csv format or comma
separated format
and then save with dot csv extension
let me show you
uh
i'm here on my mac so i will open
numbers which is a application similar
to excel
and here i can create a csv file so i'm
just opening a blank file here
so this is like excel
and here
let me just remove these extra
rows and columns
i will delete this as well
yeah this is fine so this is a excel
file so here i will give the same names
as the variable names i have given which
is endpoint
so this is endpoint let me increase this
so that you can see this properly
endpoint then we have email and make
sure this is should be case sensitive so
uh make sure that
you give the same name exactly how you
have given in the
postman variables and this is
password now here i will start giving
the value so this is the header
then we will have the data rows so let
us say
for the
end point
i will say
i'll give the end point here
then email
and
password
then
again the second
data set you can add any number of data
sets
i will say
i'll give some email
and give some password
and i will save this now
here for mac i have option i can go to
file and say export to csv
in case of windows you just have to save
it and then change the extension to csv
or save it as a csv file so i will save
it as a csv file and
i will call this as
testdata1.csv
and this will go on my documents folder
so this is done
now
i will go to run collection
so here
i will go to this collection click on
these three dots and say run collection
and here i am just going to
select
the api that i want to run which is
register user and here we have the
option to select the file and once i
select the file the iterations will get
updated automatically based on the
number of data sets we have in our file
so i'll click on select file and i will
go to my documents folder and this is my
csv file i will select the file and you
can see it has updated two iterations
because there were two data sets in my
csv file also if you observe
my csv file is added here and that data
file type is updated to text csv so
there are other options as well
application json or undetermined if it
is not able to find what format is it
but it has found it is a csv format so
it has used this and we also have this
very good feature that you can preview
your data so if i click on preview you
can see we are getting all our data and
this is what you should see in a proper
format you should see these headers
which should be same as the variables we
have given and your
data here in case you do not see this in
a proper format then you must change or
you you must check your csv file and
must update the values now one thing i
am seeing here is there is an extra
column so i will have to check this i'll
go to my file and i don't know if this
is
something missing or
i think it should be fine let me just
run and check this
so
here
i will
[Music]
click on run new collection
and let us see
if it is able to
use
our variables or data from the data file
so here
looks fine you can see it is saying 201
created 201 created so this looks fine
and here it does not shows us password
fail because we do not have any tests in
our
api so let me add some tests as well i
will go back to my api go to the test
section and i will just add a test to
check status code is
201 so i'm clicking here in the snippet
and it has added this snippet and i will
just
change to 1 200 to 201
and save it
i'll go back to the collection and i
will say run again
and this time it should also run the
tests so you can see the tests are
running and everything is passed and i
can also go to the console we have
learned this in the debugging session if
i go to this console here i will clear
and run again
and we can see all the output on the
console so it has done it has done two
times if i look at the first run you can
see it has taken this api endpoint from
our data file and if i check the request
body it has taken this email and
password from there
and then
if i see the response body this is how
the response looks like and then for the
second run
this is the second run this is the
end point taken and if i check the
request body this is the request party
email and password taken from that csv
file and this is our
response so this is working fine and we
are able to
run with a csv file let us also see how
we can use a json file
now if you want to create a json file
you can use any notepad or text editor
and add your json data and then save it
as a dot json format
so here
add json data in any
text or
notepad file
and save as
dot json extension now once you do that
you should also
verify that you have a valid json data
and for that we have a lot of
options and tools online so if i search
for
json
[Music]
beautifier or validator
i can
use any of the json formatter
so let me see
if i get some options here
yeah this one is fine let me go to this
one this is json formatter and validator
and this is the url so i can directly
create a json here and then i can
download it or i can copy it in my json
file so to create a json
so i'll give a curly bracket here
start
and stop and within this curly bracket i
will add my data so let us say
the first thing i want to add is the
endpoint
i'll say endpoint within quotes
and then the value of the endpoint
so i'll just copy the value so this is
what i want
i can also just copy from here as well
then i give a comma
and then
the second value
which is
and
i give the
value of the email
then comma
and then
the third data
which is password
and the value
i'll give the value of the password and
that's it now this is one set of data if
you want one more set then you give a
comma here
so i give a comma here and just
you can copy the same thing
that is
from the start of this curly bracket
and the end of this curly bracket the
same thing
and here i can change the values
okay
and that's it so this is a valid json
and
you can also click on process to
validate it is a valid json or not and
here i am going to
download it so i will click on download
json so it has got downloaded and there
are many other tools so uh if you just
search for json
formatter
or beautifier
can find a lot of options
so let me check
if you go to google and search for json
beautifier or json formatter you should
see all these options here
let us just wait
for this to give us some options
meanwhile we have got our json file
downloaded and this you can also do the
other way like first you create a json
file and then come here and just
validate that your json is correct so
this is very important to validate that
you have got a valid json and for now
i'll go back and use the json file in
the collection runner
so i'll go here let me clear the
console logs
and i will go back to my collection
and say
run collection
and
i'm just running this api and this time
i will select a
json file so i'm going to the downloads
folder
and this is my json file
and yes everything looks fine
uh if i you can see it has
it has
updated to iterations and this is the
file and it has selected application
json in the file type and we have got a
preview so if i click on preview so
everything looks fine here i can now
say run new collection
and here
everything is running fine it has run it
two times with two sets of data and the
tests are also pass if i look at the
console you can see here
this is my request body
and then
this is the first run this is the
response body and for the second run
this is my
request party here
and you can see everything is taken from
the
json file
so this is how we can
get data from a csv or a json file now
in case you want to
run this you want to refer data from any
of these data files in scripts like a
test script or a pre-request scripts in
that case you will have to use some
different format let me show you if i go
to my api
i will
clear the console
and go back to my
api and let us say i am going to the
tests and i will create
a test
i will call it as
verify email in the response
and here
i will say
response
body
dot hash
and now in case you are referring
this from any variable within postman we
can just give it like this
but this will not work
this will not
work in case of
getting the data from a data file for
that we will have to say
data
dot
email so it will get data from the data
file or the other way is
i can say data
and within square brackets
i will give the
variable name or the header name that is
email so this is how
we use
this if we have to refer data in the
scripts this is how we do this
and let me save this
and i'll run it again just to check this
works fine
so i'll run the collection i'll go to
run collection
and i'll select the
file
you can select csv or json file
and
i will now run and check
so you can see
this is all working fine and verify
email has also
passed if i check the
console output you can see
this is the request body and here the
email is raghav at
req dot in and then if i see the
response body here as well the email is
raghav req dot in and therefore this has
passed so this is how you can refer
data
in apis using data files and i hope this
was very useful for you please do some
hands-on if you have any questions you
can let me know and i will see you in
the next session thank you for watching
and never stop learning
浏览更多相关视频
Postman Beginner Tutorial 1 | What is Postman
Always Check for the Hidden API when Web Scraping
Django Testing Tutorial with Pytest #2 - Unit Testing (2018)
Importing Development Data | Lecture 92 | Node.JS 🔥
How to import data and install packages. R programming for beginners.
What is Postman and why everyone use it ?
5.0 / 5 (0 votes)