17

I'm trying to run unit tests on the android platform in accordance with tutorial. Say, for example, I want to run tests for Email application. I open /apps/Email/tests/AndroidManifest.xml file, look for the <manifest> element, and look at the package attribute, which is com.android.email.tests, and in the <instrumentation> element I look at the android:name attribute, which is android.test.InstrumentationTestRunner. Now I open the console, and run

$ . build/envsetup.sh
$ lunch 1
$ adb shell am instrument -w com.android.email.tests/android.test.InstrumentationTestRunner

But that fails:

INSTRUMENTATION_STATUS: id=ActivityManagerService
android.util.AndroidException: INSTRUMENTATION_FAILED: com.android.email.tests/android.test.InstrumentationTestRunner
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{com.android.email.tests/android.test.InstrumentationTestRunner}

So.. What am I doing wrong?

George
  • 8,368
  • 12
  • 65
  • 106

9 Answers9

9

Please run python development/testrunner/runtest.py email

and then you will see it works :).

Basically you do not have com.android.email.tests package installed.

You can see what is available for instrumentation

pm list instrumentation

And you should see

instrumentation:com.android.email.tests/android.test.InstrumentationTestRunner (target=com.android.email)

And when doing

pm list packages

package:com.android.email

package:com.android.email.tests

marekdef
  • 481
  • 4
  • 10
  • 4
    Android doesn't use Python. Should be `adb shell pm list instrumentation` – Chloe Oct 07 '12 at 19:56
  • Yes these are commands that you do in adb shell. Python is not in android but used to be in framework for testing android http://androidxref.com/4.2_r1/xref/development/testrunner/runtest.py – marekdef Jan 18 '13 at 09:23
3

You may need to setup a test project with the android create test-project command first. Check this page on the Android Dev site: Testing In Other IDE's for more info. I've used this method to enable command line testing with ant.

Marc Bernstein
  • 11,423
  • 5
  • 34
  • 32
2

What I actually forgot to do was building and installing that test packages onto my device/emulator. Discovered that after doing:

$ adb shell
# cd data/packages
# ls

And no com.android.email.tests package there.

George
  • 8,368
  • 12
  • 65
  • 106
1

My issue was this tag:

 <instrumentation
    android:name="android.test.InstrumentationTestRunner"
    android:label="Tests for app.under.test.package"
    android:targetPackage="app.under.test.package" />

Firstly I had the android:name attribute wrong then the target package wrong (above is the correct solution)

Blundell
  • 75,855
  • 30
  • 208
  • 233
1

Test Package Not Installed on the Emulator

I had the exact same issue and realized that the test package hadn't been installed on the emulator, which is what @marekdef is saying.

Instead of using the command found in the generated test file, I used the following:

ant debug install test

*I had my test project in <project_home>/tests so the following command is what I ended up using from my project home directory:

(cd tests && ant debug install test;)

Hope that helps.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
0

For gradle user you can use the following tasks:

$ gradle :project:installDebug :project:installDebugAndroidTest
korosmatick
  • 579
  • 4
  • 8
0

I received the "Unable to find instrumentation info" error under this condition: I defined my test project with a src package name that was the same as that of the project-under-test. For example, the source for both projects was in package com.mycompany.android. This parallel-src-package setup worked fine in Eclipse, but on the emulator it very much appeared that the test apk was overwriting the app apk.

Fix: I changed the src packge of the test project to test.mycompany.android.

Note that, in either case, the Android manifest for the test project defines:

< manifest package="pkg-of-project-under-test" ...>

and

< instrumentation android:targetPackage="pkg-of-project-under-test" ...>

cdhabecker
  • 1,693
  • 1
  • 14
  • 23
  • I just found that eclipse-Run does not care whether the manifest element defines package="pkg-of-project-under-test" (which is the value that eclipse inserts when it creates a new Android test project) or package="pkg-of-test-project". Why did I notice this? Well, eclipse-Build seems to care about that value. When I added the first Activity to my test project and edited the test manifest to describe it, eclipse started complaining that it couldn't find R when compiling my test activity. Changing the package to pkg-of-test-project solved that problem. – cdhabecker Aug 24 '10 at 22:17
0

I have this run_all_test.sh script to run all unit and instrumented test:

#!/bin/bash
./gradlew --no-daemon clean test connectedCheck --stacktrace;
if [ $? -eq 0 ]
then
    echo "tests are successful"
else
    echo "tests FAILED"
    exit 1
fi

Explanation:

  • test -> execute all unit test

  • connectedCheck -> execute all instrumented test

You can start from here to customize it based on your needs following the documentation here: https://developer.android.com/studio/test/command-line

MatPag
  • 41,742
  • 14
  • 105
  • 114
0

[Android test types]

To run all tests from Command Line using gradlew[About]

JUnit test (UnitTest suffix)

./gradlew test
./gradlew :<moduleName>:test<variantName>UnitTest

Instrument test(AndroidTest suffix)

./gradlew connectedAndroidTest
./gradlew :<moduleName>:connected<variantName>AndroidTest

If you want just to build tests and don't run them use assemble

//Unit
./gradlew :<moduleName>:assemble<variantName>UnitTest

//functional
./gradlew :<moduleName>:assemble<variantName>AndroidTest
yoAlex5
  • 29,217
  • 8
  • 193
  • 205