Postman - validate your api response array data use chai js

Chai is a BDD / TDD assertion library ,
that let you can write readable js test,
like this :

1
2
3
4
expect(2).to.equal(2); 
expect('foo').to.have.lengthOf(3); /
expect(resJson.total_count).to.be.an('number');
expect(resJson.data.length).to.be.above(0);

and postman support this library,
so here is a sample test script,
that will validate :

  1. important column not null
  2. iterator array check have data

here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

pm.test("response data not null", function () {
var jsonData = pm.response.json();

pm.expect(jsonData.page).to.be.an('number');
pm.expect(jsonData.per_page).to.be.an('number');
pm.expect(jsonData.total_count).to.be.an('number');
pm.expect(jsonData.data.length).to.be.above(0);

});

pm.test("response data have id", function () {
var jsonData = pm.response.json();

jsonData.data.forEach(function(element) {
pm.expect(element.id).to.be.an('string');
});

if (jsonData.data.length > 0) {
pm.environment.set("video_id", jsonData.data[0].id);
}
});