Search code examples
androidtestinggradlegradle-android-test-plugi

Don't fail the gradle build if a test is failing with the gradle-android-test-plugin


I set my project up to run with Robolectric and the the gradle-android-test-plugin. This all works fine and first tests are running and failing.

If a test fails this will also fail the gradle build. Is there a way to just let the build go on and write down the failing tests for evaluation in a later step?

The plan is to integrate the testing in a continuous integration server and there the build should only be unstable if a test fails.


Solution

  • The correct syntax with AndroidConnectedTests is as following:

    project.gradle.taskGraph.whenReady {
        connectedAndroidTest {
            ignoreFailures = true
        }
    }
    

    Or for newer Gradle-plugin (1.3.0 and later), try:

    project.gradle.taskGraph.whenReady {
        connectedDebugAndroidTest {
            ignoreFailures = true
        }
    }
    

    But if you have Flavors (in newer Gradle versions):

    android {
    
        // ...
    
        project.gradle.taskGraph.whenReady {
            android.productFlavors.all { flavor ->
                // Capitalize (as Gralde is case-sensitive).
                def flavorName = flavor.name.substring(0, 1).toUpperCase() + flavor.name.substring(1)
    
                // At last, configure.
                "connected${flavorName}DebugAndroidTest" {
                    ignoreFailures = true
                }
            }
        }
    }
    

    Now the test task is not failing the build anymore, which ensures the coverage report is created, and you can pick up the failed tests with your build server to mark the build as unstable etc.