

Usually API testing requires a huge amount of boilerplate code. In case, something breaks down, these tests will fail.Īnd that’s all it takes. This is how it should look like inside Postman:Įverything seems to be fine with this API as all our test cases have passed. Var contentTypeHeaderExists = responseHeaders.hasOwnProperty("Content-Type") Write the following snippet in the test editor:

If yes, we’ll check if it’s equal to application/json. We’ll first check if contentTypeHeaderExists is true. Remember that as Postman tests are just Javascript code, we can add conditionals like if to our code.We’ll assign the result of the responseHeaders.has() function to a variable contentTypeHeaderExists. Select the “Response headers: Content-Type header check” snippet. responseHeaders is a dictionary available inside the Postman test sandbox which contains all the headers returned in the response.We want to make sure that the Content-Type header exists and it is equal to “application/json” Let’s add another test for the Content-Type header.In the tests tab, Postman tells us that the test has failed (expectedly).
Postman script automation code#
Just to see how things look when the API does not behave as expected, let’s change the status code in the test to 201 and send the request again.We see that this test has passed as the correct status code is returned. We do not need to make any changes in this case. Scroll down in the snippets editor and click on the “Status code: Code is 200” snippet.We’ll check if the status code is 200, and if the Content-Type header is “application/json”. Postman renders the results in the “Tests” tab as well as in the Collection runner, depending on the boolean values present in this dictionary.įor our first test, we’ll send a GET request to to find out the status of the API. Postman expects the special tests object to be populated with a description and the result of the test. We’re going to use a new demo API that we have set up –, and which you can use while following this tutorial. Let’s write a simple test to give you a feel of how everything fits together. You’ll also be able to add custom snippets in a future version. This list will be updated as Postman’s test capabilities improve. This makes writing simple tests extremely fast. You can click on any snippet title and the corresponding code snippet will be appended to the test editor. To the right of the test editor, there is a list of snippets. When you open Postman, you should see the ‘Tests’ section in the request editor area. This is the first tutorial in the series. To get you started quickly we are publishing a two-part tutorial series, where we’ll go from a simple test to writing complicated tests and running them inside the Collection Runner. One of Postman’s most powerful features is its ability to run automated tests on your requests.
