简体   繁体   中英

Android Espresso Test Suites

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.

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*

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM