如何在gradle 4.6+中将JUnit 5测试实例生命周期切换为"每类"?

The*_*ect 2 gradle junit5

由于gradle 4.6支持JUnit 5,具有以下配置.

test {
    useJUnitPlatform()
}
Run Code Online (Sandbox Code Playgroud)

似乎改变testinstance生命周期的旧方法不起作用.

junitPlatform {
    // ...
    configurationParameter 'junit.jupiter.conditions.deactivate', '*'
    configurationParameters([
        'junit.jupiter.extensions.autodetection.enabled': 'true',
        'junit.jupiter.testinstance.lifecycle.default': 'per_class'
    ])
    // ...
}
Run Code Online (Sandbox Code Playgroud)

https://junit.org/junit5/docs/current/user-guide/#running-tests-build-gradle-config-params

如何在gradle 4.6+中将JUnit 5测试实例生命周期切换为"每类"?

The*_*ect 5

除了使用eee的答案中描述的系统属性外,junit-platform.properties还可以使用文件.junit-platform.properties文件可以放入src/test/resources.

junit-platform.properties

junit.jupiter.testinstance.lifecycle.default = per_class
Run Code Online (Sandbox Code Playgroud)

以下是JUnit团队的答案.https://twitter.com/junitteam/status/970014414891094018

在任务中将其设置为systemProperty,或将其设置在junit-platform.properties文件中.后者是更强大的机制,因为它也将在IDE中应用.