android espresso: running test from command line - android

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.

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").

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.

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

I'm adding also how to run it from Gradle here.
./gradlew connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.MyClassTest#myFunctionTest

Related

Test app in android phone using command line?

Android Studio is running quite slow in my laptop so Im planning use Android Studio just for building the app structure and use Sublime Text 3 for coding, but I want to test my app in my phone connected via USB using a command within the terminal. ADB maybe? or something else?
you can push the apk and install using adb install.
Of course, you still need to compile the new APK each time.
To run your tests from the command line use am instrument. For example to run all tests in your package:
$ adb shell am instrument -w com.example.foo/android.test.InstrumentationTestRunner
assuming your application package name is com.example.foo and is using InstrumentationTestRunner.
To verify your test package is installed, run
$ adb shell pm list instrumentation
EDIT
Or, if you just want to run the app once installed
$ adb shell am start -n com.example.foo/.MainActivity

Stopping Robotium test execution

I have a Jenkins job that starts a Robotium test from command line:
adb shell am instrument -w com.foo.tests/android.test.InstrumentationTestRunner
Sometimes the test run gets stuck. When I abort the Jenkins job, it does not stop the Robotium test run. I may have to cancel the test execution manually from the device before running another Jenkins job.
How do I stop the test execution? I cannot just uninstall the application under test with adb uninstall since it has active device admin.
You can cancel the previous Robotium test by starting another test one with a made-up test name:
adb shell am instrument -e class com.foo.tests#dummyTestName com.foo.tests/android.test.InstrumentationTestRunner
What about this:
adb shell am force-stop <PACKAGE>
You can clear the app data,like this
adb shell pm clear com.foo.tests

Run Android Instrumentation Tests Headless?

I'm not even sure if this makes sense but is it possible to run instrumentation tests in a headless mode? Currently I run my test suite from the command line like so:
ant debug install test
Or if I want to focus on single tests like so:
adb shell am instrument -w -e class com.my.package.testClass#testCase com.my.package.tests/android.test.InstrumentationTestRunner
Is there a flag I can pass to ant or adb (or both) to run the tests without a UI? I'm not using an emulator. I'm running the tests on my device.
It's not a question of running the tests headless, but rather running the emulator headless. Use the -no-window switch when starting the emulator from the command line.

Running AOSP tests

I am trying to run some AOSP tests, but I can't run any test except for the cts. I mention that I am trying to do this with an emulator. This is the output from my terminal:
manuel#banstyle:~/aosp/development/testrunner$ ./runtest.py -v core
Building tests...
about to run adb shell cat /data/local.prop
about to run adb shell ls -l /data/local.prop
about to run adb root
ONE_SHOT_MAKEFILE="frameworks/base/tests/CoreTests/Android.mk" make -j16 -C "/home/manuel/aosp" all_modules
about to run adb shell pm list instrumentation | grep android.core/android.test.InstrumentationTestRunner
Error: adb shell pm list instrumentation | grep android.core/android.test.InstrumentationTestRunner returned 1 error code
Could not find instrumentation android.core/android.test.InstrumentationTestRunner on device. Try forcing a rebuild by updating a source file, and re-executing runtest.
Exiting due to AbortError...
manuel#banstyle:~/aosp/development/testrunner$ ./runtest.py -v calculator
Building tests...
about to run adb root
ONE_SHOT_MAKEFILE="packages/apps/Calculator/Android.mk" make -j16 -C "/home/manuel/aosp" all_modules
adb push /home/manuel/aosp/out/target/product/generic/data/app/CalculatorTests.odex data/app/CalculatorTests.odex
about to run adb push /home/manuel/aosp/out/target/product/generic/data/app/CalculatorTests.odex data/app/CalculatorTests.odex
adb install -r /home/manuel/aosp/out/target/product/generic/data/app/CalculatorTests.apk
about to run adb install -r /home/manuel/aosp/out/target/product/generic/data/app/CalculatorTests.apk
32 KB/s (2699 bytes in 0.080s)
pkg: /data/local/tmp/CalculatorTests.apk
Failure [INSTALL_FAILED_DEXOPT]
about to run adb shell pm list instrumentation | grep com.android.calculator2.tests/android.test.InstrumentationTestRunner
Error: adb shell pm list instrumentation | grep com.android.calculator2.tests/android.test.InstrumentationTestRunner returned 1 error code
Could not find instrumentation com.android.calculator2.tests/android.test.InstrumentationTestRunner on device. Try forcing a rebuild by updating a source file, and re-executing runtest.
Exiting due to AbortError...
manuel#banstyle:~/aosp/development/testrunner$
Can anyone help?
I also have the same issue when I try 4.2.2. However, I don't have the problem when I work on 4.3. After googling, I realize the problem is caused by apk and odex. In 4.2.2, tests are compiled into apk and odex, which means, the apk doesn't contain the classes.dex. The odex file contains all classes. This actually cause the failure when you only "adb install apk", because pm cannot find the classes.dex.
The way to solve this is to add LOCAL_DEX_PREOPT := false in the Android.mk. This will put the dex back to the apk. I have tried this in 4.2.2 full-eng.
This thread also gives some info. https://groups.google.com/forum/#!topic/android-building/OrBJsS2J3sw

force android tests to run on different emulators from command line batch file

How can I run my android junit/robotium tests from the command line on every single emulator? I want to make sure my tests run on many android OS versions and many screen resolutions.
I'd like to write a batch file that runs from the windows command line to run my test suite over and over again on each emulator I have installed.
To run from the command line I can do this:
adb shell am instrument -w
com.myapp.client.test/android.test.InstrumentationTestRunner
but that just runs it on the default emulator. How can I force that command to run on all of the emulators I have setup?
Ideally, the batch file would looking something like:
launch emulator1
run tests
close emulator1
launch emulator2
run tests
close emulator2
...
I don't know how to do the launch and close part.
Thanks
EDIT: Found solutions. Below is my batch file
set PORTRAIT=medium
set LANDSCAPE=large
:: launch emulator
emulator -avd android2.2
:: wait for emulator to load
adb wait-for-device
:: install apps?
:: run tests in portrait
adb shell am instrument -w -e size %PORTRAIT% com.myapp.client.test/android.test.InstrumentationTestRunner
:: run tests in landscape
adb shell am instrument -w -e size %LANDSCAPE% com.myapp.client.test/android.test.InstrumentationTestRunner
:: pull screenshots
adb pull /sdcard/ c:\
:: close/kill emulator (android bug here, so must use windows taskkill)
taskkill /IM emulator-arm.exe
I would really recommend you looking to use something like Jenkins to handle this for you. You can use the android emulator plugin to build up a whole matrix of API versions/Screen size to have your tests run against.

Categories

Resources