DEV Community

Dennis Whalen for Leading EDJE

Posted on

API Testing with Java and REST Assured - Allure reporting

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!

Allure Reporting

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Hopefully you'll see a successful test run:
Alt Text

View results

Now take a look at the files in target/allure-results. These are testng result files that Allure uses to create a report:

Alt Text

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:

Alt Text

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:

Alt Text

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:

Alt Text

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!
Alt Text

It's a wrap!

Hopefully this blog post gives you some insights on using Allure to generate reports for your REST Assured tests. I should mention, Allure is not limited to REST Assured reports, and is not even limited to Java-based test frameworks. In addition to Java, Allure supports frameworks for Python, JavaScript, Ruby, Groovy, PHP, .Net, and Scala.

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!


Smart EDJE Image

Top comments (1)

Collapse
 
fatiko16 profile image
fatiko16

hello sir, thank you for this great tutorial, i followed every step but could not get reporting actually, i feel like there are some gaps about mvn test site part , is it possible for you to give some explanation about site part of the command and explanation about pom.xml file, i mean the necessary configurations in pom.xml to generate report by using mvn test site, i can generate the reports by cloning your repository but i would like to learn more details about report generation