# How do I write test for same request with different body?

**URL:** <https://community.postman.com/t/how-do-i-write-test-for-same-request-with-different-body/8976>\
**Category:** Help Hub\
**Created:** [November 13, 2019, 7:34am UTC](https://community.postman.com/t/how-do-i-write-test-for-same-request-with-different-body/8976 "2019-11-13T07:34:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![leen](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@leen](https://community.postman.com/u/leen)\
**Post date:** [November 13, 2019, 7:34am UTC](https://community.postman.com/t/how-do-i-write-test-for-same-request-with-different-body/8976/1 "2019-11-13T07:34:39Z")

</div>

If I have a request how can I write specific tests for different sets of data.  
For example,

if I have a body:

```auto
{
    "name": "Test"
    "email": "test@test.com"
}

```

I want to run a certain test case againt this particular request, say:

```auto
pm.test("response should be 200 when email is provided", function() {
    pm.response.to.have.status(200);
});

```

Now if I have a body for the same request without an email:

```auto
{
    "name": "Test"
}

```

I want to run only this test case againt this particular request body:

```auto
pm.test("response should be 400 when email is not provided", function() {
    pm.response.to.have.status(400);
});

```

All of this should happen in the same request, when the `Send` is pressed. How to achieve this?

---

<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:** [November 13, 2019, 9:21am UTC](https://community.postman.com/t/how-do-i-write-test-for-same-request-with-different-body/8976/2 "2019-11-13T09:21:38Z")

</div>

Hi @leen, you can write tests conditionally.

This is just an example to do so:

```javascript
let resp = {};

// This is because if you try to do `JSON.parse` on a response
// which is not a JSON then it'll throw an error
try {
    resp = pm.response.json();
}
catch (e) {}

pm.test(`response code should be ${pm.response.code} based on email availability`, function () {
  if (_.get(resp, 'email')) { // Using lodash which is built-in postman
    pm.response.to.have.status(200);
  }
  else {
    pm.response.to.have.status(404);
  }
});

```

There can be multiple ways to do things like these, based on what you’re checking.  
But yes, you can use the entire power of javascript to do things like these.

I also used [lodash](https://lodash.com/docs/4.17.11) which is built-into postman.  
There are [multiple other libraries](https://learning.getpostman.com/docs/postman/scripts/postman-sandbox-api-reference/) that are built-in with postman and you can use them in the pre-request / test scripts.

---

<div class="post-metadata">

**Author:** ![JJ\_k](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jj_k/32/3907_2.png) [@JJ\_k](https://community.postman.com/u/JJ_k)\
**Post date:** [February 28, 2020, 2:24pm UTC](https://community.postman.com/t/how-do-i-write-test-for-same-request-with-different-body/8976/3 "2020-02-28T14:24:35Z")

</div>

Hi @leen

Was looking for an answer myself-\>

What you could do is set the expected status code as Environment variable, and have the **prerequest script** fill it with the expected value according to how your request looks: IE

This is almost the same code as @singhsivcan suggested but then using the request instead of the response.

```
var req = JSON.parse(pm.request.body.raw);

if (_.get(req, 'email')) {
    pm.environment.set("expectedstatus", (200));
} else {

pm.environment.set("expectedstatus", (400));
}

```

then have your test check the response status against (pm.environment.get(“expectedstatus”));
