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?
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
}