[Postman] The Easiest and Most Popular automated tests in Postman
Ahmed Ramzy
Senior Software Testing Engineer | Bug Hunter | Security Testing | Dedicated to Ensuring Better Secure Websites Worldwide
Postman have a nice feature that allows us to write tests in the tests section, Which helps us in automating most of our manual tasks.
You can simply add these tests by just going to the tests section and click on any test in the right menu.
In this article we will discuss some of these tests in this section.
Lets start by:
1- Automatically check that the status code is 200/201/400 .. etc
By clicking on the selected red snippet, it will add our first test
pm.test("Status?code?is?200",?function?()?
????pm.response.to.have.status(200);
});{
In this case it will pass if you send the request and the response status code is 200, otherwise it will fails.?
you can modify the 200 to be 201,400,404 --> Depends on your test case.
2- Check on a specific header in the response?
By clicking on the selected red snippet, it will add our second test
pm.test("Content-Type?is?present",?function?()?
????pm.response.to.have.header("Content-Type");
});{
In this case it will pass if you send the request and the response header have "Content-Type" Header
you can change to be any other header
3- Check that the response time is bigger/smaller than a certain time
By clicking on the selected red snippet, it will add our third test
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
In this case it will pass if you send the request and the response time is less than 200ms
领英推荐
you can change the margin for the response time based on your test.
4- Check that the response contains a specific string
By clicking on the selected red snippet, it will add our fourth test
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("createdAt");
});
In this case it will pass if you send the request and the response body contains the word "CreatedAt"
5- Check that the response totally equal to a specific string
By clicking on the selected red snippet, it will add our fifth test
pm.test("Body is correct", function () {
pm.response.to.have.body("createdAt");
});
In this case it will pass if you send the request and the response body totally equal to the word "CreatedAt"
6- Check that the JSON Response has a correct value for a specific key.
By clicking on the selected red snippet, it will add our sixth test
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.job).to.eql("leader");
});
In this case it will pass if you send the request and the response body was JSON and has a key named "Job" that has the value "Leader"
-------------------------------------------------------------------------------------------
So now we can add automated tests for all our APIs to ensure that our responses are correct and met the desired results.
Simple note: Postman allows you to add any number of tests for each API.
I hope that this was clear and will help you in your work.
Thanks for reading.