JUnit 4 @Test按标记筛选时不考虑注释



当我尝试使用标签过滤运行测试时,只会执行那些标有 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);
  }
}

不能在同一测试中混合使用 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-vintage-engine 对 JUnit Jupiter 注释没有任何行为,因此这些注释被简单地忽略了。

相关内容

最新更新