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)) } });
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.
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.
pm.expect(pm.response.json().resources.length).to.be.above(0);
See http://www.chaijs.com/api/bdd/
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)) } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With