Search code examples
androidtestinggradle

Stop gradle build when a unit test fails


I have found many posts on how to continue a gradle build when a unit test fails, however I cannot find any posts for how to stop for fail a build if a test fails. Is it possible to stop a debug build when a unit test fails?


Solution

  • Add this to build.gradle

    import org.gradle.api.tasks.testing.TestResult.ResultType
    
    test.afterTest { TestDescriptor td, TestResult tr ->
        if (tr.resultType == ResultType.FAILURE) {
            throw new Exception("$td failed")
        } 
    }