简体   繁体   English

当JUnit测试失败时,Mark Gradle在Jenkins中构建不稳定

[英]Mark Gradle build unstable in Jenkins when JUnit tests fail

I have a Gradle build in Jenkins with various JUnit tests that are executed as part of the build. 我在Jenkins中使用了各种JUnit测试的Gradle构建,这些测试作为构建的一部分执行。 Now when some of the tests fail the complete build is marked as failed - because Gradle says the build failed. 现在,当某些测试失败时,整个构建被标记为失败 - 因为Gradle说构建失败了。

How can I convince Gradle to succeed the build and then Jenkins to mark the build as unstable? 我怎样才能说服Gradle成功构建,然后Jenkins将构建标记为不稳定? With ant this was no problem at all. 用蚂蚁这根本不是问题。

Use the ignoreFailures property in the test task. 在测试任务中使用ignoreFailures属性。

apply plugin: 'java'
test {
     ignoreFailures = true
}

You can use external properties to solve this problem. 您可以使用外部属性来解决此问题。

if (!ext.has('ignoreTestFailures')) {
  ext.ignoreTestFailures = false
}

test {
  ignoreFailures = project.ext.ignoreTestFailures
}

In this setup by default failures will fail the build. 在此设置中,默认情况下,失败将导致构建失败。 But if you call Gradle like so: gradle -PignoreTestFailures=true test then the test failures will not fail the build. 但是如果你像这样调用Gradle: gradle -PignoreTestFailures=true test那么测试失败不会使构建失败。 So you can configure Jenkins to ignore test failures, but to fail the build when a developer runs the tests manually. 因此,您可以将Jenkins配置为忽略测试失败,但在开发人员手动运行测试时使构建失败。

You can include this in your main build.gradle to be applied to all projects and all test tasks. 您可以将其包含在main build.gradle中,以应用于所有项目和所有测试任务。

allprojects{
    tasks.withType(Test) {
        ignoreFailures=true;
    }
}

Since just ignoring the failed test could not be used in my case i found out the following. 由于在我的情况下无法使用忽略失败的测试,因此我发现了以下内容。 If you are using a scripted jenkinsfile. 如果您使用的是脚本化的jenkins文件。 It is possible to wrap your test stage in a try-catch statement. 可以将测试阶段包装在try-catch语句中。

try {
 stage('test') {
  sh './gradlew test'
 } 
} catch (e) {
  echo "Test FAILED"
}

This will catch the build exception thrown by gradle but it marks the build as unstable. 这将捕获gradle抛出的构建异常,但它将构建标记为不稳定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM