Running android unit tests from the command line?

僤鯓⒐⒋嵵緔 提交于 2021-02-05 20:27:00

问题


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?


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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)




回答5:


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.




回答6:


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




回答7:


For gradle user you can use the following tasks:

$ gradle :project:installDebug :project:installDebugAndroidTest



回答8:


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




回答9:


[Android test types]

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

JUnit test

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

Instrument test

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


来源:https://stackoverflow.com/questions/3299006/running-android-unit-tests-from-the-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!