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.