Run Android Instrumentation Tests Headless? - android

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.

Related

android espresso: running test from command line

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

How not to run a particular test when executing connectedAndroidTest?

It takes long to execute some of our instrumented tests. So I'd like not to run them when I run all the other instrumented tests with gradle connectedAndroidTest.
Why don't I annotate those tests with #Ignore? Because I'd like to run them later using adb shell as described here.
Like this:
Running all tests except those in a particular class: adb shell am
instrument -w -e notClass com.android.foo.FooTest
com.android.foo/android.support.test.runner.AndroidJUnitRunner
If I marked those tests ignored and compiled them, it wouldn't be possible to execute them at all.
Is it possible to modify connectedAndroidTest or some other task to reach what I need?

Gradle: How to run instrumentation test for class

I'm running instrumentation test in Android Studio with Run Configuration defined as below (don't mind warning):
So this is invoking test suit for a specific class. How can I achieve this with command line, I guess using ./gradlew command ?
As stated in the AndroidTestingBlueprint you can use the android.testInstrumentationRunnerArguments.class property:
./gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.android.testing.blueprint.ui.espresso.EspressoTest
According to the docs:
When you run tests from the command-line with Android Debug Bridge
(adb), you get more options for choosing the tests to run than with
any other method. You can select individual test methods, filter tests
according to their annotation, or specify testing options. Since the
test run is controlled entirely from a command-line, you can customize
your testing with shell scripts in various ways.
To run instrumentation tests with adb for a particular class do:
adb shell am instrument -w -e class 'com.myapp.MyActivityTest' com.myapp.test/android.support.test.runner.AndroidJUnitRunner
Note that if you've defined a custom testInstrumentationRunner on your app/build.gradle file then you need to replace android.support.test.runner.AndroidJUnitRunner with your own, like this:
adb shell am instrument -w -e class 'com.myapp.MyActivityTest' com.myapp.test/com.myapp.MyCustomTestRunner
Tip: If you get an error because the command isn't right, know that you can simply get the right command by running the tests from within Android Studio. You'll see the command on the Run window output.
These 2 documentation pages contain execution options:
https://developer.android.com/reference/android/support/test/runner/AndroidJUnitRunner#typical-usage
https://developer.android.com/studio/test/command-line#AMSyntax

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.

Run android application with instrumentation turned on

I have created a customised version of android.app.Instrumentation and have modified my AndroidManifest.xml to use it.
However when I run the application from eclipse it does not seem to load my instrumentation object (am logging and settings global (I know horrible, just for now I swear!) variables that I later check)
I think I need to run it using adb and telling it to use instrumentation but I can't find the correct instructions for doing this (and I have read so much about instrumentation the last few days I am start to go nuts!)
Run
$ adb shell pm list instrumentation
to verify that your instrumentation is there. You will receive something like
instrumentation:my.pkg.text/my.instr (target=my.pkg)
then run
$ adb shell am instrument -w my.pkg.test/my.instr

Categories

Resources