Gradle Android:如何在不使用--info的情况下显示测试结果

时间:2015-05-18 16:29:24

标签: android gradle

我正在使用Gradle Android插件运行Android测试,并希望查看单独的测试结果。

从这个问题的答案Gradle: How to Display Test Results in the Console in Real Time?看来,我可以使用--info(打印出很多我不在乎的其他详细垃圾)或者使用这个仅适用于Java插件(不是Android插件)


test {
    afterTest { desc, result -> 
        println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
    }
}

当我运行connectedCheck任务时,我可以使用其他选项/闭包只是打印单个测试结果而没有其他所有"详细程度"。

3 个答案:

答案 0 :(得分:10)

使用Gradle信息

这将打印Gradle

中的所有信息
gradle --info

或使用Android Gradle插件:

android.testOptions.unitTests.all {
    // Configure whether failing tests should fail the build
    ignoreFailures false

    testLogging {
        events "passed", "skipped", "failed", "standardOut", "standardError"
    }
}

或直接使用Gradle

allprojects {
    tasks.withType(Test) {
        testLogging {
            exceptionFormat "full"
            showCauses true
            showExceptions true
            showStackTraces true
            showStandardStreams true
            events = ["passed", "skipped", "failed", "standardOut", "standardError"]
        }
    }
}

请参阅:https://github.com/jaredsburrows/android-gradle-java-app-template/blob/master/gradle/compile.gradle#L20

<强>输出:

io.github.hidroh.materialistic.data.SessionManagerTest > testView PASSED

io.github.hidroh.materialistic.data.SessionManagerTest > testIsViewFalse PASSED

io.github.hidroh.materialistic.data.SessionManagerTest > testIsViewNull PASSED

io.github.hidroh.materialistic.data.SessionManagerTest > testIsViewTrue PASSED

io.github.hidroh.materialistic.data.SessionManagerTest > testViewNoId PASSED

来源: https://github.com/hidroh/materialistic/blob/master/robolectric.gradle

Gradle文档: https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/logging/TestLogEvent.html

答案 1 :(得分:2)

对于Android Studio(在com.android.tools.build:gradle:2.1.0和gradle版本gradle-2.10上测试)我添加了以下部分以完整格式打印异常以及记录每个执行的测试

apply plugin: 'com.android.application'

android { ... }

dependencies { ...}

tasks.withType(Test) {
    testLogging {
        exceptionFormat "full"
    }
    afterTest { desc, result ->
        println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
    }
}

答案 2 :(得分:0)

对我来说,其他答案中提到的所有选项仍然打印出许多详细信息。因此,尽管要求不应该使用信息,但我成功使用了以下内容。例如,如果您的测试在软件包com.example.android中,则可以使用:

gradle --info connectedDebugAndroidTest | grep "com\.example\.android\..* >"

将打印e。 g。:

 com.example.android.login.LoginActivityTest > enterCredentialsTest[Testing_emulator(AVD) - 9] SUCCESS

“成功”一词将变为绿色,这真棒。