簡體   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