Testing multiple cases for a request

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?

Hi @grrigore

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

1 Like

Hi @grrigore

Some Postman test scripts you can use for testing 200 :

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

Some Postman test scripts you can use for testing 400 :

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 handling | Better Practices

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

1 Like

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

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;

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.

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

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.

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

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

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?

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 - :person_raising_hand: Help - Postman Community

Loop through values from GET to next PUT request - :person_raising_hand: Help - Postman Community