如何让带有@Tag 注释的JUnit 测试在具有@IncludeTags 的套件中执行?
How to get JUnit tests annotated with @Tag to be executed in suite that has @IncludeTags?
我有以下代码:
测试class:
@Tag("foo")
class SomeIT
{
@Test
public void testSomeStuff()
{
...
}
}
套件class:
@RunWith(JUnitPlatform.class)
@IncludeTags({"foo"})
//@SelectPackages("org.foo")
public class SomeITSuite
{
}
我的pom.xml
:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.foo</groupId>
<artifactId>bar</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.slf4j>1.7.30</version.slf4j>
<version.junit>5.7.0</version.junit>
<version.junit.platform.runner>1.7.0</version.junit.platform.runner>
</properties>
<build>
<plugins>
<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>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
<dependencies>
<!-- for testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${version.junit.platform.runner}</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>integration-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<includes>
<include>**/*ITSuite.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
这会在 Idea 和控制台(使用 mvn clean install -Pintegration-tests
)中执行带有 0 个测试的套件。
如果我恢复注释掉的 @SelectPackages("org.foo")
,它将 运行 所有测试,无论它们是否被标记。我在这里错过了什么?这是一个错误吗?
显然,当 JUnit 扫描 类 时,它不会将以 IT
结尾的作为集成测试。我不得不将它们重命名为 *ITTest
,事情开始起作用了。
如果有人对更多细节感兴趣,可以看看this pull request。
我有以下代码:
测试class:
@Tag("foo")
class SomeIT
{
@Test
public void testSomeStuff()
{
...
}
}
套件class:
@RunWith(JUnitPlatform.class)
@IncludeTags({"foo"})
//@SelectPackages("org.foo")
public class SomeITSuite
{
}
我的pom.xml
:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.foo</groupId>
<artifactId>bar</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.slf4j>1.7.30</version.slf4j>
<version.junit>5.7.0</version.junit>
<version.junit.platform.runner>1.7.0</version.junit.platform.runner>
</properties>
<build>
<plugins>
<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>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
<dependencies>
<!-- for testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${version.junit.platform.runner}</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>integration-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<includes>
<include>**/*ITSuite.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
这会在 Idea 和控制台(使用 mvn clean install -Pintegration-tests
)中执行带有 0 个测试的套件。
如果我恢复注释掉的 @SelectPackages("org.foo")
,它将 运行 所有测试,无论它们是否被标记。我在这里错过了什么?这是一个错误吗?
显然,当 JUnit 扫描 类 时,它不会将以 IT
结尾的作为集成测试。我不得不将它们重命名为 *ITTest
,事情开始起作用了。
如果有人对更多细节感兴趣,可以看看this pull request。