In my previous posts I've walked through how to get started with REST Assured and API testing. We've setup our test endpoints and created a variety of test methods that validate those endpoints. You can find the complete code for this series of blog posts in my Github project.
Up until now we've been using the IDE to run the tests and viewing our test results in the terminal window. In this post I want to show you how to generate an HTML report using the Allure reporting framework.
Here we go!
So what is Allure? Allure is an open sourced framework that leverages junit or testng test results to create an HTML report with both high level and detailed views. You can experiment with an Allure sample report to get a general idea of what the report looks like.
Adding reporting to our project
Add the Allure dependency
Let's update the project we've already been working on in previews posts. To get started with Allure we'll need to update our
pom.xml file by adding an Allure dependency:
<dependency> <groupId>io.qameta.allure</groupId> <artifactId>allure-testng</artifactId> <version>2.13.2</version> <scope>test</scope> </dependency>
Add testng.xml file
In my sample project I now have 2 test classes, and I want to run the tests from both classes. To do this I need to create a testng.xml file and put it in the root of my project:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="REST Assured test examples"> <test name="All"> <classes> <class name="SchemaValidation"/> <class name="MyTestClass"/> </classes> </test> </suite>
The file lists the test classes I want to run. Time to run the test!
Run the test suite
After making sure you have started the json server endpoint with
json-server --watch music-db.json, let's just run our test from the command line using maven:
mvn test site
Hopefully you'll see a successful test run:
Now take a look at the files in
target/allure-results. These are testng result files that Allure uses to create a report:
In addition, you should have an
index.html file in your
target/site/allure-maven-plugin folder. Right-click that file and open in browser. The report should open in your browser:
The left nav of the page will give you different views into your tests. Drilling into the suite will give you insights on each individual test:
View test history
Each time you run the report from the command line with the
mvn test site command, new testng json results file will get added in the
allure-results folder. Allure can use those files to include a historical view of your tests. Let's give that a try.
To get started run
mvn test site a few time and watch how the number of file in the
allure-reports folder grows.
No go back a view your report. Select
Suites from the left nav, select one of your tests, and cick
Retries in the right pane. You should see history of test runs for that test:
Of course no one cares about tests that are green. This historical view can give you insights on patterns with failed tests. For example, this test looks pretty flaky!
It's a wrap!
I've barely scratched the surface with this introductory post. Check out the Allure getting started page to learn more.
As I wrap up this Allure post, I am also wrapping up this 4-part series on REST Assured. I've learned a lot working through these posts, and hopefully you've learned something as well.
You can find the complete code for this series of blog posts in my Github project.
Thanks and happy testing!