# My else part is not getting executed when i am getting other status code(ex:400)

**URL:** <https://community.postman.com/t/my-else-part-is-not-getting-executed-when-i-am-getting-other-status-code-ex-400/11910>\
**Category:** Help Hub\
**Created:** [April 13, 2020, 9:45am UTC](https://community.postman.com/t/my-else-part-is-not-getting-executed-when-i-am-getting-other-status-code-ex-400/11910 "2020-04-13T09:45:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![IPGMB013](https://avatars.discourse-cdn.com/v4/letter/i/f0a364/32.png) [@IPGMB013](https://community.postman.com/u/IPGMB013)\
**Post date:** [April 13, 2020, 9:45am UTC](https://community.postman.com/t/my-else-part-is-not-getting-executed-when-i-am-getting-other-status-code-ex-400/11910/1 "2020-04-13T09:45:00Z")

</div>

Hi Guys,

I am new to the postman and i have written test scripts using if else but my else part is not executing whenever i am getting different status code instead of expected one.

Can anyone help me where i am doing wrong ?

Please find the sample code which i have written for my request below.

//To capture request body name and reponse body name field’s value  
let reqBody = JSON.parse(request.data);  
reqName = reqBody.name;  
console.log(reqName);  
let resBody = JSON.parse(responseBody);  
resName = resBody.name;  
console.log(resName);

//To check status code and name field’s value are equal  
if(pm.response.to.have.status(201) && reqName === resName) {  
pm.test(‘Test is passed with Status code 201 and expected client name’);  
console.log(“Test is passed”);  
}  
else{  
pm.test(“Test is failed”);  
console.log(“Test is falied”);  
}

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [April 13, 2020, 1:34pm UTC](https://community.postman.com/t/my-else-part-is-not-getting-executed-when-i-am-getting-other-status-code-ex-400/11910/2 "2020-04-13T13:34:39Z")

</div>

Hey, @IPGMB013 welcome to the community! 👋

Your else part is not being executed because the assertion is probably throwing an error and your script stops executing. Also, the assertion doesn’t return a boolean → `pm.response.to.have.status(201)` // this won’t return a boolean value.

The correct way to do this is, write a test:

```javascript
let reqBody = JSON.parse(request.data);
reqName = reqBody.name;
console.log(reqName);
let resBody = JSON.parse(responseBody);
resName = resBody.name;
console.log(resName);

pm.test('should have the status code as 201 and reqName to equal resName', function () {
  pm.response.to.have.status(201);
  pm.expect(reqName).to.equal(resName);
});

```

Now when you’ll run this script, you’ll see that whenever any of these assertions throw an error, the test will fail.

There are a lot of resources around writing tests in Postman, I’ll leave a few here by @jetison :

[![](https://img.youtube.com/vi/pi9MxX0HSHU/maxresdefault.jpg "Writing tests in Postman -- with examples! Part 1 of 2") ](https://www.youtube.com/watch?v=pi9MxX0HSHU)

> **[Writing tests in Postman | Postman Blog](https://blog.postman.com/writing-tests-in-postman/)**
>
> Everyone agrees that writing tests is important, but not everyone does it. As you introduce new code, tests ensure that your API...

Also, official docs:  
[https://learning.postman.com/docs/postman/scripts/test-scripts/](https://learning.postman.com/docs/postman/scripts/test-scripts/)
