Check null value in Postman test cases .not.eql() or .not.equal() are not working

Divyang Desai picture Divyang Desai · Jan 28, 2019 · Viewed 11.4k times · Source

I'm working with a API project and using Postman for testing APIs. I wrote few test cases to check null as follows,

//user id is present
pm.test("user id is present", function() {
    pm.expect(jsonData.data[0].userId).not.eql(null);
});

Also tried with .not.equal() as described in this answer.

//user id is present
pm.test("user id is present", function() {
    pm.expect(jsonData.data[0].userId).not.equal(null);
});

However, this all are getting passed even if the userId is null. Any furhter thought to check null value in Postman test case?

Answer

Ângelo Polotto picture Ângelo Polotto · Jan 28, 2019

First of all, open the console and run the flowing snippet to check the types:

pm.test("user id is present", function() {
   console.log(typeof jsonData.data[0].userId);
   console.log(typeof jsonData.data[0]);
   console.log(typeof jsonData);
}

If you get undefined for jsonData.data[0].userId, you need change your test to assert null and undefined:

pm.test("user id is present", function() {
   pm.expect(jsonData.data[0].userId).to.exist //check if it is not equal to undefined
   pm.expect(jsonData.data[0].userId).to.not.be.null; //if it is not equal to null
}