artxur
artxur

Reputation: 1940

How to use gradle-android-test-plugin

I want to get Robolectric working with Android Studio and am attempting to use [gradle-android-test-plugin][1] after reviewing answers to this [question][2], but am having no luck.

  1. I created a brand new Android project and named it MyApp.
  2. I followed all the instructions in the "Usage" section of the README.
  3. I didn't yet have any code to test in my new project, so I copied the classes (including the `RobolectricGradleTestRunner`) from the
gradle-android-test-plugin/example/src/test/java/com/example

and the

gradle-android-test-plugin/example/src/main/java/com/example 

directories into corresponding file paths in my new MyApp project.

I've never implemented unit testing for Android before and I don't know what to do next. I successfully ran ./gradlew install in the root directory of the the plugin, but doing the same in the root directory of my new project accomplishes nothing. It throws this complaint:

Task 'install' is ambiguous in root project 'MyApp'. Candidates are: 'installDebug', 'installTest'.

I'm not at all familiar with Gradle, so short of reading a textbook on Gradle itself, I'm not sure how to resolve this issue.

If I enter the MyApp directory containing AndroidManifest.xml and try the ../gradlew clean check to build and run the tests, I get this error:

Could not determine the dependencies of task ':MyApp:testDebug'.

Android Studio doesn't seem to be having any issues finding my dependencies. Can someone point me in the direction of running my tests, please? I imagine it's a simple terminal command or something that wasn't mentioned in the plugin's README. Cheers!

edit:

Here is my build.gradle. I'm aware it's not structurally identical to that of the example project included with the plugin, but that structure gave me identical errors.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
        classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.+'
    }
}

apply plugin: 'android'
apply plugin: 'android-test'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 18
    }
}

dependencies {
    compile 'com.android.support:support-v4:18.0.0'

    testCompile 'junit:junit:4.11'
    testCompile 'org.robolectric:robolectric:2.1.+'
    testCompile 'com.squareup:fest-android:1.0.+'
}

edit2: Besides the fact that I was issuing the wrong commands (as indicated in Ravindra's answer), I also failed to realize my version of Gradle was too new (as Jake's comment points out).

Upvotes: 8

Views: 7934

Answers (3)

Nikita Skvortsov
Nikita Skvortsov

Reputation: 4923

From the plugin's code, it seems like you should run ./gradlew test command for tests to be executed

Upvotes: 0

ravidsrk
ravidsrk

Reputation: 88

There is no install task but there are two tasks which are prefixed with install. You have to supply the full task name or at least enough for Gradle to figure out which one you are talking about (e.g., installD, iD, or the full thing installDebug will work)

https://github.com/square/gradle-android-test-plugin/issues/6#issuecomment-22881923

Upvotes: 2

robotoaster
robotoaster

Reputation: 3122

for list of available tasks run from main project folder

gradle tasks

for tests

gradle tests

Upvotes: 2

Related Questions