How could I check status code 403 within a “if statement”? I want to make decision for test cases based on status code.
For example:
if status code is 403:
DO this
return(do not need to check the next condition)
else if status code is 401:
DO this
return(do not need to check the last condition)
else:
DO this
ques
569×538 21.2 KB
when I try to put the condition like:
if (pm.response.to.have.status(403)){
pm.test(“Response is showing error, token expired”, function(){
pm.test(“Status code is 403 Forbidden”, function(){
pm.response.to.have.status(403)
return
the if condition works fine but the problem is when this condition is not true it still enters in this block and look for “(pm.response.to.have.status(403))” which shows an error because the status is not 403 and next two conditions do not work.
I’ve already tried
:
if (pm.response.to.have.status(403)){
pm.test("Response is showing error, token expired", function(){
pm.test("Status code is 403 Forbidden", function(){
pm.response.to.have.status(403)
return
else if (jsonData.message == "User not found"){
pm.test("User not found", function(){
pm.expect(jsonData.message).contains("User not found");
pm.test("Status code is 404", function () {
pm.response.to.have.status(404);
return;
else {
var phoneNumber = jsonData.user.phone_number;
pm.collectionVariables.set("phoneNumber", phoneNumber)
pm.test("Success, response is showing the details of user with phone number, " + phoneNumber, function(){
pm.expect(jsonData.user.phone_number).to.be.eql(phoneNumber);
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
You could try using a “try/catch” statement… Here is an example of one I built to continue through all conditions, instead of stopping after the first match.
pm.test("API response contains all of the expected fields", () => {
//Parse response and save as variable
const response = pm.response.json();
let count = 0;
//Tests to PASS
try{pm.expect(response).to.have.property("status", "available");
}catch(e){pm.test("Check status value", () => {throw new Error(e.message)}), count++;}
try{pm.expect(response).to.have.nested.property("category.id", 0);
}catch(e){pm.test("Check category.id value", () => {throw new Error(e.message)}), count++;}
try{pm.expect(response).to.have.property("id", 0);
}catch(e){pm.test("Check id value", () => {throw new Error(e.message)}), count++;}
//Tests to FAIL with WRONG DATA
try{pm.expect(response).to.have.property("name", "WRONG DATA");
}catch(e){pm.test("Check name value", () => {throw new Error(e.message)}), count++;}
try{pm.expect(response).to.have.nested.property("category.name", "WRONG DATA");
}catch(e){pm.test("Check category.name value", () => {throw new Error(e.message)}), count++;}
//Count how many tests above fail, and fail the test case using pm.expect.fail if there is 1 or more failures
if(count > 0) {
pm.expect.fail("There are " + count + " failures within the above checks, please check...")
Off the top of my head I think yours would be something like;
try{pm.response.to.have.status(403)
pm.test("Status code is 403 Forbidden", function(){
pm.response.to.have.status(403)
return
catch(e){pm.test("Check status value", () => {throw new Error(e.message)})
var jsonData = pm.response.json();
pm.test("User not found", function () {
if (pm.response.code === 404) {
pm.expect(jsonData.message).contains("User not found");
This way you pass the test if the code is not 404 and if it’s 404 it checks if the message is User not found
Please don’t.
This breaks a couple of patterns in testing.
First of all, try and make your tests as singular as possible.
Having an IF statement checking for three potential outcomes fills me with dread. It sounds like you are not in control of your test data.
If you are testing for “user not found”, make sure you have test data that will produce that result and put it in its own request and have tests specifically for this.
Same goes for 403 forbidden. It should have its own request and test.
Same principle goes for having more than one assertion in each pm.test function.
Postman will run each of the pm.test functions in the test tab.
But within each individual test, if you have more than one assertion, it will abort on the first failure.
So if you have 3 assertions within the same test function, checking for example that the firstname, lastname, and DOB are as expected. If the firstname assertion is first and fails, then it will not run the lastname or DOB checks. Therefore you are not necessarily getting the full feed back you may want. You may have more than one bug and not notice it until you re-test.
I completely agree with you, but there might be some cases where the API itself is designed to return 200 if some conditions are satisfied or else 404. For example it happens if you have a cascade of calls inside your API. In this case you might write something like:
pm.test("Status code 200 or 404", function() {
pm.expect(pm.response.code).to.be.oneOf([200, 404])
But yeah it’s a very niece case and it’s generally best to write one call per status code where for example you put a “bad token” in the headers just to test a 401 or 403 and do your tests accordingly.