# How to write a test to check if response does not contain specific value

**URL:** <https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [November 19, 2020, 3:19pm UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875 "2020-11-19T15:19:26Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhikulkarni.1988](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@abhikulkarni.1988](https://community.postman.com/u/abhikulkarni.1988)\
**Post date:** [November 19, 2020, 3:19pm UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/1 "2020-11-19T15:19:26Z")

</div>

Hi

I want to write some tests to check if the response does not contain a specific value or array or object.

Is it possible to test -ve scenario via postman scripts?

Thanks,  
Abhijit

---

<div class="post-metadata">

**Author:** ![yusuftaymann](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/yusuftaymann/32/6160_2.png) [@yusuftaymann](https://community.postman.com/u/yusuftaymann)\
**Post date:** [November 19, 2020, 3:42pm UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/2 "2020-11-19T15:42:29Z")

</div>

Hi @abhikulkarni.1988 ,

Welcome to the community 👏

You can use Chai.js, sample use like below 👍  
More than examples are here --\>

> **[Expect / Should - Chai](https://www.chaijs.com/api/bdd/#method_a)**

```auto
pm.test('is an Array', () => pm.expect(pm.response.json()).to.be.an('array').but.not.an('object'))

```

---

<div class="post-metadata">

**Author:** ![abhikulkarni.1988](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@abhikulkarni.1988](https://community.postman.com/u/abhikulkarni.1988)\
**Post date:** [November 19, 2020, 3:49pm UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/3 "2020-11-19T15:49:53Z")

</div>

Thanks for your reply but I am trying to write a test like below but it is not working

pm.test(“Response not to have address”,()=\>{  
pm.expect(pm.response.text()).not.to.include(“address”);  
});

Can you help to correct this?

---

<div class="post-metadata">

**Author:** ![yusuftaymann](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/yusuftaymann/32/6160_2.png) [@yusuftaymann](https://community.postman.com/u/yusuftaymann)\
**Post date:** [November 19, 2020, 4:14pm UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/4 "2020-11-19T16:14:30Z")

</div>

```
pm.test("Response not to have address", function () 
{
    pm.expect(pm.response.text()).to.not.include("address");
});

```

You can use the as above, should be not.to.include instead of to.not.include 👍

---

<div class="post-metadata">

**Author:** ![abhikulkarni.1988](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@abhikulkarni.1988](https://community.postman.com/u/abhikulkarni.1988)\
**Post date:** [November 19, 2020, 5:41pm UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/5 "2020-11-19T17:41:29Z")

</div>

No luck with above solution

 ![Screenshot 2020-11-19 at 11.09.36 PM](https://us1.discourse-cdn.com/getpostman/original/3X/9/c/9cc0966361cb7ded750c4a1d68446f278fecbede.png)

---

<div class="post-metadata">

**Author:** ![yusuftaymann](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/yusuftaymann/32/6160_2.png) [@yusuftaymann](https://community.postman.com/u/yusuftaymann)\
**Post date:** [November 19, 2020, 5:55pm UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/7 "2020-11-19T17:55:41Z")

</div>

The answer you shared already contains the word address. Do you want to assert only for the word “address”? 🤔

---

<div class="post-metadata">

**Author:** ![abhikulkarni.1988](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@abhikulkarni.1988](https://community.postman.com/u/abhikulkarni.1988)\
**Post date:** [November 19, 2020, 6:00pm UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/8 "2020-11-19T18:00:08Z")

</div>

I see!

Word “address” is present in key “email\_address” but my test is to check if key “address” is not present.

how to achieve this?

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [November 20, 2020, 3:32am UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/10 "2020-11-20T03:32:45Z")

</div>

Hello @abhikulkarni.1988 , Welcome to the Community 👋

So here you are testing that “address” shouldn’t be a part of your response body as a object?

In that case, you can try hasOwnProperty function.

```auto
var resp = JSON.parse(responseBody);
console.log(resp);
if (!resp.hasOwnProperty("address")){
pm.test("Address is not present", () => {
    console.log("Passed");
  });}
  else{
     console.log("Failed"); 
  }

```

Please try this 🙂 If this doesn’t work, kindly explain your need and share more details.

---

<div class="post-metadata">

**Author:** ![abhikulkarni.1988](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@abhikulkarni.1988](https://community.postman.com/u/abhikulkarni.1988)\
**Post date:** [November 20, 2020, 9:05am UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/11 "2020-11-20T09:05:02Z")

</div>

Thanks your solution worked!

But when the Address is present I want to report that the test failed.

I tried the below options in else block

pm.expect.fail(“Address is present”);

pm.test(“Address is present”, function () {  
pm.expect(1).to.eql(2);  
});

Is there better option than this?

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [November 20, 2020, 11:35am UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/12 "2020-11-20T11:35:09Z")

</div>

@abhikulkarni.1988, May be you can try this:

```auto
pm.test("Address is not present", () => {
    if (!resp.hasOwnProperty("address")) {
        pm.expect(!resp.hasOwnProperty("address")).to.equal(true)
    }
    else {
        pm.expect.fail('Address is present')
    }
})

```

Hope this as per your request, I have modified and so you will get pass or fail in single test cases as you expected.

---

<div class="post-metadata">

**Author:** ![abhikulkarni.1988](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@abhikulkarni.1988](https://community.postman.com/u/abhikulkarni.1988)\
**Post date:** [November 20, 2020, 12:20pm UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/13 "2020-11-20T12:20:13Z")

</div>

@bpricilla This worked 👍

I am very thankful for the solution! 😀

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [November 20, 2020, 12:58pm UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/14 "2020-11-20T12:58:32Z")

</div>

Glad to hear that 😀 Happy to help always 👍

---

<div class="post-metadata">

**Author:** ![abhipawar300](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@abhipawar300](https://community.postman.com/u/abhipawar300)\
**Post date:** [March 22, 2022, 8:58am UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/15 "2022-03-22T08:58:46Z")

</div>

Thank you for the solution

---

<div class="post-metadata">

**Author:** ![abhipawar300](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@abhipawar300](https://community.postman.com/u/abhipawar300)\
**Post date:** [March 22, 2022, 8:59am UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/16 "2022-03-22T08:59:20Z")

</div>

i was searching for a code like this.

Thank you for solution

---

<div class="post-metadata">

**Author:** ![BBott](https://avatars.discourse-cdn.com/v4/letter/b/8dc957/32.png) [@BBott](https://community.postman.com/u/BBott)\
**Post date:** [May 4, 2022, 8:15pm UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/17 "2022-05-04T20:15:42Z")

</div>

Thank you for sharing this! It was extremely helpful and was able to adjust to apply to my current test issue.

---

<div class="post-metadata">

**Author:** ![mataranikijai1](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@mataranikijai1](https://community.postman.com/u/mataranikijai1)\
**Post date:** [March 15, 2023, 8:06am UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/18 "2023-03-15T08:06:03Z")

</div>

Hi I want to add test for validating that a particular product has been removed from a node in the response while other nodes may contain the product can we do it?  
can any one help me with the test

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 22, 2023, 2:01pm UTC](https://community.postman.com/t/how-to-write-a-test-to-check-if-response-does-not-contain-specific-value/17875/19 "2023-08-22T14:01:36Z")

</div>

Closed on August 22, 2023, 2:01pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
