简体   繁体   English

从命令行运行 android 单元测试?

[英]Running android unit tests from the command line?

I'm trying to run unit tests on the android platform in accordance with tutorial .我正在尝试按照教程在 android 平台上运行单元测试。 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 .我打开/apps/Email/tests/AndroidManifest.xml文件,查找<manifest>元素,并查看package属性,即com.android.email.tests ,并在<instrumentation>元素中查看android:name属性,即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?所以..我做错了什么?

Please run python development/testrunner/runtest.py email请运行 python development/testrunner/runtest.py email

and then you will see it works :).然后你会看到它有效:)。

Basically you do not have com.android.email.tests package installed.基本上你没有安装 com.android.email.tests 包。

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)仪器:com.android.email.tests/android.test.InstrumentationTestRunner (target=com.android.email)

And when doing当做

pm list packages

package:com.android.email包:com.android.email

package:com.android.email.tests包:com.android.email.tests

You may need to setup a test project with the android create test-project command first.您可能需要先使用android create test-project命令设置一个测试项目。 Check this page on the Android Dev site: Testing In Other IDE's for more info.查看 Android 开发站点上的此页面: 在其他 IDE 中测试以获取更多信息。 I've used this method to enable command line testing with ant.我已经使用这种方法来启用 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.那里没有com.android.email.tests包。

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)首先我有android:name属性错误然后目标包错误(以上是正确的解决方案)

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.我遇到了完全相同的问题,并意识到模拟器上尚未安装测试包,这就是@marekdef 所说的。

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: *我在<project_home>/tests有我的测试项目,所以以下命令是我最终从我的项目主目录使用的命令:

(cd tests && ant debug install test;)

Hope that helps.希望有帮助。

对于 gradle 用户,您可以使用以下任务:

$ gradle :project:installDebug :project:installDebugAndroidTest

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.在这种情况下,我收到“无法找到检测信息”错误:我使用与被测项目相同的 src 包名称定义了我的测试项目。 For example, the source for both projects was in package com.mycompany.android.例如,两个项目的源代码都在包 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.这个 parallel-src-package 设置在 Eclipse 中运行良好,但在模拟器上很明显测试 apk 覆盖了应用 apk。

Fix: I changed the src packge of the test project to test.mycompany.android.修复:我将测试项目的src包改为test.mycompany.android。

Note that, in either case, the Android manifest for the test project defines:请注意,无论哪种情况,测试项目的 Android 清单都定义了:

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

and

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

I have this run_all_test.sh script to run all unit and instrumented test:我有这个run_all_test.sh脚本来运行所有单元和仪器测试:

#!/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 connectedCheck -> 执行所有插桩测试

You can start from here to customize it based on your needs following the documentation here: https://developer.android.com/studio/test/command-line您可以从这里开始根据您的需要按照此处的文档进行自定义: https : //developer.android.com/studio/test/command-line

[Android test types] [Android 测试类型]

To run all tests from Command Line using gradlew [About]使用 gradlew 从命令行运行所有测试[关于]

JUnit test (UnitTest suffix) JUnit 测试(UnitTest 后缀)

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

Instrument test(AndroidTest suffix)仪器测试(AndroidTest后缀)

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

If you want just to build tests and don't run them use assemble如果您只想构建测试而不运行它们,请使用assemble

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

//functional
./gradlew :<moduleName>:assemble<variantName>AndroidTest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM