API Testing with Postman- Part 1- Setup

Now lets Write our own Basic Test.

Let write a test that check that the get request localhost:3000/books return 5 books for example. We will expand this example to touch on topics incrementally.

{“email”: “khemlall.mangal#gmail.com”,“firstName”: “khemlall.mangal#gmail.com”,“lastName”: “khemlall.mangal#gmail.com”,“householdid”: 1}
{"email": "rpersaud23@gmail.com","firstName": "rohini","lastName": "mangal","householdid": 1}

COLLECTION RUNNER

Click on ellipse → run collection

Run collection
pm.globals.set(“variable_key”, “variable_value”);
pm.globals.set(“householdId”, pm.response.json().id);
householdid now have the value
“email”: “khemlall.mangal#gmail.com”,“firstName”: “khemlall.mangal#gmail.com”,“lastName”: “khemlall.mangal#gmail.com”,“householdid”: {{householdId}}}
pm.globals.set(“firstwishlistid”, pm.response.json().wishlistId);
pm.globals.set(“secondwishlistid”, pm.response.json().wishlistId);
localhost:3000/wishlist/{{firstwishlistid}}/books/1
localhost:3000/wishlist/{{secondwishlistid}}/books/2
add second book to wishlist
localhost:3000/households/{{householdId}}/wishlistBooks
run collection

PRE-REQUEST SCRIPT

In testing you know that we need to have a setup and a teardown to basically do a set of actions before we start the test and maybe make some configuration changes etc and then tear those down. Setup data etc ….

const users=[{"email": "khemlall.mangal#gmail.com","firstName": "khemlall","lastName": "mangal"},{"email": "khemlall.mangal#gmail.com","firstName": "rohini","lastName": "mangal"},{"email": "khemlall.mangal#gmail.com","firstName": "kalawaite","lastName": "mangal"},{"email": "khemlall.mangal#gmail.com","firstName": "geta","lastName": "permanand"}];const user = _.sample(users);pm.globals.set("email", user.email);pm.globals.set("firstname", user.firstname);pm.globals.set("lastname", user.lastname);
{
"email": {{email}},
"firstName": {{firstName}},
"lastName": {{lastName}},
"householdid": {{householdId}}
}

Datafile

Download the following Datafiles for practice in this section.
https://drive.google.com/drive/folders/1ZU4ptoUzZ57LHpPEtFRik6cxmeFUcusM?usp=sharing

  1. Create a New collection called “create Books”… By now you should know how to create a collection, if you have forgotten please refer to the section above.
  2. Add a Post request to : {{host}}/books
  3. Add your Preset Header
  4. Add the following raw body:
{
"title": "{{title}}",
"author": "{{author}}",
"isbn": "{{isbn}}",
"publicationDate": "{{publicationDate}}"
}

Initialize Test Data with postman

We want to setup our data in a way to reduce duplication and intialize your test data. Instead of adding a request for every single user you want to create and have post script the number of user to create.

pm.globals,set("numberofUsers",2);pm.globals,set("currentuserCount",0);pm.globals,set("numberofWishlistAdds",4);pm.globals,set("currentwishlistCount",0);pm.globals,set("wishlistIds",[]);const getbooks ={url: 'http://${pm.environment.get("host")}/books',method: "GET",header: "G-TOKEN:ROM831ESV"}pm.sendRequest(getbooks, function(err, books){const ids = _.map(books.json(), function(book){return book.ids;});pm.globals.set("bookIds", ids)})

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Khemlall Mangal

Khemlall Mangal

I am a passionate coder, QA Engineer, and someone who enjoys the outdoors.