Overview: This lesson will cover how to create effective test automation in Postman, then run it as a collection, on the command line, and eventually in Jenkins. This will include: how to use Postman snippets to create tests quickly, data generation through pre-request scripts, using variables in tests, test setup using the Postman createRequest API, installing and running the Newman tool, and running a Postman collection in Jenkins and viewing the result.
Note:
For maximum benefit, please view the course video for dynamic demonstrations.
So far, we've learned a little bit about how to use Postman and we've gone over our testing strategy. And we've learned quite a bit about how the Restful Booker API works, so now we're ready to finally start writing some test automation.
If we open up the endpoint that we'd like to test, we can see that there's a tab on the right that says “Tests.” And what this is, is a little JavaScript Sandbox where you can do checking of your responses and a lot of other different things. And if you're familiar with JavaScript, then that's going to help you out a lot, but Postman provides a lot of shortcuts as well.
For example, here’s a basic test just to make sure that we got a 200 OK response.
pm.test(“Status code is 200”, function () {
pm.response.to.have.status(200);
});
We're doing the ping, we expect the 200 OK. The format is the basically the Postman object as the test, or have a description of the test right here, status code is 200, and then our test function is Postman response to have status 200. This is using the chai assertion framework for these BDD style assertions.
Now that we have this test, we can run our API, and we'll see that it failed because, instead of 200 OK, which you would expect for a ping, we got 201 Creative. And we see in the results it said insertion error, that we expected 200 but got 201.
That's a very basic, simple test that's easily available in Postman.
Quiz:
Note: Chapter 4 has been divided into multiple sub-chapters. You'll find the Quiz on the last sub-chapter, i.e. Chapter 4.4. Read on!