# Testing multiple cases for a request

**URL:** <https://community.postman.com/t/testing-multiple-cases-for-a-request/34601>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [April 20, 2022, 6:57am UTC](https://community.postman.com/t/testing-multiple-cases-for-a-request/34601 "2022-04-20T06:57:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![grrigore](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@grrigore](https://community.postman.com/u/grrigore)\
**Post date:** [April 20, 2022, 6:57am UTC](https://community.postman.com/t/testing-multiple-cases-for-a-request/34601/1 "2022-04-20T06:57:04Z")

</div>

Hello,

Is there a way to test both 200 or 400 API status responses in the same test?

Let’s say you have a login endpoint that takes an email and a password. If the credentials are correct it returns 200, but if they aren’t it returns 400. This means the login endpoint’s body needs different data and I don’t know how to handle it in the same request (as in Postman).

So far I’ve created two requests with different bodies, but I think there might be a better approach.

What are the best options for this? To test multiple cases for a request?

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [April 20, 2022, 3:17pm UTC](https://community.postman.com/t/testing-multiple-cases-for-a-request/34601/2 "2022-04-20T15:17:27Z")

</div>

Hi @grrigore

You could feed data into a single request using a CSV file;

> **[Importing data files | Postman Learning Center](https://learning.postman.com/docs/running-collections/working-with-data-files/)**
>
> Importing data files: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

---

<div class="post-metadata">

**Author:** ![pooja-mistry](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/pooja-mistry/32/11451_2.png) [@pooja-mistry](https://community.postman.com/u/pooja-mistry)\
**Post date:** [April 20, 2022, 10:20pm UTC](https://community.postman.com/t/testing-multiple-cases-for-a-request/34601/3 "2022-04-20T22:20:20Z")

</div>

Hi @grrigore

Some Postman test scripts you can use for testing 200 :

```auto
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

```

Some Postman test scripts you can use for testing 400 :

```auto
pm.test("Status code is 400", function () {
    pm.response.to.have.status(400);
});

```

Take a look at this blog for positive and negative testing : [Unhappy path test cases to ensure proper input validation and error&nbsp;handling | Better Practices](https://medium.com/better-practices/negative-testing-for-more-resilient-apis-af904a74d32b)

Also if your just getting started you can check this out as well : [Four Questions to Consider when Starting your API Testing Journey - DEV Community](https://dev.to/poojamakes/four-things-to-consider-when-starting-your-api-testing-journey-3873)

---

<div class="post-metadata">

**Author:** ![grrigore](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@grrigore](https://community.postman.com/u/grrigore)\
**Post date:** [April 29, 2022, 8:11am UTC](https://community.postman.com/t/testing-multiple-cases-for-a-request/34601/4 "2022-04-29T08:11:43Z")

</div>

Can you provide an example? Can one import data for every request or does it have to be a single .csv file?

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [April 29, 2022, 10:17am UTC](https://community.postman.com/t/testing-multiple-cases-for-a-request/34601/5 "2022-04-29T10:17:22Z")

</div>

I would probably do a csv file with something like:

username,password,expectedResponse  
test1,test1,PASS  
test2,test2,FAIL

The username and password would be fed into the request and then I’d put some code in the test tab like;

```auto
let z = pm.iterationData.get('expectedResponse');

if (z == "PASS") {
    pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
    });
} else if (z == "FAIL") {
    pm.test("Status code is 400", function () {
    pm.response.to.have.status(400);
    });
}

```

Once set up, run in collection runner and check the outputs;

I left the 400 as a fail but if the credentials actually failed then the response would be the expected 400 code.

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/6/681bdca8ebd79125d23b4d1b3bab9fc16ea02d03.png)

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/4/4f26022142679aa3a0f785338dcbd576488500f1.png)

---

<div class="post-metadata">

**Author:** ![grrigore](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@grrigore](https://community.postman.com/u/grrigore)\
**Post date:** [April 29, 2022, 10:29am UTC](https://community.postman.com/t/testing-multiple-cases-for-a-request/34601/6 "2022-04-29T10:29:41Z")

</div>

But do you need to pass a CSV for each request? How does it know what `pm.iterationData` is?

---

<div class="post-metadata">

**Author:** ![jchaugule](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jchaugule](https://community.postman.com/u/jchaugule)\
**Post date:** [April 29, 2022, 10:57am UTC](https://community.postman.com/t/testing-multiple-cases-for-a-request/34601/7 "2022-04-29T10:57:47Z")

</div>

Hi @grrigore

In my case whenever I want to test with different data in request body I generally run a loop and send them in the Pre-request script then run it.  
But yes this is possible only if you have few sets of data as it is the case right now for you.  
I’m giving a sample code for what I do.

```auto
pm.request.body.raw = ""
pm.request.body.formdata.clear()
pm.request.url.variables.clear()

```

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [April 29, 2022, 11:20am UTC](https://community.postman.com/t/testing-multiple-cases-for-a-request/34601/8 "2022-04-29T11:20:35Z")

</div>

> [@grrigore](#):
>
> But do you need to pass a CSV for each request? How does it know what `pm.iterationData` is?

Have a look at the link I sent above, it explains how to work with CSV files.

---

<div class="post-metadata">

**Author:** ![mmontero84](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@mmontero84](https://community.postman.com/u/mmontero84)\
**Post date:** [September 26, 2023, 11:33am UTC](https://community.postman.com/t/testing-multiple-cases-for-a-request/34601/9 "2023-09-26T11:33:23Z")

</div>

Is it possible to do it not from Collection Runner but when sending the request itself so then I can see the results in the ‘Test Results’ tab?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 26, 2023, 2:47pm UTC](https://community.postman.com/t/testing-multiple-cases-for-a-request/34601/10 "2023-09-26T14:47:01Z")

</div>

You can create a loop by setting a collection variable with your test data and then using a pre-request script and the JavaScript array.shift() method to get the first record from the array. (The shift method also deletes that record from the array).

You can then retrieve the various elements from the current record and set them as collection variables to be used in the actual request.

You also need to re-save the updated array over the top of the existing collection variable (which should now have one less record).

In the tests tab. You retrieve the array again which will be used in an IF statement in conjunction with pm.setNextRequest() to keep running the same request while the length of the array is more than zero.

Please note that setNextRequest() still requires the collection runner to work. You can’t get this to loop without the collection runner. Both the CSV method and the setNextRequest() method require the collection runner.

Here are a few topics that are utilizing this method.

[Simulating 100 users using Pre-Request and Tests - 🙋 Help - Postman Community](https://community.postman.com/t/simulating-100-users-using-pre-request-and-tests/48125)

[Loop through values from GET to next PUT request - 🙋 Help - Postman Community](https://community.postman.com/t/loop-through-values-from-get-to-next-put-request/41899)
