Gradle: How to run instrumentation test for class - android

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

Related

CTS execution using junit categories

CTS execution:
How can we trigger the test suite of Junit by using annotations and categories through command line?
You can run CTS individual test methods or the whole class using android instrumentation command.
For Example: Here in this example CTS test class with a specific method is mentioned to run, you can remove the test method from the below command to run all test methods within the class.
adb shell am instrument -w -e class android.animation.AnimatorSetEventsTest#testCancel
\com.android.frameworks.coretests android.support.test.runner.AndroidJUnitRunner
For more details on running tests via instrumentation follow this link -
https://source.android.com/compatibility/tests/development/instrumentation

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?

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.

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