How to Write Tests in Postman - API Testing
Tags: rest, postman, api, testing
Published: 2021-10-13 19:38:03

{% youtube OSwoz3kJF2o %} 
Writing tests in Postman is a great way to test your API. It is easy as well as necessary for your API Development as it makes sure your API is working as expected. You can check all your API in a couple of clicks. Let's Talk about how to test API in Postman.

First of all, you need to create a collection in Postman. I hope you already know how to create a collection and how to add a request to it.
After that, you need to write tests in the `Tests` tab.

Tests in postman are based on the `Chai Js` Assertion Library.
Chaijs is a BDD(Behavior Driven Development)/TDD(Test Driven Development) assertion library for node and the browser that can be delightfully paired with any javascript testing framework.

Let's start with the most basic and very useful test.

## Check Response Status

```JavaScript
pm.test("Status code is 201",()=>{
    pm.response.to.have.status(200);
    pm.expect(pm.response.code).to.equal(200);
    pm.expect(pm.response.code).to.be.oneOf([201, 200]);
    pm.response.to.be.ok;
});
```

`pm.test` is a Postman function that takes the name of the test and a callback function as arguments. Inside the callback function, you can write your assertions.
as you can see in the above example

- First: Checking that the status code is 201.
- Second: Expecting the response code is equal to 201.
- Third: Expecting the response code is one of the following: [201, 200].
- Forth: Checking that the response type is Ok.

## Checking the Response time

```JavaScript
pm.test("Response time is less than 200ms", () => {
    pm.expect(pm.response.responseTime).to.be.below(250);
});
```

## Is Content type is Present? Content type is JSON or not ?

```JavaScript
pm.test("Content type is present", () => {
    pm.response.to.have.header("Content-Type");
    pm.expect(pm.response.headers.get("Content-Type")).to.be.equal("application/json");
    pm.response.to.be.json;
});
```

## Checking the Response Type by first storing the response in a variable

```JavaScript
pm.test("Test data type of the response", () => {
    const res = pm.response.json();
    pm.expect(res).to.be.an("object");
    pm.expect(res.name).to.be.a("string");
    pm.expect(res.age).to.be.a("number");
    pm.expect(res.hobbies).to.be.an("array");
    pm.expect(res.website).to.be.undefined;
    pm.expect(res.email).to.be.null;
    pm.expect(res).to.be.ok;
});
```

## Response body should be an array and should have at least one element

```JavaScript
pm.test("Response body should be an array", () => {
    let res = pm.response.json();
    pm.expect(res).to.be.an("array");
    pm.expect(res).to.have.length.above(0);
});
```

## Checking for the response have a particular key & Include a particular string

```JavaScript
pm.test("Response should have a name & id property", () => {
    let res = pm.response.json();
    pm.expect(res[0]).to.have.property("name");
    pm.expect(pm.response.text()).to.include("id");
});
```

## Response body should not return the password

```JavaScript
pm.test("Response body should not return password", () => {
    let res = pm.response.json();
    pm.expect(res).to.not.include("password");
    pm.expect(res).to.not.have.property("password");
});
```

There are all the basic tests you can write in Postman.

Let's check out some helper functions to make your testing easier.

## Setting Different types of Variables

```JavaScript
let res = pm.response.json();
pm.environment.set("id", res[0]["id"]);
pm.collectionVariables.set("id", res[0].id);
pm.globals.set("id",res[0]["id"]);
console.log(pm.environment.get("id"));
```

They're different in the types of variable is the scope of the variable. Globals are available to all the tests in all the collections. Collection Variables are available only to the tests in the collection. Environment variables are available to all the tests in the collections if the environment is set.

## Checking the Active Environment

```JavaScript
pm.test("Check the active environment", () => {
    pm.expect(pm.environment.name).to.eql("Api-Template");
});
```

## Basic schema validation

The database schema is its structure described in a formal language supported by the DBMS. The term "schema" refers to the organization of data as a blueprint of how the database is stored in the database.

```JavaScript
var schema = {
    "items": {
        "type": "boolean"
    }
};

var data1 = [true, false];
var data2 = [true, 123];

pm.test("Schema is valid", () => {
    pm.expect(tv4.validate(data1, schema)).to.be.true;
});
```

## Send request inside Test

```JavaScript
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    console.log(response.json());
});
```

That's Sit for basic to intermediate level of testing of APIs in Postman

### Let me know if you have any questions or queries. I’ll be happy to help you

## Like Share, and Follow. You can also check my other profiles on [King Technologies](https://kingtechnologies.in/ "King Technologies")

## Thanks for Reading