【问题标题】:JUnit 4 @Test annotation not taken into account when filtering by tags按标签过滤时未考虑 JUnit 4 @Test 注释
【发布时间】:2019-01-03 08:19:12
【问题描述】:

当我尝试使用标签过滤运行测试时,只有那些标记为 JUnit 5 中的 @Test 的测试被执行,而不是那些标记为 JUnit 4 中的@Test 的测试。

关键是如果过滤表达式是“!slow”,它实际上执行没有标签“slow”的测试,而不管使用哪个@Test注解。但是当我使用表达式“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?为什么?
  • 因为我今天添加了 JUnit 5 并且 @Test 注释在不同的包中。这意味着我必须去每个测试类来更改导入语句。这是我不想做的事情。
  • 恕我直言,当 JUnit 4 和 5 添加到现有项目中并且您不想一次迁移所有测试类时,可以在同一个项目中使用 JUnit 4 和 5。

标签: java junit junit4 junit5


【解决方案1】:

您不能在同一个测试中混合使用 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 注释没有任何行为,因此这些注释将被忽略。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    相关资源
    最近更新 更多