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
Related
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?
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
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
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.
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.