Writing Automated API Tests Using Postman For Beginners: Step 1 — The Basics & Making Our First GET Request

Faz540
6 min readFeb 10, 2018

--

Introduction:

Postman is a testing tool that I’ve used since 2014 and it has played a huge part in my testing career (hence my avatar being Postman Pat).

I remember when I first started using Postman how infuriating it was to use a tool that didn’t have beginner-friendly documentation. It was also practically non-existent on YouTube.

Although I’ve slowly moved away from Postman as a testing tool to manage my own API tests through pure JavaScript, I still always recommend it to testers that want to start writing code.

So, let’s start writing some code!

Before We Start:

First of all, download and install Postman:

https://www.getpostman.com

Once installed, feel free to create an account or just skip straight to the app.

Postman comes with a Dark Theme. To select it, click the ‘Wrench’ icon > Settings > ‘Themes’ tab and click the Dark Theme. Close the window to continue.

There are many public REST APIs you can use such as the Star Wars API (SWAPI), the Pokemon API (PokéApi) and many others.

In this article I’ll be using the PokéApi.

Our First API Test:

In our first API test, we will send a GET request to return us some data. We will then write basic JavaScript to confirm that the data returned is correct.

Enter the following request URL in the Request URL field:

https://pokeapi.co/api/v2/type

1 — Request URL

To send the GET request, click ‘Send’ or you can press CTRL + Enter (assuming you’re on Windows)

Once sent, you should be returned all the different ‘Types’ of Pokémon.

1 — Response for Request URL

You can switch between showing the Pretty’ or Raw version of the data returned by clicking their respective buttons.

1 — Raw Response for Request URL

Now let’s write our first test!

Click the ‘Tests’ tab underneath the Request URL field:

Copy and Paste the below code into the ‘Test’ window , don’t worry I’ll explain what all of this means throughout the article:

var body = JSON.parse(responseBody);
tests["At least 1 Pokemon Type Is Returned"] = body.count > 0;

Variables — The first thing we did was create a new variable called body.

Variables are used as a way to store data.

For example, let’s say I have $20 in my bank account.

var totalMoneyInBankAccount = 20;

Then a friend finally pays you back for that coffee you bought them last month.

var totalMoneyInBankAccount = 20;
var coffeePayment = 5;
totalMoneyInBankAccount = totalMoneyInBankAccount + coffeePayment;

Our totalMoneyInBankAccount variable has had its value changed to $25.

The last line of code declares that the totalMoneyInBankAccount equals its original value + 5.

Now, back to the test

JSON.Parse() — This is a function that takes a raw set of JSON data and allows us to interact with it. The parsed/usable version of the raw response has been assigned to a variable called ‘body’.

tests["At least 1 Pokemon Type Is Returned"] = body.count > 0;

The above is the ‘legacy’ way of writing Postman tests, I’ll show the ‘new’ way of writing tests towards the end of the article. I personally find the ‘new’ way intimidating for beginners…

To write our first test in Postman, we start by simply writing the word ‘tests’ followed by a pair of square brackets.

tests[]

Inside the square brackets, we write the name of the test inside quotes (single or double quotes, it doesn’t matter). You can write absolutely anything in here.

tests["At least 1 Pokemon Type Is Returned"]

Next, you want to have a single equals sign after the final square bracket.

tests["At least 1 Pokemon Type Is Returned"] =

After that, you want to enter the thing you’re actually testing. In my case, I want to look at the ‘count’ property and confirm that its value is above 0.

tests["At least 1 Pokemon Type Is Returned"] = body.count > 0;

You may be asking, where am I getting ‘body.count’ from? I’m giving Postman the ‘path’ to the ‘count’ property to tell it what I’m testing against.

For example, let’s say I have a text file called ‘phoneNumbers.txt’ on my computer and it’s inside a folder called ‘myDocs’.

phoneNumbers.txt

If I right click on the file and click ‘Properties’ I can see the ‘path’ to that file.

Path to phoneNumbers.txt

You’re first telling the computer to go into the C drive, then into the ‘Users’ folder, then into the ‘Public’ folder, then the ‘Documents’ folder, and finally the ‘myDocs’ folder.

This is the same concept when we’re referring to data in JSON. It’s all just parent > child > grand child > great grand child etc. relationship.

{  "firstName": "Paul",  "lastName": "Farrell"  "eyeColour": "Blue"
}

To refer to my eye colour, assuming we’ve ‘parsed’ the above JSON to ‘body’ like before, it would be:

body.eyeColour

Make sense? Good — Back to the test! (we’ve almost finished)

tests["At least 1 Pokemon Type Is Returned"] = body.count > 0;

So we’ve told Postman we’re targeting the count property inside the ‘body’ and we are using a ‘>’ (greater than) symbol. Meaning, we expect the value of ‘body.count’ to be greater than 0.

Now, send the request and click the ‘Test Results’ tab below.

Our first test result

As you can see, this test has passed. Let’s make it fail to confirm our test is working correctly.

Response

So we know the value of the ‘count’ property is 20. Let’s modify the test so it expects the value to be greater than 100.

Modified Test

Send the request again and the test should fail.

Failed Test

As you can see the test has failed and there’s an error displayed.

‘Expected false to be truthy’ is Postman’s way of saying the value returned isn’t what you expected it to be.

Let’s wrap this article up by converting the test into the ‘new’ way of writing tests:

var body = JSON.parse(responseBody);
pm.test("At least 1 Pokemon Type Is Returned", function () {
pm.expect(body.count).to.be.greaterThan(1);
});

Would you have kept reading this article if I shown you that test syntax first?

The above is the new syntax to write tests, but don’t worry — The older/easier way will still be supported for a long time.

I could go into a lot more detail but this article is long enough.

Stay tuned for more Beginner-Friendly Postman articles.

--

--