独立运行JUnit测试并生成报告

kse8i1jr  于 2023-01-30  发布在  其他
关注(0)|答案(1)|浏览(164)

如何运行junit测试并从他们那里得到一些好的(例如类似gradle的html)报告,而不需要项目源代码(例如测试+依赖项打包在一个uber jar中)?
我可以用maven汇编插件将测试和maven打包到可运行的jar中,在How to run JUnit test cases from the command line的帮助下运行这个jar,但是如何创建一些漂亮的测试结果报告呢?

agxfikkp

agxfikkp1#

可以使用main方法添加Main类,该方法调用JUnit4测试并添加AllureJunit4侦听器。

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);
    }
}

假设您的pom.xml如下所示。

<?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>

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>

您的测试可以使用Allure注解进行丰富。

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");
    }
}

要 Package 广口瓶:

mvn clean package

运行测试

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

它将创建allure-results目录。
最后,您可以使用Allure命令行提供结果。

allure serve allure-results

The results

相关问题