[英]How to run JUnit5 suite using the @Suite annotation
一年多来我一直在努力解决这个问题,但除非我使用 JUnit4 @RunWith 注释,否则我无法运行 JUnit5 套件,这给我带来了一些其他问题。 我正在尝试使用较新的@Suite
注释,但无论我尝试什么(命名以 IT 结尾的测试、测试、通过 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>
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.