【发布时间】: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。