Creating A Test Automation Portfolio Episode 4: API testing for Restful Booker using Postman

Intro

This blog continues my journey of creating a test automation portfolio, which has been ongoing for a few months now. Although I’ve covered API testing in an earlier post, I wanted to include Postman as the tool of choice, partly because its something we use at work, partly because I want to get better at it and partly because its such an industry leading tool.

I’ve already had a go at using a Newman call to run a Postman collection through the Microsoft DevOps Azure CI Pipeline – you can check out the code for that here as I won’t be referring to it again in this post. I find terms like DevOps and CI a bit scary when I read them in blogs, and I don’t want to put you off reading on, Postman is a super simple one to start with I promise!

Postman Image

The Project

I continued my magpie tendency and avoided reinventing the wheel by Googling “Postman tests for Restful Booker API” and hit gold with an amazing GitHub Repo from Joanna Denni. She had created a really great and easy to read repo with some excellent README instructions.

The great thing about the pretend hotel booking website Restful Booker is there is a limited number of really clearly documented API endpoints, so its easy to get a good amount of test coverage, and practice testing GET, POST, PATCH, PUT as well as DEL calls. Mark Winteringham (its co-creator) recently tweeted about the sheer number of requests which are sent to the public server, so there must be plenty of other people learning in this particular sandpit.

Tweet from Mark Winteringham About the Popularity of Restful Booker

Key Features

I wanted to develop what was already in the repo to support my learning by:-

  • Install the original repo, and get it up and running.
  • Get the existing tests to run (very straightforward, also includes pre-request script examples which is v. cool)
  • Add in a few new tests from scratch including MOCKs, randomised response variables and params
Example set of tests for a GET endpoint and below actual code snippets (for improved accessibility)
pm.test("Status code should be 200 OK", function () {
    pm.response.to.have.status(200);
});
pm.test("Response Content-Type should be json", function () {
    pm.response.to.be.json;
    pm.response.to.have.header("Content-Type", "application/json; charset=utf-8");
});
pm.test("Response should contain at least one bookingid", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData[0].bookingid).to.not.be.null;
    pm.expect(jsonData[0].bookingid).to.be.a('number');
});

Here is a video of the tests successfully passing against the public Restful Booker site when executed via a Postman Collection Runner. I did 2 iterations which gave me 130 test passes in just a few seconds – not bad eh!

Video of test execution using Postman’s Collection Runner

The Code

I’m still learning the inner circle workings of GitHub, so I have a lingering doubt that I’ve gone about this in the wrong way, but this time instead of forking my own branch I took the original code and generated pull requests for my additional work. The code can be found here:-

https://github.com/joannalaine/postman-restful-booker/pulls

Pull Requests demonstrating the additional work I have added to the original brilliant repo

Lessons Learned

I think the idea of “API” testing is massively offputting to a lot of people, because it feels super techy and “backend”. In reality, particularly when using Postman, I’ve found it way simpler than a lot of UI type automation. It is also very maintainable once you know what you’re doing with variables etc. Feel free to use this code as an example to get you started!

The other thing I’ve found about Postman that I love is the amazing amount of online help available. The Postman learning center is great, and a big shout out to the incredible Danny Dainton, who has helped me out a tonne too.

I’m now over half way through my Test Automation Portfolio, which is a pace I’m really happy with. Yaaaaaaaaay.

Onwards!

4 thoughts on “Creating A Test Automation Portfolio Episode 4: API testing for Restful Booker using Postman

Leave a comment