Home Junit 5 Junit 5 with allure reports example

Junit 5 with allure reports example

Allure is an open-source framework designed to create test execution reports. In this article you will see a java maven project that integrating Junit 5 with allure and how to generate some cool reports by executing test cases.

Technologies used in following examples :

  1. Junit 5.5.2
  2. Maven 3
  3. Java 8
  4. Spring Tool Suite 3.9.8
  5. allure 2.13.0
  6. aspectjweaver 1.9.4

Project Structure :

Junit 5 allure reports example

1. Maven configuration for Allure and JUnit 5

pom.xml :

You need to configure allure-maven plugin and allure-junit5 dependency in pom.xml . Following is the maven dependency and plugin configuration in pom.xml for the Junit 5 with allure example.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.javabydeveloper.allure</groupId>
    <artifactId>junit5-allure-report-example</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <junit.api.version>5.5.1</junit.api.version>
        <junit.jupiter.version>5.5.2</junit.jupiter.version>
        <junit.platform.version>1.3.1</junit.platform.version>
        <aspectj.version>1.9.4</aspectj.version>
        <allure.version>2.13.0</allure.version>
		<allure.cmd.download.url>
		   https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline
		</allure.cmd.download.url>
    </properties>

    <dependencies>
        <!-- Test API(s) -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.api.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- Parameterized Tests -->
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-params</artifactId>
			<version>${junit.jupiter.version}</version>
			<scope>test</scope>
		</dependency>
        <!-- Test engine(s) -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-junit5</artifactId>
            <version>${allure.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>io.qameta.allure</groupId>
                <artifactId>allure-maven</artifactId>
                <version>2.10.0</version>
                <configuration>
                    <reportVersion>${allure.version}</reportVersion>
                    <allureDownloadUrl>
                      ${allure.cmd.download.url}/${allure.version}/allure-commandline-${allure.version}.zip
                    </allureDownloadUrl>
                </configuration>
            </plugin>
            <!-- Maven plugin to use perticular java version to compile code -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
        </plugins>
    </build>
</project>

2. Writing Junit 5 tests

In this example I have used only Junit 5 specific annotations to create tests, you can use allure annotations as well, like @Step and @Description etc. To demonstrate test reports I have created couple of Junit 5 test cases in this example such as Timeout Tests, Parameterized tests, Repeated tests, Tag based tests and Display names tests.

You will see all the tests cases from our git hub repository, you will find repository url end of this article.

3. Running Junit 5 tests and generating allure reports

Allure results will appear in allure-results folder under the project root. To generate html report and automatically open it in a web browser, run the following command:

Navigate to project root directory and issue following command to run junit 5 tests and report.

$ mvn clean test allure:report

Run the following command to open the generated reports in browser.

$mvn allure:serve

3.1. Generated Reports :

The allure generated reports looks like in the following image.

3.2. Filter and Run tag-specific test cases and generate allure report :

You run the following command to run only test cases based on tags.

$ mvn clean test -Dgroups="api | performance" allure:report

There are 2 api tests and 1 performance test in application, the following image shows you the allure report results.

Junit 5 with allure filter and tag specific tests

4. Conclusion

In this article we have covered Junit 5 with allure integration using maven and generating and open results for several use cases.

You can download source code from our github repository.

References

  1. Allure document
  2. Junit 5 maven example
  3. Junit 5 tags and filter tests for execution

Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

2 COMMENTS

  1. Superb! You rocks! You really helped me to set up pom file! Just want to add that a new version of Aspectj for some reason doesn’t work: 1.9.5 – it give an error: Dependency ‘org.aspectj:aspectjweaver:1.9.5’ not found, however I can find it – but it is not related with your article – just for extra information

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles