android espresso: running test from command line

16,091

Solution 1

This worked for me (the change is in bold:

adb shell am instrument -w-e class full.path.and.TestClassName\ com.demo.app.test/android.support.test.runner.AndroidJUnitRunner

Based on: https://developer.android.com/studio/test/command-line.html#AMOptionsSyntax (look under options for "class").

Solution 2

If you're using gradle, then there is gradle task you can directly use to achieve it. It would be something like this:

./gradlew connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices.

To run on specific flavor:

./gradlew connectedMyAppFlavorDebugAndroidTest

It does everything for you, right from building the app, installing on the connected device, run the tests and finally uninstall the app.

If you're not sure about the exact gradle task you need to execute the tests, run the following to get the all available gradle tasks:

./gradlew tasks

You'll get the list of all the tasks with the short description.

Solution 3

To run via command line

Start device. I use Genymotion so I would do

gmtool admin start DeviceName

Install via command line

For ADB

for ADB is should be exactly what the console outputs from Android studio when you start .

$ adb push /Users/x/x-android/app/build/outputs/apk/x-debug.apk 
$ adb shell pm install -r "/data/local/tmp/com.x"

$ adb push /x/x/x-android/app/build/outputs/apk/x-debug-androidTest.apk /data/local/tmp/com.x.test
$ adb shell pm install -r "/data/local/tmp/com.x.test"

For Genymotion it is

gmtool device install ~/Desktop/x.apk 
gmtool device install ~/Desktop/x-androidTest.apk 

For Genymotion connect Genymotion to ADB

gmtool device adbconnect


Start your tests. This is for both ADB and Geny

adb shell am instrument -w -r   -e debug false -e class com.x.MyTest com.x.test/android.support.test.runner.AndroidJUnitRunner
Share:
16,091
San Kh
Author by

San Kh

Updated on June 27, 2022

Comments

  • San Kh
    San Kh almost 2 years

    I have a test.espresso package with all the test classes. I am trying to run a single test class from the command line, however it ends up running all the test classes.

    adb shell am instrument -w \ com.demo.app.test/android.support.test.runner.AndroidJUnitRunner

    How do I just run a single test class. I want to use bamboo(which is like jenkins) to run all the test classes individually in separate jobs.