Guide For API

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

API Testing Guidelines

The tool used for writing API calls, and executing API test cases is Postman, and it's built in
tool Collection Runner. alt text
Download and install from https://www.getpostman.com/
An Intro to testing is presented here, with a lot of examples of tests you could use from the
interenet https://www.getpostman.com/docs/postman/scripts/test_scripts

Below is a detailed guide on how to do everything in order to import/export collections


and variables used in testing, how to use files, where to write tests, how to edit/add/use
the mentioned variables etc.

Importing existing collections


Use the Import button in the top left to import a test collection, and import via file, an
entire folder, link or raw text from a .json file.

Exporting existing collections


Click the three dots on the collection, click export and then select Collection v2
Importing environment and global variables
Use the cogwheel dropdown, select "Manage Environments" in the top left to open the
variables window and then click import, then select the .json file which contains the
variables.

Exporting environment and global variables


Use the cogwheel dropdown, select "Manage Environments" in the top left to open the
variables window and then click the Download Environment button to download the
desired environment set.
To download the global environments simply click the Global button then the Download as
JSON button in the next window.

Using Environment and Global Variables and .csv/.json files for testing
purposes
Postman can use environment and global variables, along with .csv or .json files for testing,
you can pass values in the call itself, the header, the call body and in the test cases you
write for your call.
An important thing to know before using all three of these is to understand the following -
if Postman encounters a variable with the same name in the file, in an environment and in
an global variable, it will prioritize it in this order:
1. File
2. Environment variable
3. Global variable

Editing and Using environment and global variables


To Add a new environment and fill it out with data, use the "Manage Environments" menu.
Click on the Add button and after that, write your desired environment name, and the key-
value pairs you wish to use. After you're done click on the Add button again. To edit an
already existing environment, simply click on it's name and then edit the key-value pairs.

Environments are changeable on the fly via the dropdown menu in the upper right corner.
alt text

They contain predefined data useful for executing individual API calls, without changing
the contents of the calls header or body. It can also be used to store data recieved from a
response, and in many other cases.
Global variables are added with the "Global" button in the picture above, the same way as
you would add environment key-value paits. Global variables are accessible at any point as
soon as they are added in the collection, and are useful for data you won't frequently
change or it's value can be applied towards a number of diferent collections/calls.

How to call the environment and global variables value in an API call
-In the API call

-In the Header

-In the Body

Note: If it's not a string, write it without quotation marks, like


"key-name": {{variable-name}}

-In the Pre-request Script Note: The Pre-request Script is executed before the actual API
call, it's useful if you want to always set a value of a variable, clear the variable, or execute
any json code block that you may requre.
Using them in Tests is described under the "Writing Tests" segment, along with how to use
csv or json files in testing.

Writing Tests
The tests are written under the "Tests" tab under the API link

The best way to explain this is simply to provide a few examples.


Here, we have written 4 tests - the first three use "jsonData".key, to check if the desired key
has the value we're looking for - In this example we are checking if the value is equal to an
environment variable that we have specified before the test/in a previous API call.
The 4th test simply checks if the response code is the one that we expect.
On the right you can use the SNIPPETS for predefined tests/other useful code and it's
generally enough to write a full test for any call.
This is how it looks in the response under "Tests"

Setting variable values within Tests


In case there is a piece of data you need to store for usage in a later test, you can do it by
writing the following code:
var jsonData = JSON.parse(responseBody);
//environment variable
postman.setEnvironmentVariable("variable-key", jsonData.response-key);
//global variable
postman.setGlobalVariable("variable-key", jsonData.response-key);

Using files in your tests


Using files to specify values in our calls or tests is only ever useful if you're going to run
multiple iterations of a test with diferent values.
The way you call these values in the API link, the Headers or the Body is the same as it is
when using an environment or global variable.
In order to use a value from a csv or json file in the pre-request script or in the Tests, use
the following example

Simply use data.columnName and you'll grab the value of that key (columnName being the
key, "open", "save" etc. being examples in the picture);
However, there is a small problem in the example above - All of our tests except one failed.

Because we didn't specify a file. This is where we should mention Postmans Collection
Runner.
The Collection Runner
Open up the collection runner by clicking on any collection and then clicking the Run
button

You will be met with this screen


If the screen is any diferent, you installed the
https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomo
p version. I would advise using the https://www.getpostman.com/ one, since it has a better
and more detailed overview.
Now, in order to run a collection of tests simply select the folder where your collection is,
or any subfolders if you want to run only the content of that folder. After that, you can
specify the environment you want to use, the number of iterations you want to run (useless
if you don't have a file to follow the test), the delay between the end of each call and the
start of the next one, what you want the reporter to list, which file you want to use and the
"Persist variables" tickbox which when left checked will return any variable value to the
value it had before the test was run, otherwise it will leave the values changed if they
changed while the tests were running.
Select your collection, your environment, and click the run button and you should be met
with a screen like this.

Let's try using a file in our next run, the csv file looks like this

language,open,claim,title,dateCreated,dueDate,assignedTo,save,complete,module
en,OPEN,CLAIM,Title,Date created,Due date,Assigned to,Save,Complete,task-
inbox
sr,OTVORI,PREUZMI,Naziv,Datum pravljenja,Datum
do,Dodeljeno,Sačuvaj,Završi,task-inbox

And it's json counterpart would look like this


[
{
"language": "en",
"open": "OPEN",
"claim": "CLAIM",
"title": "Title",
"dateCreated": "Date created",
"dueDate": "Due date",
"assignedTo": "Assigned to",
"save": "Save",
"complete": "Complete",
"module": "task-inbox"
},
{
"language": "sr",
"open": "OTVORI",
"claim": "PREUZMI",
"title": "Naziv",
"dateCreated": "Datum pravljenja",
"dueDate": "Datum do",
"assignedTo": "Dodeljeno",
"save": "Sačuvaj",
"complete": "Završi",
"module": "task-inbox"
}
]

Click the New button, and navigate to a collection that uses data in it's Tests. Select your
environment, and click on the Select File button, then select the csv or json file you wish to
use. After you selected your file, the iteration number should automatically swap to the
amount of rows it read in the file. Click the preview button to see if your values are correct,
and click the Run button.
You should be met with a screen looking like this

The test that we ran earlier that failed is now successful, because it used data from a file in
order to complete it's tests. alt text

You might also like