25

I have tried with !== null, but it is returning PASS even when the field is returning 0 or empty string.

zhulien
  • 5,145
  • 3
  • 22
  • 36
Tester77
  • 309
  • 1
  • 4
  • 10

12 Answers12

32

This works as of Mar-2019:

pm.test("Check if value is null", function() {
  var jsonData = pm.response.json();
  pm.expect(jsonData.<yourField>).not.eq(undefined);
});
keplerian
  • 512
  • 1
  • 8
  • 19
Shiraz
  • 421
  • 4
  • 5
  • 2
    `.is.not.undefined` also works. If you really care about null as well, use `is.not.oneOf([null, undefined])`. – Noumenon May 28 '20 at 04:39
  • This is the 5th version of this that I've had to look for (no exaggeration). Works for a couple of weeks, then stops. These solutions don't work for me. Neither does .to.eql(null) which is what I had before googling it, which is no longer working for me as of today. – Kreidol Apr 30 '21 at 01:09
17

Did you try

pm.expect(response.your_field).to.eql(null);

?

Old Panda
  • 1,466
  • 2
  • 15
  • 30
12

If you are checking the Id of the first item returned in a list, you could use not.equal(null):

pm.expect(pm.response.json().value[0].Id).not.equal(null);

Note that the word "equal" is fully spelled out, although the shortened "eql" does work.

Zysce
  • 1,200
  • 1
  • 10
  • 35
myrtle303
  • 125
  • 1
  • 5
3

try this one:

pm.test("your-value is not null", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.your_value).not.eql(null);
});
cleo
  • 39
  • 1
3

Postman doesn't reference non-existent paths as null, but as undefined.

pm.expect(JsonResponse.FAKE.PATH).not.eql(undefined);

This test should fail, as this fake json path is actually undefined.

Mario
  • 405
  • 7
  • 15
1

I faced similar issue. But checking it in following way worked for me

tests["Item is not null"] = 
    jsonData.item !== undefined;
0

You can access response json like :

    var json = JSON.parse(responseBody);
    var yourVAr = json.yourVar
    if(yourVar == null){
        //your var is null here
    }
Nitin
  • 2,701
  • 2
  • 30
  • 60
0

I have done similar, this is one part of the code and its works well Check this code

var jsonData = JSON.parse(responseBody);

for(i=0; i<jsonData.data.length; i++){
tests["due date is between given date range"] = jsonData.data[i].duedate < environment.Cenddate && jsonData.data[i].duedate > environment.Cstartdate;

tests["response body has department name"] = jsonData.data[i].department.name !== null;

}
0

How about:

var jsonData = JSON.parse(responseBody);

tests["Item is not null"] = 
    jsonData.item !== null && 
    jsonData.item !== ' ' && 
    jsonData.item !== 0;
John Meyer
  • 2,296
  • 1
  • 31
  • 39
0

You gotta place the + [i] after the function you gonna validate on the tests[] so only it will return valid statements on each array. For example,

function checkIsNull() {
    var items = json.data.pois.items
    var PlaceIdIsNull = true;
    var subTypeExtraIsNull = true;
    var websiteIsNull = true;

    for (var i = 0; i < items.length; i++) {
    if (items[i].placeId !== null) {
        PlaceIdIsNull = false;
    }
    if (items[i].subTypeExtra !== null) {
        subTypeExtraIsNull = false;
    }
    if (items[i].website === null) {
        websiteIsNull = false;
        tests['website is null only happened on these arrays' + [i]] = true;
        }
        tests['Place ID is null '] = PlaceIdIsNull
        tests['subTypeExtra is null '] = subTypeExtraIsNull
}
}
       checkIsNull();

Result:


PASS Place ID is null

PASS subTypeExtra is null

0

Try this:

let jsonData = pm.response.json();

pm.expect(jsonData["yourfield"]).to.eq(null);
vimuth
  • 5,064
  • 33
  • 79
  • 116
Svathna
  • 11
  • 1
0

This works as at Sept.19, 2022

     pm.test("Check that response body is not null", ()=>{
   pm.response.to.be.have.body
    })
Piri
  • 1