Beginners guide to writing Postman API Tests
Tags: postman, javascript, php, beginners
Published: 2020-11-22 12:48:12

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](https://www.postman.com), 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!.

<hr/><br/>

## STEPS 🪜
* [Install Postman](#chapter-1)
* [Add Environment Variable](#chapter-2)
* [Add Test Request](#chapter-3)
* [Add Test Cases](#chapter-4)

<hr/><br/>

### 💻 Install Postman <a name="chapter-1"></a>: 
Installing the Postman app is really easy, visit this [page](https://www.postman.com/downloads/) and obtain your platform version (Mac, Windows, Linux).

<hr/><br/>

### 🍕 Add Environment Variable <a name="chapter-2"></a>: 
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](https://dev-to-uploads.s3.amazonaws.com/i/jyi9ffpkl0x2bmmwphax.png)

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

![Add Environment Variable](https://dev-to-uploads.s3.amazonaws.com/i/ddtgzvh0q36k3dj6jzvy.png)

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
```

![Set Environment Variables](https://dev-to-uploads.s3.amazonaws.com/i/n8tf2msg48qnmi9i3qz9.png)

* 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](https://dev-to-uploads.s3.amazonaws.com/i/wxmpo9s6qjk6vm779lhg.png)

<hr/><br/>

### 🧱 Add Test Request <a name="chapter-3"></a>: 
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](https://dev-to-uploads.s3.amazonaws.com/i/kkltczuy62878o6267ow.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/p7fizjt06pdgyvincwjo.png)

<hr/><br/>

### 🎄 Add Test Cases <a name="chapter-4"></a>: 
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](https://dev-to-uploads.s3.amazonaws.com/i/vqlj7u3a4fk0269h73fn.png)

<hr/><br/>

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.

```javascript
pm.test("Test status response is 200", function () {
    pm.response.to.have.status(200);
});
```
![Test One Pass](https://dev-to-uploads.s3.amazonaws.com/i/rcstyz8wkq1dk0ife7ou.png)

<hr/><br/>

* 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.

```javascript
pm.test("Test response output status", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.status).to.equal(0);
});
```
![Test Two Pass](https://dev-to-uploads.s3.amazonaws.com/i/o33xw30yyf1rzm23i7kc.png)

<hr/><br/>

* 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.

```javascript
pm.test("Test response output message", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.message).to.equal("Success");
});
```
![Test Three Pass](https://dev-to-uploads.s3.amazonaws.com/i/yrmgiyjr19i87b276h0g.png)

<hr/><br/>

* 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.

```javascript
pm.test("Test response output to be an array", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.output).to.be.an("array");
});
```
![Test Four Pass](https://dev-to-uploads.s3.amazonaws.com/i/fk8np5f79dbg9qt2rgal.png)

<hr/><br/>

* 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.

```javascript
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
});
```
![Test Five Pass](https://dev-to-uploads.s3.amazonaws.com/i/zlctxm6n5cjllxfjgtgg.png)

<hr/><br/>

* 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.

```javascript
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');
});
```
![Test Six Pass](https://dev-to-uploads.s3.amazonaws.com/i/8qndp8vwjxgo8wugdkjw.png)

<hr/><br/>

Awesome! 👏, now we are all done, I urge you to try out some more test cases in your free time, Postman offers [Public APIs](https://www.postman.com/explore) 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: https://jefferyclonne.com
Twitter: https://twitter.com/@clonne101
LinkedIn: https://www.linkedin.com/in/jeffery-osei-551626a6

Video Link:
https://dev.to/clonne101/video-postman-api-testing-2pm