简体   繁体   English

如何在Android Studio中设置机器人

[英]How to setup the robolectric in Android Studio

I am doing work to test my android application using unit framework robolectric . 我正在使用单元框架robolectric测试我的android应用程序。 I have installed the Android Studio (.4.6) 我已经安装了Android Studio(.4.6)

All blogs saying for this "In order to be able to run Android unit tests with Gradle, we need to add the Gradle Android Test plug-in to the build script." 所有博客都表示:“为了能够使用Gradle运行Android单元测试,我们需要将Gradle Android Test插件添加到构建脚本中。”

but that is deprecated now then how can I setup this without using this or I have to use this. 但是现在不推荐使用,那么我该如何设置而不使用它,或者我必须使用它。

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

So in your root build.gradle (buildscript section): 因此,在您的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:+'
}

In your app's build.gradle 在您应用的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'
}

Note that you have to declare the dependency twice (one for instrumentTestCompile scope and one for testCompile scope (for android-unit-test plugin)). 请注意,您必须声明两次依赖关系(一次用于InstrumentTestCompile范围,一次用于testCompile范围(对于android-unit-test插件))。 This is necessary at least for this version of Android Studio and the plugin. 至少对于此版本的Android Studio和插件而言,这是必需的。

Then you can run tests with gradlew test from terminal (either in Android Studio or standalone). 然后,您可以从终端(在Android Studio中或独立运行)使用gradlew test运行测试。

Side note 1: I had some problems with Android Studio terminal integration on Windows. 旁注1:Windows上的Android Studio终端集成存在一些问题。 It did not handle well the limited horizontal space available, truncating the output. 它不能很好地处理有限的可用水平空间,因此会截断输出。 As a result I started using ConEmu , avoiding the embedded terminal in Android Studio and the standard cmd.exe. 结果,我开始使用ConEmu ,避免使用Android Studio中的嵌入式终端和标准cmd.exe。

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

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