在 maven 中正确包含 jUnit5 @Nested classes when 运行 single class
Correctly include jUnit5 @Nested classes when running single class in maven
在 JUnit 5 中执行的 @Nested
classes 它们被命令 运行 在关闭 class 中的所有测试之后。如果我的目标是 运行 一个封闭的 class 并且它是嵌套的 classes,我如何使用 maven 强制执行相同的行为?是否有命令行或 pom.xml 修改以使此示例测试通过?
package example;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
public class SomeTopLevelTest {
private static boolean nestedDone = false;
@Test
void test1() {
Assertions.assertFalse(nestedDone,
"Nested classes should execute after the enclosing tests");
}
@Nested
class SomeNestedTest {
@Test
void test2() {
nestedDone = true;
}
}
@AfterAll
static void recheck() {
Assertions.assertTrue(nestedDone, "Nested class should be executed");
}
}
这确实通过了 IDE:
但在命令行中没有,如果我尝试指定名称:
mvn test -Dtest=example.SomeTopLevelTest
[ERROR] Failures:
[ERROR] SomeTopLevelTest.recheck:27 Nested class should be executed ==> expected: <true> but was: <false>
mvn test -Dtest=example.SomeTopLevelTest*
[ERROR] Failures:
[ERROR] SomeTopLevelTest.test1:14 Nested classes should execute after the enclosing tests ==> expected: <false> but was: <true>
@Nested
classes 未执行的问题是一个已知问题,并且已在两个 JUnit5 and Surefire 问题跟踪器中报告,但截至目前仍未解决。
当前状态(使用 Maven 3.6.3、Junit5 5.7.2、Surefire 2.22.2 至 3.0.0-M5 测试):
一个。未选择测试
mvn test
按预期执行所有测试 classes 的结果:首先包含 classes 的方法,然后是 @Nested
classes 的方法,逐级
乙。选择测试标准方式
mvn test -Dtest=example.SomeTopLevelTest
这显然会触发使用以下模式的默认万无一失 excludes
:
<excludes>
<exclude>**/*$*</exclude>
</excludes>
如果 A 是一个谜,为什么它不会发生,但可以通过显式清除排除模式来覆盖此行为:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude/>
</excludes>
</configuration>
</plugin>
如果不修改 pom.xml 似乎是不可能的。
这 不 解决这个问题中发布的问题,因为嵌套的 classes 仍然首先执行。
C。使用带 -Dtest 参数的通配符
mvn test -Dtest=example.SomeTopLevelTest*
这明确选择了所有嵌套的 classes,但是 - 如问题中所述 - 导致首先执行嵌套的 classes,因此它不是解决方案。
D.使用包括
mvn test -DtestName=example.SomeTopLevelTest
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>${testName}</include>
</includes>
</configuration>
</plugin>
显然 include
模式与 -Dtest
参数的工作方式完全不同,因为这是最终通过问题测试的解决方案。使用此设置,testName
可能是单个 class、通配符模式或正则表达式
example.SomeTopLevelTest
在单个 class 中执行所有测试方法
example/*
- 包示例中的所有测试(包括嵌套),但不包括子包
example/**
- 包和子包中的所有测试
- 高级正则表达式,也受支持
在 JUnit 5 中执行的 @Nested
classes 它们被命令 运行 在关闭 class 中的所有测试之后。如果我的目标是 运行 一个封闭的 class 并且它是嵌套的 classes,我如何使用 maven 强制执行相同的行为?是否有命令行或 pom.xml 修改以使此示例测试通过?
package example;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
public class SomeTopLevelTest {
private static boolean nestedDone = false;
@Test
void test1() {
Assertions.assertFalse(nestedDone,
"Nested classes should execute after the enclosing tests");
}
@Nested
class SomeNestedTest {
@Test
void test2() {
nestedDone = true;
}
}
@AfterAll
static void recheck() {
Assertions.assertTrue(nestedDone, "Nested class should be executed");
}
}
这确实通过了 IDE:
但在命令行中没有,如果我尝试指定名称:
mvn test -Dtest=example.SomeTopLevelTest
[ERROR] Failures:
[ERROR] SomeTopLevelTest.recheck:27 Nested class should be executed ==> expected: <true> but was: <false>
mvn test -Dtest=example.SomeTopLevelTest*
[ERROR] Failures:
[ERROR] SomeTopLevelTest.test1:14 Nested classes should execute after the enclosing tests ==> expected: <false> but was: <true>
@Nested
classes 未执行的问题是一个已知问题,并且已在两个 JUnit5 and Surefire 问题跟踪器中报告,但截至目前仍未解决。
当前状态(使用 Maven 3.6.3、Junit5 5.7.2、Surefire 2.22.2 至 3.0.0-M5 测试):
一个。未选择测试
mvn test
按预期执行所有测试 classes 的结果:首先包含 classes 的方法,然后是 @Nested
classes 的方法,逐级
乙。选择测试标准方式
mvn test -Dtest=example.SomeTopLevelTest
这显然会触发使用以下模式的默认万无一失 excludes
:
<excludes>
<exclude>**/*$*</exclude>
</excludes>
如果 A 是一个谜,为什么它不会发生,但可以通过显式清除排除模式来覆盖此行为:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude/>
</excludes>
</configuration>
</plugin>
如果不修改 pom.xml 似乎是不可能的。
这 不 解决这个问题中发布的问题,因为嵌套的 classes 仍然首先执行。
C。使用带 -Dtest 参数的通配符
mvn test -Dtest=example.SomeTopLevelTest*
这明确选择了所有嵌套的 classes,但是 - 如问题中所述 - 导致首先执行嵌套的 classes,因此它不是解决方案。
D.使用包括
mvn test -DtestName=example.SomeTopLevelTest
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>${testName}</include>
</includes>
</configuration>
</plugin>
显然 include
模式与 -Dtest
参数的工作方式完全不同,因为这是最终通过问题测试的解决方案。使用此设置,testName
可能是单个 class、通配符模式或正则表达式
example.SomeTopLevelTest
在单个 class 中执行所有测试方法
example/*
- 包示例中的所有测试(包括嵌套),但不包括子包example/**
- 包和子包中的所有测试- 高级正则表达式,也受支持