按标签过滤时不考虑JUnit 4 @Test批注

时间:2019-01-03 08:19:12

标签: java junit junit4 junit5

当我尝试运行带有标签过滤的测试时,仅执行JUnit 5中标有@Test的那些,而不执行JUnit 4中标有@Test的那些。

重点是,如果过滤表达式为“!slow”,则无论使用哪个@Test批注,它实际上都将执行不带标签“ slow”的测试。但是,当我使用表达式“ slow”进行过滤时,如果具有JUnit 4中的@Test,则不会执行带有此标记的测试。

我知道添加标签时可以更改为新的批注,但是不必对已经进行的测试执行此操作就很好了。

我已将此导入到pom中

<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-engine</artifactId>
  <version>5.1.0</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.junit.platform</groupId>
  <artifactId>junit-platform-launcher</artifactId>
  <version>1.3.2</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.junit.vintage</groupId>
  <artifactId>junit-vintage-engine</artifactId>
  <version>5.1.0</version>
  <scope>test</scope>
</dependency>

我要运行的测试是

import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Tag;
public class Test {
  @org.junit.Test
  @Tag("slow")
  public void test() {
    assertTrue(true);
  }
}

1 个答案:

答案 0 :(得分:0)

您不能在同一测试中混合使用JUnit 4和JUnit 5批注。因此,您的方法不起作用。解决方案可能是将这些标记的测试迁移到JUnit 5。

背景:JUnit平台使用不同的测试引擎来发现和执行测试。 junit-vintage-engine可以处理针对JUnit 4 API编写的测试,其中测试方法用@org.junit.Test注释。 junit-jupiter-engine可以处理针对JUnit Jupiter API(通常称为JUnit 5)编写的测试,其中测试方法用@org.junit.jupiter.api.Test注释。每个引擎仅具有关于其发现的测试方法的知识。对于JUnit Jupiter注释,junit-vintage-engine没有任何行为,因此这些注释将被忽略。