【问题标题】:Android Studio unit testing support vs robolectricAndroid Studio 单元测试支持 vs robolectric
【发布时间】:2015-02-20 10:49:24
【问题描述】:

迄今为止,我使用 JUnit 4.x 的 robolectric 单元测试来测试我的业务逻辑。在 Android Studio 1.1.0 的最后一个版本中,宣布了 native support 的单元测试 junit:4.+

我应该拒绝使用 robolectric 吗? robolectric 有一些我可能不知道的明显优势吗?

至于我使用 Android Studio 原生测试更方便更简单。在 robolectric 测试结果存储在 html 文件中,并且可以显示在浏览器中(这对我来说不方便)。原生 Android Studio 测试结果显示在 Run 输出窗口中,如果某些测试失败,我们可以通过点击输出窗口中的错误轻松打开这行代码。

【问题讨论】:

    标签: java android unit-testing android-studio robolectric


    【解决方案1】:

    没有理由不能同时使用两者。 Robolectric 的优势在于您可以在需要时针对实际的 Android 行为进行测试。普通的 JUnit 只会让您测试不与 Android 交互的代码部分。

    这将让您在 Android Studio 中查看 robolectric 测试的结果以及普通的 JUnit 4.x 测试

    如果您使用的是 gradle,robolectric example github repo 有一个示例说明如何操作。

    我目前正在迁移一些应用程序以使用这种方法

    这里是build.gradle的相关部分

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.1.0'
        }
    }
    dependencies {
        testCompile 'junit:junit:4.12'
        testCompile 'org.hamcrest:hamcrest-core:1.1'
        testCompile 'org.hamcrest:hamcrest-library:1.1'
        testCompile 'org.hamcrest:hamcrest-integration:1.1'
        testCompile('org.robolectric:robolectric:2.4') {
            exclude module: 'classworlds'
            exclude module: 'commons-logging'
            exclude module: 'httpclient'
            exclude module: 'maven-artifact'
            exclude module: 'maven-artifact-manager'
            exclude module: 'maven-error-diagnostics'
            exclude module: 'maven-model'
            exclude module: 'maven-project'
            exclude module: 'maven-settings'
            exclude module: 'plexus-container-default'
            exclude module: 'plexus-interpolation'
            exclude module: 'plexus-utils'
            exclude module: 'wagon-file'
            exclude module: 'wagon-http-lightweight'
            exclude module: 'wagon-provider-api'
        }
    }
    

    测试类不需要任何修改

    【讨论】:

    • com.github.jcandksolutions.gradle:android-unit-test 已弃用 (github.com/JCAndKSolutions/android-unit-test/blob/master/…)。有人可以提供一个非弃用的 Robolectric 解决方案,它适用于 androidTestCompile 和 testCompile 吗? Android 工具团队目前“待定”(又名:妈妈)tools.android.com/tech-docs/new-build-system/…
    • 谢谢!你知道如何启用 jacoco 输出以支持 Android 对单元测试的支持吗?当我“android { buildTypes { debug { testCoverageEnabled = true } } jacoco { version = '0.7.3.201502191951' } }”时,我没有得到 jacoco 输出。如果我“应用插件:'jacoco'”,testDebug 任务将失败,并输出“线程中的异常”“main”java.lang.ClassNotFoundException:org.jacoco.agent.rt.internal_932a715.PreMain”。仅供参考:我正在整理我自己的 SO 问题以提供更多细节。
    • 很遗憾我没有
    • 实际上,我确实在我的项目根目录中获得了 jacoco.exec 输出。我之前没有注意到这一点,因为我希望它在构建目录中。另一个问题是我不小心写了“android { ... buildTypes { ... jacoco { version = '0.7.3.201502191951' } } }”而不是“android { ... buildTypes { ... } jacoco { version = '0.7.3.201502191951' } }”。它似乎也不再需要,而且没有它似乎也能正常工作。
    • 如果不清楚,您的 build.gradle 文件中不需要“应用插件:'jacococ'”!您只需要指定“android { buildTypes { debug { testCoverageEnabled true } } }”
    猜你喜欢
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多