如何在 Android 单元测试中打开控制台输出? [英] How to turn on console output in Android Unit tests?

查看:81
本文介绍了如何在 Android 单元测试中打开控制台输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以在控制台输出中打开测试日志吗?

Is there any way to turn on the test logging in the console output?

我知道我们可以查看 HTML 文件中生成的测试结果并检查那里的标准输出,但我觉得这有点不方便.

I know that we can look at the test results generated in a HTML file and check standard output there, but I find it a little bit inconvinient.

我知道有一种方法可以使用标准的 java 插件来做到这一点:

I know that there is a way to do this with standard java plugin:

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

但是在 Android 项目中使用它会导致错误:

But using it in an Android project causes an error:

Could not find method test()

应用java插件当然是不可接受的,因为它与Android插件不兼容.

Applying java plugin is unacceptable, of course, as it's not compatible with Android plugins.

推荐答案

android {

...

  testOptions {
        unitTests.all {
        // All the usual Gradle options.
            testLogging {
                events "passed", "skipped", "failed", "standardOut", "standardError"
                outputs.upToDateWhen {false}
                showStandardStreams = true
            }
        }
    } 
}

就我而言,我遵循了这个文档和添加了 testLogging 选项,如上.这应该打印出在 src/test 文件夹下编写的单元测试的日志,而不是 src/androidTest 一个.在回答这个问题时,我使用的是 Android Studio 2.0 预览版和 gradle 2.8.命令是 ./gradlew test./gradlew test --continue 在 iTerm 2 中运行.

In my case, I followed this document and added the testLogging option as above. This should printout the log for the unit tests written under src/test folder but not the src/androidTest one. At the moment of this answer, I was using Android Studio 2.0 preview and gradle 2.8. The commands were ./gradlew test and ./gradlew test --continuein which ran in iTerm 2.

这篇关于如何在 Android 单元测试中打开控制台输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆