3

I am interested in how to run Espresso tests from command line (gradle task) individually (run group/suite tests then close app and then run another group/suite of tests).

Found that it is feasible to implement JUnit Test Suites but do not really understand how does it looks like under the hood in a context of instrumentation tests. Does it starts separate processes per Test suite? There is sample application on Github but how to execute it from terminal?

Another interesting discovery is Sharding tests. However, it one sentence documentation.

May be somebody can share with any experience of running Espresso tests individually.

Robertas Setkus
  • 3,075
  • 6
  • 31
  • 54

2 Answers2

2

Most of this is documented as part of AndroidJUnitRunner: https://developer.android.com/reference/android/support/test/runner/AndroidJUnitRunner.html

The key piece that is missing is how to pass those parameters via Gradle. You can do that by specifying the options at the commandline as such:

./gradlew connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=*The full name of your test suite goes here*
jdonmoyer
  • 1,245
  • 1
  • 18
  • 28
  • Tried this command but still without any luck. Cannot recall what kind of error it throws. Anyway, I will give up on executing unit test suites cause test shards is what I need. Thanks anyway. – Robertas Setkus Jul 19 '17 at 17:04
1

I would recommend using the Spoon library to run your tests individually or in parallel on multiple devices. You can either use the jar file or use the Spoon gradle plugin mentioned on the same page. Both of them have adequate documentation to help you set it up.

You can also use Spoon to run an individual test and the command would look something like this:

./gradlew yourSpoonTaskName -PspoonClassName=com.yourPackageName.blah.ClassName
    -PspoonMethodName=methodName

In order to know what yourSpoonTaskName is run ./gradlew tasks.

Also, in your build.gradle file add the following spoon configuration:

spoon {
    // for debug output
    debug = true

    // To grant permissions to Android M >= devices
    grantAllPermissions = true

    // for sharding
    /*
    this will execute tests in parallel on multiple devices.
    */
    shard = true

    // Add this to run a specific test class & method 
    if (project.hasProperty('spoonClassName')) {
        className = project.spoonClassName
    }

    if (project.hasProperty('spoonMethodName')) {
        methodName = project.spoonMethodName
    }    
}

If you are not interested in Spoon and just want a simple solution, then use the following command to run an individual test:

am instrument -w -r -e class com.packageName.blah.TestName#methodName com.packageName.blah.YourIntrumentationRunnerName

You can easily determine these values if you right click the test name in AndroidStudio and run it. In the console, you will see the entire command being printed when the test is bring run.

mark w.
  • 1,047
  • 9
  • 16
  • Already tried Spoon. However, surprisingly it doesn't solve my problem. The problem which I am dealing with is that espresso tests crashes due to famous `OutMemoryError`. Memory is leaked over one 3rd party library which is massive dependency and it cannot be replaced. I thought that running test over multiple emulators will solve my problem temporarily while I will find proper solution. – Robertas Setkus Jul 19 '17 at 17:27
  • It would have been better if you had mentioned your OutMemoryError problem earlier as your initial statement mentioned nothing about this. All you wanted to know was how to do test sharding which I told you about. I would be more than happy to answer your actual problem if you go on to explain in more detail. Some logs would be helpful, too. – mark w. Jul 19 '17 at 20:28