loading...
Cover image for Beginners guide to writing Postman API Tests

Beginners guide to writing Postman API Tests

clonne101 profile image Jeffery Osei ・5 min read

Hello there 🤓, today I want to give you a gist on a tool I have been using both at work and in my personal projects for testing the APIs I write.

Postman, this to me is by far one of the best tools for running API tests and automation if needed, I have been on this tool for more than a year now.

In the beginning I used it as any other REST API client, just add in my requests and see a positive response come in but there was so much I could have achieved if only I knew this.

So today I would like you to join me on a short adventure of writing a simple API test with Postman, let's go!.



STEPS 🪜



💻 Install Postman :

Installing the Postman app is really easy, visit this page and obtain your platform version (Mac, Windows, Linux).



🍕 Add Environment Variable :

Environment variables in Postman aids you when setting up your request, a good example is setting your API url for re-use in other places. To do this, Open your installed Postman app and click on the Manage Environment button at the top right corner of the window as seen below.

Manage Environment

You will be greeted with a popup window. At the bottom right corner, click on the Add button as seen below.

Add Environment Variable

In the next view you will find a section to enter your environment name, set your variable name, add an initial value and lastly a current value, find an example below.

📝 To follow this test, you can set your initial value and current value to the URL below.

http://play.jefferyclonne.com/code/postman-training/index.php?limit=0
Enter fullscreen mode Exit fullscreen mode

Set Environment Variables

  • Initial value:
    This is the value shared to your team or publicly when you decide to share your request with others.

  • Current value:
    This is the value used when Postman is making requests using your environment, it is an ephemeral value that does not get saved, only exist locally.

Now Add and exit out of the window, check the Environments drop-down to make sure yours is selected, as seen below.

Select Environment



🧱 Add Test Request :

If you are using the test URL above, follow this setup. In your Postman window click on the + button as shown below.

Add New Request

Now in the address bar enter your environment variable {{url}} and click on the Send button to make sure the API is reachable, as seen below.

Test Request



🎄 Add Test Cases :

Now that we have everything setup (wheew! 😅), lets start writing our test cases! 🤩.

In your Postman window select the Tests tab to get started as seen below.

Tests Tab



Now lets write our cases:

  • 1. Test status response is 200: In this test we try to check if the response from the API has a status of 200, meaning everything is OK.

♻️ Add the following code and click on the Send button to run this test, now in the Response section click on the Test Results tab to check if your test case passed, as seen below.

pm.test("Test status response is 200", function () {
    pm.response.to.have.status(200);
});
Enter fullscreen mode Exit fullscreen mode

Test One Pass



  • 2. Test response output status: From our API response we have a status variable, now lets check to make sure its always 0 as expected.

♻️ Add the following code and click on the Send button to run this test, now in the Response section click on the Test Results tab to check if your test case passed, as seen below.

pm.test("Test response output status", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.status).to.equal(0);
});
Enter fullscreen mode Exit fullscreen mode

Test Two Pass



  • 3. Test response output message: From our API response we have a message variable, now lets check to make sure its always Success as expected.

♻️ Add the following code and click on the Send button to run this test, now in the Response section click on the Test Results tab to check if your test case passed, as seen below.

pm.test("Test response output message", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.message).to.equal("Success");
});
Enter fullscreen mode Exit fullscreen mode

Test Three Pass



  • 4. Test response output to be an array: From our API response we have an output variable, now lets check to make sure its always an array as expected.

♻️ Add the following code and click on the Send button to run this test, now in the Response section click on the Test Results tab to check if your test case passed, as seen below.

pm.test("Test response output to be an array", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.output).to.be.an("array");
});
Enter fullscreen mode Exit fullscreen mode

Test Four Pass



  • 5. Test response output to be an array which is not empty: From our API response we have an output variable, now lets check to make sure its not empty as expected.

♻️ Add the following code and click on the Send button to run this test, now in the Response section click on the Test Results tab to check if your test case passed, as seen below.

pm.test("Test response output to be an array which is not empty", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.output).to.not.be.empty
});
Enter fullscreen mode Exit fullscreen mode

Test Five Pass



  • 6. Test response output object to have expected keys: From our API response we have an output variable and this holds some data, now lets check to make sure it has the minimum expected data for the first object.

♻️ Add the following code and click on the Send button to run this test, now in the Response section click on the Test Results tab to check if your test case passed, as seen below.

pm.test("Test response output object to have expected keys", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.output[0]).to.have.a.property('id');
    pm.expect(jsonData.output[0]).to.have.a.property('name');
    pm.expect(jsonData.output[0]).to.have.a.property('post');
});
Enter fullscreen mode Exit fullscreen mode

Test Six Pass



Awesome! 👏, now we are all done, I urge you to try out some more test cases in your free time, Postman offers public APIs you can play with. Happy Postman Testing 😉.

🐱‍💻 Resources:

Kindly find below links to resources you can use as a guide and also links to my social media should you need to make contact for any challenges you might have or just to have a short conversation if you are starting out.

Postman: https://www.postman.com
Postman Learning: https://learning.postman.com/docs/getting-started/introduction
Postman Test Functions (ChaiJS): https://www.chaijs.com/api

GitHub repository for API code:
https://github.com/clonne101/postman-training

Social Links:
Website: http://jefferyclonne.com
Twitter: https://twitter.com/@clonne101
LinkedIn: https://www.linkedin.com/in/jeffery-osei-551626a6

Discussion

pic
Editor guide