7

I have an Android/Gradle project. Whenever I want to run tests, I run:

./gradlew connectedInstrumentTest

which runs all my tests under the test folder of my project.

My test folder has several automation tests as well as non-automation tests. I'm mostly interested in running the fast non-automation tests without the slow automation tests.

Is there a way to run just a specific set of tests, such as from one specific class or anything similar? I'm basically asking about any kind of separation so that I can choose to run just a few tests when I want to.


Created a sample project here.

Edit local.properties to point at your Android SDK.

Next, start up an emulator or connect a phone to your computer. Then you can run tests using ./gradlew connectedInstrumentTest --info. This runs all tests.

What I am unable to figure out is how to only run tests in, say, one class and not all tests.

tir38
  • 9,810
  • 10
  • 64
  • 107
Mendhak
  • 8,194
  • 5
  • 47
  • 64
  • Does this answer your question? [Running a specific instrumentation unit test with Gradle](https://stackoverflow.com/questions/19565857/running-a-specific-instrumentation-unit-test-with-gradle) – tir38 Feb 06 '20 at 00:09

3 Answers3

12

Since Android Gradle Plugin 1.3.0

Starting from version 1.3.0 you can (finally!) specify the arguments the Android Gradle Plugin have to pass to the InstrumentationTestRunner.

For example, if you want to run only the tests annotated with @SmallTest and ignore the others:

android {
  //....
  defaultConfig {
  //....
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    testInstrumentationRunnerArgument "size", "small"
  }
}

Old workaround Prior to plugin 1.3.0 is not possible to do that but I've found a little workaound. Basically I've annotated with the @SmallTest annotation the fast tests and using a custom subclass of the InstrumentationTestRunner I'm able to run just them and not the whole suite.

You can found the example code in this gist.

rciovati
  • 27,603
  • 6
  • 82
  • 101
  • This helped me, I had to make sure my package name was `testPackageName "com.example.packagename.test"` and after that I was able to pass in `-PtestSize=small`. Now my Travis builds don't randomly fail as I can ignore the Robotium tests. Really appreciated. – Mendhak Apr 15 '14 at 10:08
  • 1
    This is helpful, thanks. I didn't realize that `@SmallTest` is a standard JUnit annotation. Here are the guidelines for `@SmallTest`, `@MediumTest` and `@LargeTest` (in my case I'm going to overload one of them to run a particular set of tests): https://plus.google.com/+AndroidDevelopers/posts/TPy1EeSaSg8 – Dan J Jul 16 '14 at 18:13
  • You can also use this feature to run everything **except** one annotation. `testInstrumentationRunnerArgument 'notAnnotation', 'android.test.suitebuilder.annotation.LargeTest'` – Kevin Brotcke Jul 09 '15 at 01:53
  • @Riccardo what if I want to run both small and medium test? – Arade Sep 15 '16 at 17:36
2

Also you can install test suit on the device and run it manually through adb. See android docs

gerbit
  • 902
  • 9
  • 17
1

Yes, please have a look here. It should also work with an android project. Unfortunately as far as I know there's no way to rung single method - You can limit to whole suit only.

Opal
  • 81,889
  • 28
  • 189
  • 210
  • There is no test task in Android Gradle projects as it goes through their [plugin](http://tools.android.com/tech-docs/new-build-system/user-guide). – Mendhak Apr 13 '14 at 18:51
  • OK. Thanks for clarification. Any sample project on GH to have a try? Beyond the fact that here's no `test` task I guess that there's still a possibility to configure a single test run. Will ask android devs for some details at work on tomorrow. – Opal Apr 13 '14 at 19:19
  • Cheers, modified the post to include a zip download. I'd understand if many Android devs are still on non-gradle systems though as it's somewhat new. – Mendhak Apr 13 '14 at 19:47
  • Thanks. Will have a look at it! – Opal Apr 13 '14 at 19:49