开发者

Running android unit tests from the command line?

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=ActivityManagerS开发者_如何学运维ervice
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?


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


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.


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.


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)


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.


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


For gradle user you can use the following tasks:

$ gradle :project:installDebug :project:installDebugAndroidTest


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


[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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜