我想过滤我的 Junit 5 测试用例,我使用注释 @Tag("type_test")
。我使用 maven 使用命令 mvn -Dtests=a test
运行测试,但它运行所有情况。例如,我有两个方法,我只想运行带有 @Tag("a")
的方法,但控制台中的结果是“Hello word 1”和“Hello word 2”。请参阅示例代码。
static Properties properties = null;
@BeforeAll
public static void setUp() throws Throwable {
properties = CommonMethods.loadConfigPropertiesFile();
RestAssured.baseURI = properties.getProperty("BASE_HOST");
}
@Test
@Tag("a")
public void test1() {
System.out.println("hello word 1");
}
@Test
@Tag("b")
public void test2() {
System.out.println("hello word 2");
}
}
pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<properties>
<includeTags>${tests}</includeTags>
</properties>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0-M2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0-M2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0-M2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0-M2</version>
</dependency>
</dependencies>
最佳答案
您使用的版本和库已过时。请重试:
- Maven Surefire 2.22.1(更好:3.0.0-M3)
- JUnit Jupiter 5.3.2(更好:5.4.0-M1)
请参阅此示例 pom.xml
文件,其中还介绍了如何过滤标签:
<build>
<plugins>
<!-- JUnit 5 requires Surefire version 2.22.1 or higher -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<groups>a</groups>
<!-- excludedGroups>slow</excludedGroups -->
</configuration>
</plugin>
</plugins>
</build>
来源:https://github.com/junit-team/junit5-samples/blob/master/junit5-migration-maven/pom.xml
有关如何使用 Maven 配置 JUnit 平台的更多详细信息,请参阅 JUnit 5 用户指南 https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven或 Maven Surefire 文档:https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html
关于java - 使用 Maven 使用注释 @Tag ("name_test") 过滤 JUnit 5 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54188943/