30

I'm trying to make a test in POSTMAN where the size has to be greater than 0 but I haven't been able to make the test correctly.

What I did was to make it fail when the size is smaller than 0.

Is there a function in postman to check if the size is greater than x number?

    pm.test("Step 7/ Getting the resources and availabilites list " , function(){

    pm.expect(pm.response.code).to.be.oneOf([200]);
    if(pm.response.code === 200){
        var jsonData = JSON.parse(responseBody);
        var sizeOK= 1;
        if(jsonData.resources.length>0){

        }else{
            //I will make the test fail if there is not data available on the response.
            pm.test("Response body is empty ", function () {
                pm.expect(pm.response.json().resources.length).to.equal(1);
            });

        }
        console.log(Boolean(jsonData.resources.length>1))
    }

});
martijnn2008
  • 3,552
  • 5
  • 30
  • 40
mavi
  • 1,074
  • 2
  • 15
  • 29

3 Answers3

63
pm.expect(pm.response.json().resources.length).to.be.above(0);

See http://www.chaijs.com/api/bdd/

anti_gone
  • 973
  • 8
  • 18
1

Postman uses an extended implementation of the chai library. You can check out the source code here: https://github.com/postmanlabs/chai-postman

So logically the test only fails when you throw an error and the test catches it. Else it simply passes. So the expect calls actually throw an error which makes the test fail. If you just return anything or maybe return nothing, even then the test would pass.

Think in terms of a simple try and catch block. So, to solve your problem instantly you can just throw an error and your test would fail.

You can modify your code like so:

pm.test("Step 7/ Getting the resources and availabilites list " , function(){

    pm.expect(pm.response.code).to.be.oneOf([200]);
    if(pm.response.code === 200){
        var jsonData = JSON.parse(responseBody);
        var sizeOK= 1;
        if(jsonData.resources.length>0){

        } else {
            pm.test("Response body is empty ", function () {
               throw new Error("Empty response body"); // Will make the test fail.
            });

        }
        console.log(Boolean(jsonData.resources.length>1))
    }

});

Also, you can additionally use simple javascript to test the length / size easily like so (just an eg.):

pm.test("Step 7/ Getting the resources and availabilites list " , function(){

        pm.expect(pm.response.code).to.be.oneOf([200]);
        if(pm.response.code === 200){
            var jsonData = JSON.parse(responseBody);
            var sizeOK= 1;
            if(jsonData.resources.length>0){

            } else {
                pm.test("Response body is empty ", function () {
                   if(jsonData.length < 3) {
                      throw new Error("Expected length to be greater than 3");
                   }
                });

            }
            console.log(Boolean(jsonData.resources.length>1))
        }

    });
Sivcan Singh
  • 1,775
  • 11
  • 15
0

Though I'm not sure the degree of precision you need, you get a response size in Postman. It is made of Body size and Headers size (just point on Size's value in the app). In your test area, you can recover Body size doing:

var size=0;
for (var count in responseBody) {
    if(responseBody.hasOwnProperty(count))
        size += 1;
}
console.log("BODY SIZE = " + size); // you'll see the correct value in the console for the body part

and then test against this value ...

A.Joly
  • 2,317
  • 2
  • 20
  • 25
  • Hi A.Joly, thanks for answering, but you may don´t understand me. What I want to do in the test is to check if the resources.length is greater than 2 doing something like this : pm.expect(pm.response.json().resources.length).to.greaterThan(2); or something like : pm.expect(pm.response.json().resource.length > 2). But even when the length result is smaller than 2 the test is successful – mavi Nov 21 '17 at 18:14
  • 1
    ah, ok, sorry ... I'm not familiar enough with pm.xx methods ... did you try without Chai ? in an old fashion coding of javascript ? – A.Joly Nov 22 '17 at 07:12
  • I haven't but I will check it. Thanks! – mavi Nov 22 '17 at 22:59