28

I have a simple android app and I am testing it using my phone. So, there are two ways to do that :

  1. Using eclipse
  2. 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

Chait
  • 1,052
  • 2
  • 18
  • 30
user1968471
  • 301
  • 1
  • 3
  • 5

3 Answers3

37

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.

Birei
  • 35,723
  • 2
  • 77
  • 82
  • Thank you, thank you, thank you Birei ! The install action solved my problem, as that was the reason why I was getting the "Unable to find instrumentation info" error – gsaslis Apr 18 '14 at 10:12
  • I checked with your way but its not working at all. And one thing more In sixth step i am able to see name of my project in instrumentation list but when i am trying to run tests so it gives error same like you described above. Please help me. – sam_k May 26 '14 at 11:27
  • 6
    This is my favorite step: "`adb shell pm list instrumentation`" ;) – Trinimon Nov 25 '14 at 13:08
  • you forgot to install test on the device – nutella_eater Jan 27 '21 at 13:42
2

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/.

ThomasThiebaud
  • 11,331
  • 6
  • 54
  • 77
vpathak
  • 1,133
  • 12
  • 12
1

Make sure you uninstall the previous app and reinstall or kick off the test only after uninstalling the previous app

Testing Singh
  • 1,347
  • 1
  • 15
  • 26