我正在清理我们的测试套件,我看到的一件事是,在展开的参数化 Spock 测试中,maven Surefire 正在报告无数据标题行的额外“测试”。这是使用 Spock 2.3。 这是一个示例测试文件:
package com.sample.utility
import org.joda.time.LocalDate
import spock.lang.Specification
class DateUtilityTest extends Specification {
def "#years years from today being over 21 is #result"() {
expect:
DateUtility.isOver21(new LocalDate().plusYears(years).toDate()) == result
where:
years || result
-20 || false
-21 || true
-22 || true
}
def "#years years from today being over 18 is #result"() {
expect:
DateUtility.isOver18(new LocalDate().plusYears(years).toDate()) == result
where:
years || result
-17 || false
-18 || true
-19 || true
}
}
当然,期望运行 6 个测试,每种方法 3 个,如果我在那里运行测试,这就是 Intellij 报告的方式。但是,当运行 mvn test 时:
[INFO] Running com.sample.DateUtilityTest
[INFO] Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.025 s -- in com.sample.DateUtilityTest
这也是 Jenkins 中报告的方式,带有测试名称:
pom中的插件定义是
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.1</version>
</dependency>
</dependencies>
</plugin>
有没有办法进行配置,以便 Maven 不会报告标题行的额外测试?
这是此问答的准重复。正如您所看到的,这是 JUnit 5 平台的一项功能,绝不限于 Spock,还会影响参数化的 JUnit Jupiter 测试。
至于如何让你报告的名字更加人性化,我建议是这样的:
@Unroll("age #years years, result is #result")
def "check if at least 21 years old"()
然后,测试容器将有一个干净的名称,其中的每个参数化测试也将有一个干净的名称。它在 IntelliJ IDEA 等 IDE 中看起来也会更好。