So I use postman tests to test my REST API and its really cool. One thing I’m wondering how to do is how to write tests that are expected to fail, so I can check if its returning correct error status code etc etc. As you know tests in postman are attached to requests, so if I create separate request with wrong parameters to test that its failing “correctly”, then these requests will also be visible in docs when I publish them (which I don’t want). So whats the good practice here?
Welcome to the community
What you’re saying sounds perfect to me. The best practice, from a general testing perspective, is to write out test cases that cover each use case you have.
So for example use cases:
A user enters a url that does not exist, the should receive a status code of 404.
A user enters an improperly formatted request, they receive a status code of 400.
A user enters a method thats doesn’t match the url, they receive a status code of 405.
Those are just some examples, at least around client side issues. Now, you just write a test case the purposely puts those inputs, in your test section, and then validates by checking the response status code.
Now you may not have to do all of those, since some of the use cases may be covered by your application stack, depending on the stack you are using, and already tested by those developers. However any customizations you put on top of those, you’ll want to test.
Nonetheless, including tests like these are exhaustive and provide anyone reading your documentation all the context they’ll ever need to understand your API.
I hope this helps!
Hi, thanks for the answer!
What you wrote makes perfect sense, but you said
The best practice, from a general testing perspective, is to write out test cases that cover each use case you have
But to write out such test cases I have to create separate requests right? So for example request with incorrect url which will return status code 404. Then these “failed” requests will be also visible in documentation, which I don’t think is good practice. I think there is no way to hide specific requests/folders from documentation in Postman.
To answer your question, that is correct. Every request in postman will have to cover its own use case, and you can isolate these into their own folder under the collection for better organization.
In regards to the documentation, thats purely up to you whether you want them to be included. It may not be best practice, or bad practice, but I can see how it clutters documentation. Since I don’t publish too often, I’m not certain if theres a way to exclude a folder. However, you can make it a separate collection, and this way, these tests wouldn’t make it into your documentation.
Hope that helps!