I have a simple android app and I am testing it using my phone. So, there are two ways to do that :
Using eclipse
Using CLI
Problem:
When I run unit test case using Eclipse, it installs app on my phone at runtime and run junit test and after that if I use command on CLI:
adb -d shell am instrument -w com.abc.xyz.test/android.test.InstrumentationTestRunner, it runs fine.
However, if I directly run the above command in CLI without first running the unit test cases in Eclipse, I am getting error:
android.util.AndroidException: INSTRUMENTATION_FAILED: com.abc.xyz.test/android.test.InstrumentationTestRunner
at com.android.commands.am.Am.runInstrument(Am.java:586)
at com.android.commands.am.Am.run(Am.java:117)
at com.android.commands.am.Am.main(Am.java:80)
at com.android.internal.os.RuntimeInit.finishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:263)
at dalvik.system.NativeStart.main(Native Method)
INSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation target package: com.abc.xyz
INSTRUMENTATION_STATUS_CODE: -1
AndroidMAnifest.xml contains:
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.abc.xyz"
inside instrumentation tag
Could anyone please help me
I suppose that you will have solved it since january, but I work with command-line tools, found similar problem (error message is different) and solved it like I explain in following steps. I do the whole process from creating a dummy project with its empty test until a successful test run. I hope it can be useful for someone:
First step, create the project:
android create project
--name MyExample
--target "Google Inc.:Google APIs:17"
--path MyExample
--package com.example
--activity MyExampleActivity
Second step, create test project:
android create test-project
--path MyExampleTest
--name MyExampleTest
--main ../MyExample
Third step, access to your project directory, build it and check that the process ends successfully:
cd MyExample && ant debug
Fourth step, install it to the emulator:
adb -s emulator-5554 install -r bin/MyExample-debug.apk
Fifth step, access to your test project directory and try to run the tests:
cd ../MyExampleTest &&
adb shell am instrument -w com.example.tests/android.test.InstrumentationTestRunner
That yields:
INSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{com.example.tests/android.test.InstrumentationTestRunner}
INSTRUMENTATION_STATUS_CODE: -1
android.util.AndroidException: INSTRUMENTATION_FAILED: com.example.tests/android.test.InstrumentationTestRunner
at com.android.commands.am.Am.runInstrument(Am.java:676)
at com.android.commands.am.Am.run(Am.java:119)
at com.android.commands.am.Am.main(Am.java:82)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:235)
at dalvik.system.NativeStart.main(Native Method)
Sixth step, list your instrumentation clases and ensure that your current project is missing:
adb shell pm list instrumentation
That in my machine yields:
instrumentation:com.android.emulator.connectivity.test/android.test.InstrumentationTestRunner (target=com.android.emulator.connectivity.test)
instrumentation:com.android.emulator.gps.test/android.test.InstrumentationTestRunner (target=com.android.emulator.gps.test)
instrumentation:com.android.example.spinner.tests/android.test.InstrumentationTestRunner (target=com.android.example.spinner)
instrumentation:com.android.smoketest.tests/com.android.smoketest.SmokeTestRunner (target=com.android.smoketest)
instrumentation:com.android.smoketest.tests/android.test.InstrumentationTestRunner (target=com.android.smoketest)
instrumentation:com.example.android.apis/.app.LocalSampleInstrumentation (target=com.example.android.apis)
As you can see, the instrumentation for com.example.tests doesn't exist, so we will have to create it.
Seventh step, build you test project and check that it did successfully:
ant debug
Eigth step, install it to the emulator:
adb -s emulator-5554 install -r bin/MyExampleTest-debug.apk
Ninth step, list your instrumentation classes and look for the one of your project:
adb shell pm list instrumentation
That yields:
instrumentation:com.android.emulator.connectivity.test/android.test.InstrumentationTestRunner (target=com.android.emulator.connectivity.test)
instrumentation:com.android.emulator.gps.test/android.test.InstrumentationTestRunner (target=com.android.emulator.gps.test)
instrumentation:com.android.example.spinner.tests/android.test.InstrumentationTestRunner (target=com.android.example.spinner)
instrumentation:com.android.smoketest.tests/com.android.smoketest.SmokeTestRunner (target=com.android.smoketest)
instrumentation:com.android.smoketest.tests/android.test.InstrumentationTestRunner (target=com.android.smoketest)
instrumentation:com.example.tests/android.test.InstrumentationTestRunner (target=com.example)
instrumentation:com.example.android.apis/.app.LocalSampleInstrumentation (target=com.example.android.apis)
Look at the second to last, instrumentation:com.example.tests, it's which we wanted.
Tenth step, run your tests:
adb shell am instrument -w com.example.tests/android.test.InstrumentationTestRunner
That yields:
Test results for InstrumentationTestRunner=
Time: 0.0
OK (0 tests)
That is all. Now implement your tests, compile and install as usual. Additionally you can remove them like:
adb shell pm uninstall com.example.tests
But you will need to create instrumentation classes again to avoid the same error.
A more precise explanation/approach is the following:
Make sure you do
adb install -r bin/<>-debug.apk
from both from tests and the app directory.
After that ant test should work from the tests directory. (My guess is that there was a missing dependency to the app from test package - which was causing the failure).
Other than the above little hack, rest of the procedure I followed came from the android testing introduction on http://developer.android.com/.
Make sure you uninstall the previous app and reinstall or kick off the test only after uninstalling the previous app
Related
As per this blogpost from CommonsWare, AndroidManifest.xml file can have an android:testOnly attribute.
In my AndroidManifest.xml it is set as "false"
android:testOnly="false"
And I am generating the apk file using the “Build APK(s)” menu option as shown below image,
And when i am trying to install app from command line, adb install -r myapp.apk, I am still getting error,
Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
Android studio version is as below,
What else shall I do to make my app run?
You can also use command like this :
adb install -r -t myapp.apk
it works for me:
PS C:\Users\languoguang> adb -P 12345 install -r D:\GreeneTrans\HelloWorld-signed.apk
adb: failed to install D:\GreeneTrans\HelloWorld-signed.apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
PS C:\Users\languoguang> adb -P 12345 install -t D:\GreeneTrans\HelloWorld-signed.apk
Success
PS C:\Users\languoguang> adb -P 12345 install -r -t D:\GreeneTrans\HelloWorld-signed.apk
Success
PS C:\Users\languoguang>
Just use the following command:
adb install -t app/build/outputs/apk/debug/app-debug.apk
You do not need to use -r, -r means Reinstall an existing app, keeping its data.
Install an app You can use adb to install an APK on an emulator or
connected device with the install command:
adb install path_to_apk
You must use the -t option with the install command when you install a
test APK. For more information, see -t.
https://developer.android.com/studio/command-line/adb#move
-t: Allow test APKs to be installed. Gradle generates a test APK when you have only run or debugged your app or have used the Android Studio
Build > Build APK command. If the APK is built using a developer
preview SDK (if the targetSdkVersion is a letter instead of a number),
you must include the -t option with the install command if you are
installing a test APK.
https://developer.android.com/studio/command-line/adb#-t-option
Or you could use the same command as you click Run in Android Studio
adb push {project dir}/app/build/outputs/apk/debug/app-debug.apk /data/local/tmp/{appId}
adb shell pm install -t /data/local/tmp/{appId}
appId is defined in the app/build.gradle.
defaultConfig {
applicationId appId
Now the app is installed from locally on the device
Launch the first activity.
adb shell am start -n "{package name}/{package name}.splash.SplashActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
2019-11-13 13:43 Gradle sync started
13:44 Project setup started
13:44 Executing tasks: [:app:generateDebugSources,
:vplus_explore:generateDebugSources,
:vplus_uibase:generateDebugSources,
:vplus_widget:generateDebugSources,
:vplus_runtime:generateDebugSources,
:vplus_cards:generateDebugSources,
:vplus_launcher:generateDebugSources,
:vplus_settings:generateDebugSources,
:vplus_transactions:generateDebugSources,
:vplus_payment:generateDebugSources,
:vplus_common:generateDebugSources,
:vplus_account:generateDebugSources,
:vplus_commonres:generateDebugSources,
:vplus_bootstrap:generateDebugSources,
:vplus_logger:generateDebugSources]
13:44 Gradle sync finished in 27 s 126 ms
13:44 Gradle build finished in 4 s 666 ms
13:45 * daemon not running; starting now at tcp:5037
13:45 * daemon started successfully
13:45 Executing tasks: [:app:assembleDebug]
13:46 Gradle build finished in 33 s 640 ms
If you really want to be able to remove the test flag from the APK generated in Android Studio, you could try adding the following to your gradle.properties file:
android.injected.testOnly = false
Solution 1
Click drop-down menu with your configuration and choose Edit Configurations…
Select tab General and add -t to Install Flags field. Click Ok.
Now start the application again and it should work.
Solution 2
This means that, the shared application has some test packages, so
unless those has been removed and source is recompiled, you will not
be able to install this apk. But adb command provides a flag “-t”
using which you can install the apps with test packages.
$ adb install -r -t YourAndroidApp.apk
2566 KB/s (7266004 bytes in 2.764s)
Success
Solution 3
This error might occur if you moved the project from other computer where it was stored in different directory. To resolve the problem: Clean the project and build it again.
Solution 4
Go to “Settings” -> “Build, execution, deployment” and disable “instant run to hot swap code…”
Solution 5
Add this line to gradle.properties:
android.injected.testOnly = false
If you would like to manually install an APK or give it to someone for manual installation using the following adb command, then you should only build the APK from the Menu bar -> Build -> Build Bundle/APK.
adb install -r xyz.apk
Do not click on the play button as it builds the APK for test purposes only. Clicking on the play button overrides the APK in the default location which can then be installed manually by using the following command only.
adb install -r -t xyz.apk
I am trying to automate my build and make running our acceptance tests a part of that.
Unfortunately the gradle task connectedAndroidTest is reporting
Starting 0 tests on Nexus_5X_API_25(AVD) - 7.1.1
com.android.builder.testing.ConnectedDevice > No tests found.[Nexus_5X_API_25(AVD) - 7.1.1] FAILED .
No tests found. This usually means that your test classes are not in the form that your test runner expects (e.g. don't inherit from TestCase or lack #Test annotations).
This does not happen when I run the tests/suite from the IDE.
I found that Android Studio is executing following commands:
$ adb push /path/to/apk/app-debug.apk /data/local/tmp/com.MyApp
$ adb shell pm install -r "/data/local/tmp/com.MyApp"
$ adb push /path/to/apk/app-debug-androidTest.apk /data/local/tmp/com.MyApp.test
$ adb shell pm install -r "/data/local/tmp/com.MyApp.test"
$ adb shell am instrument -w -r -e debug false -e class com.myapp.TestSuite com.MyApp.test/com.myapp.testrunners.CreationInterceptingTestRunner
I know what those commands do but I do not know what connectedAndroidTest is doing under the hood and why it fails.
I found out that the issue has to do with multidex. At the end of the official page about enabling multidex it reads:
Notes:
* Don't use MultiDexTestRunner, which is deprecated; use AndroidJUnitRunner instead.
* Using multidex to create a test APK is not currently supported.
I do not understand what is mean with create test APK and why the adb command works.
I do not want to run the adb commands by hand as they have no reporting built in and I would need to do this all myself.
Is there any way to use connectedAndroidTest with multidex apps?
When I run instrumentation tests from within Android Studio, I see that the app remains on the device afterwards. But I can't figure out to do this from the command line with gradlew. My intention is to run tests that save screenshots in e.g /data/data/MyApp/cache/screenshots and download these with adb pull afterwards.
./gradlew connectedAndroidTest
causes the app to be uninstalled. I also tried
./gradlew connectedAndroidTest -x uninstallAndroidTest
but that didn't make any difference. What's causing the uninstallation, and how can I avoid it?
I solved this by letting gradle only build the apk, and then handling the install/test/uninstall work with adb. Here's an approximation of my script.
PKGNAME=com.corp.app
./gradlew assembleAndroidTest
adb install -r app/build/outputs/apk/app-debug.apk
adb install -r app/build/outputs/apk/app-debug-androidTest-unaligned.apk
adb shell am instrument -w ${PKGNAME}.test/android.support.test.runner.AndroidJUnitRunner
[ -d screenshots ] || mkdir screenshots
adb pull /data/data/${PKGNAME}/cache/screenshots screenshots
# Now we can uninstall.
adb uninstall ${PKGNAME}.test
adb uninstall ${PKGNAME}
I didn't find the way out of this problem. Looks like there is no way to run instrumentation test without uninstalling. You can use gradle commands to build and install your app and testApp. It is a better way to use these commamds because apps will be installed on all connected devices.
gradlew installVersionDebug
gradlew installVersionDebugAndroidTest
adb shell am instrument -w -r -e debug false -e class com.example.android.EspressoUITest {PKGNAME}.test/android.support.test.runner.AndroidJUnitRunner
But there is still a problem with reports. You can use custom testRunner to generate a JUnit style XML report. Such report could be converted to HTML format with common approach.
I'm exploring the Android build system and want to move everything to Jenkins server. I've managed to build the project and run instrumentation tests (white box testing) on Jenkins. And now stuck at the integration testing..
I followed the tutorial to create a Android Test Project on Eclipse. And the tests run successfully. But I need to be able to run it from command line so that I can trigger the tests on Jenkins.
If I run the project on Eclipse first, and later I will be able to use the command:
adb shell am instrument -w com.example.uitests/Android.test.InstrumentationTestRunner
However, if just want to start everything from command line, I don't know how to build the project...It looks like there's no build file generated by Eclipse..
Do I need to use ant or gradle? if so, what's the right way of doing so?
Edit:
When calling to list all instrumentation info
adb shell pm list instrumentation
I'm able to find my tests package:
instrumentation:com.example.uitests/android.test.InstrumentationTestRunner
(target=com.exmaple.android)
But when running the first command, I get
INSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for:
ComponentInfo{com.example.uitests/Android.test.InstrumentationTestRunnerpm}
INSTRUMENTATION_STATUS_CODE: -1 android.util.AndroidException:
INSTRUMENTATION_FAILED:
com.android.uitests/Android.test.InstrumentationTestRunnerpm
at com.android.commands.am.Am.runInstrument(Am.java:802) at
com.android.commands.am.Am.onRun(Am.java:242) at
com.android.internal.os.BaseCommand.run(BaseCommand.java:47) at
com.android.commands.am.Am.main(Am.java:75) at
com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:235) at
dalvik.system.NativeStart.main(Native Method)
The solution I have now is to add ant build.xml to this test project, follow the tutorial
android update project --name --target
--path
After updating the project, I'm able to build and install the apk to emulators
ant clean debug install
I would like to run monkey on Jenkins but after use configuration:
https://wiki.jenkins-ci.org/pages/viewpage.action?pageId=57181910
I see in monkey.txt file:
No activities found to run, monkey aborted.
$ E:\server\AndroidSDK/platform-tools/adb.exe -s localhost:46881
shell monkey -v -v -p package.name -s 0 --throttle 0 50
$E:\server\AndroidSDK/platform-tools/adb.exe disconnect
localhost:46881 [android] Stopping Android emulator [android]
Archiving emulator log
I run this command on my PC, but I cannot run it on server.
If I run the command: adb shell monkey -v -v 50 -p package.name -s --throttle 0 on cmd on server it will work.
How can I configure Jenkins to run monkey properly?
I'm using the newest plugins for Jenkins.
Are you really using "package.name" in the monkey command line rather than your actual package name?
Did you ensure the APK was installed on the emulator before running monkey?
Otherwise, is there any output in the logcat.txt which indicates what's going wrong?
Also, I'm not sure why you linked to a really old version of the Android Emulator Plugin wiki page, but since then there's a Jenkins build step that will run monkey for you, without you having to manually write it into a batch script step.
Like Thomas pointed out in Christopher's comment:
You also have to think about installing the apk to the smartphone.
This can be done by using the "install android package" build step before "Run android monkey tester".
After that you can add another build step "uninstall android package"