How to Use Assertions in the Scripting Window?
Let’s say, for starters, we just want to ensure that whether the request is successful or not. We can know that by checking its status code. So the Expected outcome is that a Status Code should be 200.
pm.test(“First Test”,function(){
pm.response.to.have.status(200)
})
The name of the test is First Test and in the function, we checking its status which is expected to be 200. You can see the result in the Test Results section. If it shows Fails then it means that your response contains any other status code.
You can write as many tests as you want in the Tests section one after other as per your requirements. Let’s see one other example of a Test, it will test whether the response time is less than 200ms.
pm.test(“Response time is Less than 200ms”,function(){
//We are expecting responseTime to be less than 200
pm.expect(pm.response.responseTime).to.be.lessThan(200)
})
You can see that I get Two results in which the first one is Passed but the second Fails, so by this I can work on my API to reduce the response time. By this, Postman can help us to figure out the real-time problems that we could face in our application.
Assertions in Postman and How to Use that in Scripting Window
Assertions are checks or validations we can include in our API requests to ensure that the response from the server meets certain criteria. These criteria could be based on the response’s status code, headers, body content, or any other aspect of the response. Postman provides a variety of built-in assertion options that we can use to validate the responses you receive during API testing. These assertions help us to confirm that our APIs are behaving as expected.
Assertions are mainly the tests that we want to execute after writing of code. In this case, when our APIs are ready, we just want to make sure that they are perfectly working or not. For this Postman provides us a Test section where we can write a test for the request. Postman test uses Chai Assertion Library BDD syntax.
Contact Us