繁体   English   中英

如何在Android Studio中设置机器人

[英]How to setup the robolectric in Android Studio

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

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

但是现在不推荐使用,那么我该如何设置而不使用它,或者我必须使用它。

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

因此,在您的root 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和插件而言,这是必需的。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM