简体   繁体   中英

How to setup the robolectric in Android Studio

I am doing work to test my android application using unit framework robolectric . I have installed the 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."

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:+

So in your root build.gradle (buildscript section):

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

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)). This is necessary at least for this version of Android Studio and the plugin.

Then you can run tests with gradlew test from terminal (either in Android Studio or standalone).

Side note 1: I had some problems with Android Studio terminal integration on Windows. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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