5

How can one run junit tests and get some nice (e.g. gradle-like html) reporting from them without project source code (e.g. tests + dependencies packaged in an uber jar)?

I can package the tests and maven into runnable jar with maven assembly plugin, run this jar with help of How to run JUnit test cases from the command line but how can one create some nice test results report then?

Community
  • 1
  • 1
Kamil Roman
  • 973
  • 5
  • 15
  • 30

1 Answers1

0

You can add Main class with main method that calls JUnit4 tests and adds an AllureJunit4 listener.

package samples;

import io.qameta.allure.junit4.AllureJunit4;
import org.junit.runner.JUnitCore;

public class Main {
    public static void main(String[] args) {
        JUnitCore engine = new JUnitCore();
        engine.addListener(new AllureJunit4());
        engine.run(SampleTest.class);
    }
}

Let us say that your pom.xml looks as follows.

<?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.test.classes</groupId>
    <artifactId>junit-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>18</maven.compiler.source>
        <maven.compiler.target>18</maven.compiler.target>
        <aspectj.version>1.8.10</aspectj.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-junit4</artifactId>
            <version>2.20.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>2.0.5</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M8</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.4.2</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>samples.Main</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

and assembly.xml

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 https://maven.apache.org/xsd/assembly-2.1.1.xsd">
    <id>fat-tests</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>test</scope>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/test-classes</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**/*.class</include>
            </includes>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
    </fileSets>
</assembly>

Your test can be enriched with Allure annotations.

package samples;


import io.qameta.allure.Description;
import io.qameta.allure.Feature;
import org.junit.Assert;
import org.junit.Test;

public class SampleTest {
    @Test
    @Description(
            """
            Here we can provide longer description.
            Even the whole user story in bullet points
              * As a ...
              * I want to ...
              * in order to ..."""
    )
    @Feature("Feature 1")
    public void first(){
        System.out.println("first");
        Assert.assertEquals("1", "1");
    }

    @Test
    public void second(){
        System.out.println("second");
        Assert.assertEquals("2", "3");
    }
}

To pack the jar:

mvn clean package

To run tests

java -jar junit-maven-1.0-SNAPSHOT-fat-tests.jar

It will create allure-results directory.

Finally, you can use the Allure command line to serve the results.

allure serve allure-results

The results