我在我的 build.gradle
文件中设置了集成测试:
task integrationSetup(dependsOn: jar, type: Exec) {
workingDir "$projectDir/resources/integration"
commandLine 'sh', './start_service.sh'
}
task testIntegration(dependsOn: integrationSetup, type: Test) {
testClassesDirs = sourceSets.testIntegration.output.classesDirs
classpath = sourceSets.testIntegration.runtimeClasspath
ignoreFailures = true
}
task integrationTearDown(dependsOn: testIntegration, type: Exec) {
workingDir "$projectDir/resources/integration"
commandLine 'sh', './stop_service.sh'
}
testIntegration.mustRunAfter integrationSetup
testIntegration.finalizedBy integrationTearDown
integrationTearDown.mustRunAfter testIntegration
但是,自从将 Gradle Wrapper 升级到版本 4+ 后,任务不再正确执行。最后的拆卸永远不会运行,服务会继续。版本 3 和版本 4 之间发生了什么变化以改变这种行为。非常令人沮丧的 Gradle 在没有警告或弃用通知的情况下就这样做了。
一个愚蠢的选择是降级 Gradle 包装器版本(可以确认此设置在 3.1 上仍然有效)。但在 IMO 看来,这不是必需的。
更新:针对每个用户@Opal 进行了一些更改。但是仍然存在问题,如果在集成测试期间出现任何错误,最终的拆卸将不会运行。
> Task :compileTestIntegrationJava
Putting task artifact state for task ':compileTestIntegrationJava' into context took 0.0 secs.
file or directory '/home/project/cleaner/src/testIntegration/java', not found
file or directory '/home/project/cleaner/src/testIntegration/java', not found
Executing task ':compileTestIntegrationJava' (up-to-date check took 0.072 secs) due to:
Output property 'destinationDir' file /home/project/cleaner/build/classes/java/testIntegration has changed.
Output property 'destinationDir' file /home/project/cleaner/build/classes/java/testIntegration/com has been removed.
Output property 'destinationDir' file /home/project/cleaner/build/classes/java/testIntegration/com/project has been removed.
All input files are considered out-of-date for incremental task ':compileTestIntegrationJava'.
file or directory '/home/project/cleaner/src/testIntegration/java', not found
Compiling with JDK Java compiler API.
/home/project/cleaner/src/integration/java/com/project/cleaner/CleansRequestsTests.java:415: error: reached end of file while parsing
}
^
1 error
:compileTestIntegrationJava (Thread[Daemon worker Thread 8,5,main]) completed. Took 0.162 secs.
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileTestIntegrationJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output.
BUILD FAILED in 8s
8 actionable tasks: 8 executed
Stopped 0 worker daemon(s).
最佳答案
在讨论中,事实证明 OP 想要在运行测试之前停止启动的服务,无论是什么,例如编译错误。可以使用以下脚本完成:
ext.integrationTearDown = {
workingDir "$projectDir/resources/integration"
commandLine 'sh', './stop_service.sh'
}
task(type: Exec, 'stop_service', integrationTearDown)
gradle.buildFinished {
exec integrationTearDown
}
testIntegration.dependsOn integrationSetup
testIntegration.finalizedBy stop_service
使用这段代码,服务将在每个 build - 成功后停止。避免这种行为 BuildResult
传递给 buildFinished
可用于确定所需的行为。
关于java - Gradle 任务在测试后未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47193479/