【问题标题】:How to setup the robolectric in Android Studio如何在 Android Studio 中设置 robolectric
【发布时间】:2014-03-10 11:25:10
【问题描述】:

我正在使用单元框架 robolectric 测试我的 android 应用程序。我已经安装了 Android Studio (.4.6)

所有博客都说“为了能够使用 Gradle 运行 Android 单元测试,我们需要将 Gradle Android Test plug-in 添加到构建脚本中。”

但现在已弃用,那么我如何在不使用它的情况下设置它,或者我必须使用它。

【问题讨论】:

    标签: android android-studio robolectric android-gradle-plugin


    【解决方案1】:

    我正在使用com.github.jcandksolutions.gradle:android-unit-test:+

    所以在你的根 build.gradle(buildscript 部分):

    repositories {
        mavenLocal()
        mavenCentral()
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.8.+'
        classpath 'com.github.jcandksolutions.gradle:android-unit-test:+'
    }
    

    在您应用的 build.gradle 中

    apply plugin: 'android'
    
    android {
    
        [...]
    
        sourceSets {
            // this sets the root test folder to src/test overriding the default src/instrumentTest
            instrumentTest.setRoot('src/test')
        }
    }
    
    apply plugin: 'android-unit-test'
    
    dependencies {
        // example dependencies
        instrumentTestCompile 'junit:junit:4.+'
        instrumentTestCompile 'org.robolectric:robolectric:2.3-SNAPSHOT'
        testCompile 'junit:junit:4.+'
        testCompile 'org.robolectric:robolectric:2.3-SNAPSHOT'
    }
    

    请注意,您必须两次声明依赖项(一次用于 instrumentTestCompile 范围,另一次用于 testCompile 范围(用于 android-unit-test 插件))。至少对于这个版本的 Android Studio 和插件来说,这是必要的。

    然后您可以从终端使用 gradlew test 运行测试(在 Android Studio 中或独立)。

    旁注 1:我在 Windows 上集成 Android Studio 终端时遇到了一些问题。它不能很好地处理有限的可用水平空间,从而截断了输出。结果我开始使用ConEmu,避免使用Android Studio中的嵌入式终端和标准的cmd.exe。

    【讨论】:

    • 是的,我现在尝试了您的解决方案,在尝试您的解决方案后,我收到了这个新错误,stackoverflow.com/questions/22343013/…
    • 如果我的帖子解决了您的问题,您能否将我的帖子标记为答案?
    • 我收到一个错误:“未找到 Gradle DSL 方法:'instrumentTestCompile()”
    • 您使用的是哪个版本的 Android Studio?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多