Gradle – How to continue build if test is failed

By default, the Gradle build process will be stopped and failed if any unit test is failed.


$ gradle build

:clean
:compileJava
:processResources
:classes
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test

com.mkyong.example.TestExample > test_example FAILED
    java.lang.Exception at TestExample.java:9
//...

3 tests completed, 1 failed
:test FAILED

//...

BUILD FAILED  // <-------------- see status

In this article, we will show you a few ways to continue the build process even if the test process is failing.

1. Ignore Failed Test

Try ignoreFailures settings.

build.gradle

test {
	ignoreFailures = true
}

Now, the build will continue even the test process is failing.


$ gradle build

:clean
:compileJava
:processResources
:classes
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test

com.mkyong.example.TestExample > test_example FAILED
    java.lang.Exception at TestExample.java:9
//...

3 tests completed, 1 failed
:test FAILED

//...

:check //ignore test failed, continue the build
:build

BUILD SUCCESSFUL  // <-------------- see status

2. Exclude the Failed Test

Find out the failed unit test and exclude it:

build.gradle

test {
	exclude '**/ThisIsFailedTestExample.class'
	exclude '**/*FailedTestExample*'
}

Refer this Gradle exclude some tests example

3. Skipped the Test

Last one, skipped the entire test process.


$ gradle build -x test

References

  1. Gradle – How to skip unit test
  2. Gradle – How to exclude some tests
  3. Gradle Test documentation

7 comments on “Gradle – How to continue build if test is failed

  1. very good information. Thanks a lot, @mkyong.

    Reply
  2. I have one situation where I want to ignore failures on integrationTest. Is there any command line option available for gradle?
    34 tests completed, 2 failed

    > Task :integrationTest FAILED

    FAILURE: Build failed with an exception

    BUILD FAILED in 3m 41s
    26 actionable tasks: 26 executed

    Reply
  3. java.lang.NoSuchMethodError: org.slf4j.impl.StaticLoggerBinder.getSingleton()Lorg/slf4j/impl/StaticLoggerBinder;
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
    at io.github.bonigarcia.wdm.WebDriverManager.(WebDriverManager.java:93)
    at BaseProperties.BaseClass.initializeDriver(BaseClass.java:32)
    at stepDefinations.stepDefination.initialize_the_browser_with_chrome(stepDefination.java:19)
    at ?.Given Initialize the browser with Chrome(Branch.feature:5)

    When I use the WebDriverManager dependency in maven project then run the TestRunner class then above error is display and doesn’t invoke the browser

    Reply
  4. Hi Mykong,

    Where should I use ignoreFailures settings?

    Here is my build.gradle.

    plugins {
    // Apply the java-library plugin to add support for Java Library
    id ‘java-library’
    }

    dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api ‘org.apache.commons:commons-math3:3.6.1’

    implementation ‘com.google.guava:guava:23.2’ // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    compile group: ‘com.google.guava’, name: ‘guava’, version: ‘25.1-jre’

    compile group: ‘org.seleniumhq.selenium’, name: ‘selenium-java’, version: ‘3.14.0’
    compile group: ‘io.github.bonigarcia’, name: ‘webdrivermanager’, version: ‘2.2.5’

    compile group: ‘log4j’, name: ‘log4j’, version: ‘1.2.17’
    compile group: ‘org.slf4j’, name: ‘slf4j-api’, version: ‘1.7.25’
    compile group: ‘ch.qos.logback’, name: ‘logback-classic’, version: ‘1.2.3’

    compile group: ‘org.apache.commons’, name: ‘commons-collections4’, version: ‘4.1’
    compile group: ‘org.apache.commons’, name: ‘commons-lang3’, version: ‘3.3.1’
    compile group: ‘org.apache.poi’, name: ‘poi’, version: ‘3.17’
    compile group: ‘org.apache.poi’, name: ‘poi-ooxml’, version: ‘3.17’

    compile group: ‘org.apache.commons’, name: ‘commons-collections4’, version: ‘4.1’
    compile group: ‘org.testng’, name: ‘testng’, version: ‘6.14.3’

    compile group: ‘org.yaml’, name: ‘snakeyaml’, version: ‘1.11’
    }

    // In this section you declare where to find the dependencies of your project
    repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    mavenCentral()
    }

    task runTests(type: JavaExec, dependsOn: ‘classes’) {
    main = ‘org.testng.TestNG’
    classpath = files(“./src/test/resources”,
    project.sourceSets.main.compileClasspath,
    project.sourceSets.test.compileClasspath,
    project.sourceSets.main.runtimeClasspath,
    project.sourceSets.test.runtimeClasspath)
    args = [“-threadcount”, “1”, “-d”, “./build/”, “./testng.xml”]
    }

    Reply
    1. If i give ignoreFailures=true inside the task it shows error as below.

      FAILURE: Build failed with an exception.

      * Where:
      Build file ‘D:\Backup\EC_Workspace\KEM\kem_testframework\build.gradle’ line: 48

      * What went wrong:
      A problem occurred evaluating root project ‘testautomation’.
      > Could not set unknown property ‘ignoreFailures’ for task ‘:runTests’ of type org.gradle.api.tasks.JavaExec.

      * Try:
      Run with –stacktrace option to get the stack trace. Run with –info or –debug option to get more log output. Run with –scan to get full insights.

      * Get more help at https://help.gradle.org

      BUILD FAILED in 2s

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *