How not to run a particular test when executing connectedAndroidTest? - android

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?

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

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

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.

How to write a Git pre-commit hook that prevents committing of an Android project if the test project fails?

Given that I in my workspace I have an android project MyAndroidProject and my tests project MyAndroidProjectTests directories how could I write a pre-commit git hook that will run the tests in the MyAndroidProjectTests and refuse to commit any code changes if the tests fail?
When I run tests on the terminal they usually have output like this:
com.mydomain.tests.Models.MyProjectTests:.......
Test results for InstrumentationTestRunner=.......
Time: 0.05
OK (10 tests)
What I'm unsure about is how to what to use to try to determine if the tests passed or failed other than parsing the output of the last line (e.g. OK or FAILED and I'm not sure I like that method so much. I ideally I'd like a status to be returned from the command I use to run tests on the terminal:
adb shell am instrument -w com.mydomain.tests/android.test.InstrumentationTestRunner
I'd like to use the response in a shell script that I could place in the .git/hooks/ folder as a pre-commit hook.
I would appreciate any info or links to other resources and much thanks in advance.
git hooks are executed from your project/repo root - so you should be able to just use your command directly in a precommit hook:
#!/bin/bash
adb shell am instrument -w com.mydomain.tests/android.test.InstrumentationTestRunner
if adb returns a none-zero exist code on failure - the commit will be aborted.
If you need to get the exit code for another purpose there are other questions indicating exactly how to do that.

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