如何使用@Suite注释运行JUnit5套件

问题描述 投票:0回答:2

我已经尝试解决这个问题一年多了,但我无法运行 JUnit5 套件,除非我使用 JUnit4 @RunWith 注释,这给我带来了一些其他问题。我正在尝试使用较新的

@Suite
注释,但无论我尝试什么(命名以 IT 结尾的测试、Test、通过 IntelliJ 运行、通过 maven 运行、包含标签、排除标签等),我总是得到测试没有发现错误。我想我在这个阶段真的已经没有想法了,并且希望有人指出我可能仍然缺少什么。

<?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>org.example</groupId>
    <artifactId>pact</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>8</java.version>
        <maven.compiler.version>1.8</maven.compiler.version>
        <maven.target.version>1.8</maven.target.version>
        <maven.surefire.version>3.0.0-M5</maven.surefire.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite-api</artifactId>
            <version>1.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>vcservice</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.surefire.version}</version>
                <configuration>
                    <testSourceDirectory>src/test/java/com/hmhco/vcservice</testSourceDirectory>
                    <trimStackTrace>false</trimStackTrace>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

class GoogleTest {

    @Tag("google")
    @Test
    void testing() {
        System.out.println("testing google");
    }
}

非常感谢。

更新

添加了更多依赖项,一次又一次的失败让我遇到了这个新错误并更新了 pom 文件。

import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

@SelectPackages("com.my.package")
@IncludeTags("google")
@Suite
public class TestSuiteGoogle {
}
<?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>org.example</groupId>
    <artifactId>pact</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>8</java.version>
        <maven.compiler.version>1.8</maven.compiler.version>
        <maven.target.version>1.8</maven.target.version>
        <maven.surefire.version>3.0.0-M5</maven.surefire.version>
        <junit.jupiter.version>5.0.0-M2</junit.jupiter.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite-api</artifactId>
            <version>1.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite-engine</artifactId>
            <version>1.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.7.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
          <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>5.7.0</version>
            <type>pom</type>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>vcservice</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.surefire.version}</version>
                <configuration>
                    <testSourceDirectory>src/test/java/com/hmhco/vcservice</testSourceDirectory>
                    <trimStackTrace>false</trimStackTrace>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.0-M2</version>
                    </dependency>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-logger-api</artifactId>
                        <version>2.21.0</version>
                        <!-- to get around bug https://github.com/junit-team/junit5/issues/1367 -->
                        <scope>test</scope>
                        <optional>true</optional>
                    </dependency>
                </dependencies>

            </plugin>
        </plugins>
    </build>

</project>

enter image description here

enter image description here

java maven junit5 maven-surefire-plugin suite
2个回答
3
投票

Junit 5 用户指南指出:

除了 junit-platform-suite-api 和 junit-platform-suite-engine 工件之外,您还需要至少一个其他测试引擎及其对类路径的依赖项。

您的项目不包括

junit-platform-suite-engine
。请将以下依赖项添加到您的项目中:

        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite-engine</artifactId>
            <scope>test</scope>
        </dependency>

0
投票

我遇到了与您在上面的帖子中描述的相同的问题,我用以下解决方案修复了它:

  • 在 .pom 文件中添加 junit-platform-suite-engine 依赖项。

    org.junit.platform junit 平台套件引擎 1.11.3 测试
  • 在 Surefire 中使用您的测试套件类设置参数。

    com.x.y.z.TestSuiteClass
  • 先运行mvn clean,然后运行mvn编译测试

© www.soinside.com 2019 - 2024. All rights reserved.