Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if something is greater than certain number with POSTMAN

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))     }  }); 
like image 367
mavi Avatar asked Nov 20 '17 06:11

mavi


People also ask

How do you test a variable in a Postman?

Open a new request tab and enter https://postman-echo.com/get?var={{my_variable}} as the URL. Hover over the variable name to inspect the variable's value and scope. Select Send and send the request. Inspect the response, which confirms that Postman sent the variable value to the API.

How do you write a test case for a Postman?

To start building test cases quickly, commonly-used snippets are listed next to the test editor. Select a snippet to append the code to the test editor. If needed, update the stub with assertions specific to your endpoint's expected response. Then, send the request to view the test results at the bottom.


2 Answers

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

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

like image 124
anti_gone Avatar answered Sep 20 '22 04:09

anti_gone


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))         }      }); 
like image 42
Sivcan Singh Avatar answered Sep 23 '22 04:09

Sivcan Singh