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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3299006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:34:57  来源:igfitidea点击:

Running android unit tests from the command line?

androidunit-testingshell

提问by folone

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.xmlfile, look for the <manifest>element, and look at the packageattribute, which is com.android.email.tests, and in the <instrumentation>element I look at the android:nameattribute, which is android.test.InstrumentationTestRunner. Now I open the console, and run

我正在尝试按照教程在 android 平台上运行单元测试。比如说,我想为电子邮件应用程序运行测试。我打开/apps/Email/tests/AndroidManifest.xml文件,查找<manifest>元素,然后查看package属性,即com.android.email.tests,并在<instrumentation>元素中查看android:name属性,即android.test.InstrumentationTestRunner。现在我打开控制台,然后运行

$ . 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?

所以..我做错了什么?

采纳答案by marekdef

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

package:com.android.email.tests

包:com.android.email

包:com.android.email.tests

回答by Marc Bernstein

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

您可能需要先使用该android create test-project命令设置一个测试项目。查看 Android 开发站点上的此页面:在其他 IDE 中测试以获取更多信息。我已经使用这种方法来启用 ant 的命令行测试。

回答by folone

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.testspackage there.

com.android.email.tests那里没有包裹。

回答by Blundell

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:nameattribute wrong then the target package wrong (above is the correct solution)

首先我的android:name属性错误然后目标包错误(以上是正确的解决方案)

回答by Joshua Pinter

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>/testsso the following command is what I ended up using from my project homedirectory:

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

(cd tests && ant debug install test;)

Hope that helps.

希望有帮助。

回答by korosmatick

For gradle user you can use the following tasks:

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

$ gradle :project:installDebug :project:installDebugAndroidTest

回答by MatPag

I have this run_all_test.shscript 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

回答by cdhabecker

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.

在这种情况下,我收到“无法找到检测信息”错误:我使用与被测项目相同的 src 包名称定义了我的测试项目。例如,两个项目的源代码都在包 com.mycompany.android 中。这个 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" ...>