In this short blog post I will share the tips on how to start writing API tests with Serenity framework and Rest Assured library
Target audience: QA Automation engineers
Origins of Serenity
In one of my previous posts, I wrote couple of words about Serenity and specifically about one of issues, which my team have faced not so long ago. It was tricky investigation, but, fortunately, issue was resolved.
Serenity framework (also known as Thucydides) - it is open - source framework for test automation. Framework has a great integration with Selenium WebDriver; it gives the choice whether to use BDD approach (with Cucumber of JBehave tools) or pure JUnit tests. One of the framework benefits - it is full - featured test report, which can be generated after the test run and used even as “living documentation” for the functionalities.
If you want to learn about Serenity more - please start with this great reference manual. It is definitely helplful.
Here comes the REST
REST architecture approach is widely used in modern software development now. A lot of API’s created using this approach and they should be carefully tested and automated too. One of the most used library in Java world for test automation of REST API is Rest Assured. It makes REST testing a lot easier by providing good abilities including different types of authentication, powerfull response validation both for XML and JSON based messages and more.
Thankfully, Serenity has an integration with Rest Assured library just ‘from the box’.
Personally, I have used Rest Assured official examples on GitHub for reference and learning library features. Also, I can highly recommend to use Bas Dijkstra’s blog post series on REST test automation (particularly, his Rest Assured training materials also on GitHub).
Enough of theory - let’s do some practice!
There are lot of ways of starting new project with Serenity and Rest Assured. I will describe only the simplest one.
In my current example I will use JBehave BDD tool with Serenity. For Cucumber tool the steps will be pretty similar.
- Create new Java project in Intellij IDEA. While creating - choose to use serenity-jbehave-archetype for generating new Maven - based project;
- Add serenity-rest-assured dependency to the pom.xml;
- Rebuild the project;
Turn BDD scenarios into real code.
Our practice example will be based on automating the Google Map API (particularly base and reverse geo coding functionality). JBehave BDD scenarios are placed at test/resources/stories/GeoCoding.story file:
Scenario is set of steps. Each JBehave step has own implementation in Java code. In our case JBehave step implementations will be placed at GeoApiStepDefinitions.class:
Serenity framework just adds additional layer of abstraction: now one BDD step can include one or more different Serenity steps. Serenity steps are just methods, but with special annotation @Step. We are implementing steps by adding an additional Steps class for our API. Here we will use Serenity-Rest-Assured lib for sending requests and validating responses:
As all parts of our test automation puzzle are revealed - we can run our test scenarios in order to make sure that scenarios are correct and return the expected results. For this purpose just run AcceptanceTestSuite.class for running all JBehave stories in scope.
The final step is to generate serenity report by running Maven command “serenity:aggregate”. Report will be generated and placed in target/site/serenity directory of the project. In order to open the report - open index.html file.
As you can see, it is easy to integrate and use RestAssured library with Serenity framework and BDD approach. Of course, it is separate question - whether to use RestAssured with an additional level of abstraction as BDD scenarios. But in some cases it can add value - specifically if the project needs a living and understandable documentation for all features, including REST services part.
All code examples available on Github: serenity-rest