'./gradlew -Dtest.single = SimpleTest test' runs all the tests that I have

My project is a regular Android project created on Android Studio .

project structure

I searched a lot to run one test (tests in SimpleTest, 1 test in this case), everyone says what I have to do, for example

./gradlew -Dtest.single=SimpleTest test 

in my root folder.

or I did it in my internal project folder

 ../gradlew -Dtest.single=SimpleTest test 

I tried as many ways as this, but it always ran all the tests that I have. (11 tests in all classes)

Is there a problem in my build.gradle or is there something I can't see?

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' } } 
+8
android android-studio testing gradle robolectric
source share
3 answers

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

Anyone who wants the same solution as me can solve the problem using this project.

You can also use it in build.gradle as

 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.

+2
source share

Designation

 -Dtest.single=SimpleTest 

means that in a task named "test" only SimpleTest is executed. If your test task name is different, you need to change the system property. For example. your test task is called "InstrumentTest", the property should be

 -DinstrumentationTest.single=SimpleTest 

amuses Rene

+8
source share

this is what worked for me:

./gradlew testDebug --tests *TaskCommandSpec

Use testDebug or testRelease instead of test . If you have build options, use, for example, testProRelease

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

+2
source share

All Articles