9

My project is normal Android project created on Android Studio.

project structure

I googled a lot to run a single test(tests in SimpleTest, 1 test in this case), everyone say that I should do like

./gradlew -Dtest.single=SimpleTest test

in my root folder.

or I did in my inner project folder

../gradlew -Dtest.single=SimpleTest test

I tried so many ways like this but it always run all tests I have.(11 tests in all Classes)

Is there a problem on my build.gradle or is there a something I missing?

Here is my build.gradle file.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'

        classpath 'com.github.jcandksolutions.gradle:android-unit-test:1.0.+'
    }
}

apply plugin: 'android'

    repositories {
        mavenCentral()
    }

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.1"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 18
        // I changed this for this question
        packageName "com.XXX.XXXX"
    }

    sourceSets {
        instrumentTest.setRoot('src/test')
    }
}

apply plugin: 'android-unit-test'

dependencies {
    repositories {
        mavenCentral()
    }
    compile 'com.android.support:support-v4:18.0.+'
    compile 'com.android.support:appcompat-v7:+'

    compile 'com.google.code.gson:gson:2.2.4'
    compile group:'com.squareup.picasso', name:'picasso', version:'2.1.1'
    compile group:'com.squareup.okhttp', name:'okhttp', version:'1.2.1'
    compile group:'com.squareup', name:'otto', version:'1.3.4'

    compile group:'com.jakewharton', name:'butterknife', version:'3.0.0'

    compile group:'com.github.kevinsawicki', name:'http-request', version:'5.4.1'

    compile fileTree(dir: 'libs', include: '*.jar')

    testCompile 'junit:junit:4.10'
    testCompile 'org.robolectric:robolectric:2.2'
    testCompile 'com.squareup:fest-android:1.0.+'

    instrumentTestCompile 'junit:junit:4.10'
    instrumentTestCompile 'org.robolectric:robolectric:2.2'
    instrumentTestCompile 'com.squareup:fest-android:1.0.+'
}

tasks.withType(Compile) {
    options.encoding = "UTF-8"
}

tasks.withType(Test) {
    testLogging {
        events 'started', 'passed'
    }
}
Wooseong Kim
  • 1,871
  • 2
  • 21
  • 19
  • 1
    `-Dtest.single` is the right way to run a single test class or method when using the `java` plugin. Things might be different for the `android-unit-test` plugin. – Peter Niederwieser Nov 07 '13 at 01:57
  • @PeterNiederwieser Thank you for comment. Does it mean that I should find a specific solution for android-unit-test? – Wooseong Kim Nov 07 '13 at 08:24
  • @PeterNiederwieser you gave me a clue :) I figured out `android-unit-test` plugin still doesn't support `-Dtest.single` option by asking this to developer of this plugin at `Github`. He made a new patch for this and uploaded new version to Maven, So finally now I can run a single test class :) If you answer my question about this, I'll take that :) – Wooseong Kim Nov 08 '13 at 05:12
  • 1
    This worked for me: http://stackoverflow.com/questions/19565857/running-a-specific-unit-test-with-gradle/35585778#35585778 – jlhonora Feb 23 '16 at 18:54

4 Answers4

9

The notation

-Dtest.single=SimpleTest

means that in the task named 'test' run only SimpleTest. If your test task name differs you have to change the system property. E.g. your test task is named 'instrumentationTest' the property must be

-DinstrumentationTest.single=SimpleTest

cheers, René

Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
  • Thank you for comment :) I'm a newbie on gradle or Android Studio, so I just use the default test. instrumentTest isn't my custom task. Actually, If I run like `./gradlew instrumentTest`, It says **there is no task like this**. That's why I just run **test** for SimpleTest. – Wooseong Kim Nov 07 '13 at 08:35
  • And I tested running `../gradlew -DinstrumentTest.single=SimpleTest test`, it runs all tests I have. – Wooseong Kim Nov 07 '13 at 08:39
  • so your test task is simply named "test"? Can you check if putting the `-Dtest.single=...` part **after** the task makes a difference: `../gradlew test -Dtest.single=SimpleTest` ? It shouldn't, but you'll never know – Rene Groeschke Nov 07 '13 at 08:53
  • Yeah, I tried `../gradlew test -Dtest.single=SimpleTest` and `../gradlew test -DinstrumentTest=SimpleTest` but still runs all tests. – Wooseong Kim Nov 07 '13 at 11:14
4

this is what worked for me:

./gradlew testDebug --tests *TaskCommandSpec

Use testDebug or testRelease instead of just test. If you have build variants then use e.g testProRelease

docs: https://docs.gradle.org/current/userguide/userguide_single.html

zack
  • 3,198
  • 2
  • 35
  • 51
2

@PeterNiederwieser gave me a clue. I got a new Android Studio plugin for Robolectric from https://github.com/JCAndKSolutions/android-unit-test

Anyone who want the same solution like me can solve the problem using this project.

Also you can use it in build.gradle like

buildscript {
repositories {
    mavenCentral()
    mavenLocal()

}

dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
        classpath 'com.github.jcandksolutions.gradle:android-unit-test:1.0.1'
    }
}

and now ../gradlew clean check -Dtest.single=SomeTest works well.

ayushgp
  • 4,891
  • 8
  • 40
  • 75
Wooseong Kim
  • 1,871
  • 2
  • 21
  • 19
0

Run every classes in the admob package:

./gradlew clean test --tests "admob.*"

Run single test class:

./gradlew clean test --tests SimpleTest

Run single test method in SimpleTest class:

./gradlew clean test --tests SimpleTest.simpleMethod
Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49