27

我有一个简单的 android 应用程序,我正在使用我的手机对其进行测试。因此,有两种方法可以做到这一点:

  1. 使用日食
  2. 使用命令行

问题:

当我使用 Eclipse 运行单元测试用例时,它会在运行时在我的手机上安装应用程序并运行 junit test,然后如果我在 CLI 上使用命令: adb -d shell am instrument -w com.abc.xyz.test/android.test .InstrumentationTestRunner,它运行良好。

但是,如果我直接在 CLI 中运行上述命令而没有先在 Eclipse 中运行单元测试用例,则会出现错误:

android.util.AndroidException:INSTRUMENTATION_FAILED:com.abc.xyz.test/android.test.InstrumentationTestRunner
        在 com.android.commands.am.Am.runInstrument(Am.java:586)
        在 com.android.commands.am.Am.run(Am.java:117)
        在 com.android.commands.am.Am.main(Am.java:80)
        在 com.android.internal.os.RuntimeInit.finishInit(本机方法)
        在 com.android.internal.os.RuntimeInit.main(RuntimeInit.java:263)
        在 dalvik.system.NativeStart.main(本机方法)
INSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS:错误=无法找到检测目标包:com.abc.xyz
INSTRUMENTATION_STATUS_CODE:-1

AndroidMANifest.xml 包含:

    android:name="android.test.InstrumentationTestRunner"
    android:targetPackage="com.abc.xyz" 

    inside instrumentation tag

谁能帮帮我

4

3 回答 3

36

我想你会在一月份解决它,但我使用命令行工具,发现了类似的问题(错误消息不同)并像我在以下步骤中解释的那样解决了它。我完成了从创建一个带有空测试的虚拟项目到成功测试运行的整个过程。我希望它对某人有用:

第一步,创建项目:

android create project 
  --name MyExample 
  --target "Google Inc.:Google APIs:17" 
  --path MyExample 
  --package com.example 
  --activity MyExampleActivity

第二步,创建测试项目:

android create test-project 
  --path MyExampleTest 
  --name MyExampleTest 
  --main ../MyExample

第三步,访问你的项目目录,构建它并检查过程是否成功结束:

cd MyExample && ant debug

第四步,安装到模拟器:

adb -s emulator-5554 install -r bin/MyExample-debug.apk

第五步,访问你的测试项目目录并尝试运行测试:

cd ../MyExampleTest && 
adb shell am instrument -w com.example.tests/android.test.InstrumentationTestRunner

这会产生:

INSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{com.example.tests/android.test.InstrumentationTestRunner}
INSTRUMENTATION_STATUS_CODE: -1
android.util.AndroidException: INSTRUMENTATION_FAILED: com.example.tests/android.test.InstrumentationTestRunner
        at com.android.commands.am.Am.runInstrument(Am.java:676)
        at com.android.commands.am.Am.run(Am.java:119)
        at com.android.commands.am.Am.main(Am.java:82)
        at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
        at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:235)
        at dalvik.system.NativeStart.main(Native Method)

第六步,列出你的检测类并确保你当前的项目缺失:

adb shell pm list instrumentation

在我的机器中产生:

instrumentation:com.android.emulator.connectivity.test/android.test.InstrumentationTestRunner (target=com.android.emulator.connectivity.test)
instrumentation:com.android.emulator.gps.test/android.test.InstrumentationTestRunner (target=com.android.emulator.gps.test)
instrumentation:com.android.example.spinner.tests/android.test.InstrumentationTestRunner (target=com.android.example.spinner)
instrumentation:com.android.smoketest.tests/com.android.smoketest.SmokeTestRunner (target=com.android.smoketest)
instrumentation:com.android.smoketest.tests/android.test.InstrumentationTestRunner (target=com.android.smoketest)
instrumentation:com.example.android.apis/.app.LocalSampleInstrumentation (target=com.example.android.apis)

如您所见,仪器com.example.tests不存在,因此我们必须创建它。

第七步,构建你的测试项目并检查它是否成功:

ant debug

第八步,安装到模拟器:

adb -s emulator-5554 install -r bin/MyExampleTest-debug.apk

第九步,列出您的仪器类并查找您的项目之一:

adb shell pm list instrumentation

这会产生:

instrumentation:com.android.emulator.connectivity.test/android.test.InstrumentationTestRunner (target=com.android.emulator.connectivity.test)
instrumentation:com.android.emulator.gps.test/android.test.InstrumentationTestRunner (target=com.android.emulator.gps.test)
instrumentation:com.android.example.spinner.tests/android.test.InstrumentationTestRunner (target=com.android.example.spinner)
instrumentation:com.android.smoketest.tests/com.android.smoketest.SmokeTestRunner (target=com.android.smoketest)
instrumentation:com.android.smoketest.tests/android.test.InstrumentationTestRunner (target=com.android.smoketest)
instrumentation:com.example.tests/android.test.InstrumentationTestRunner (target=com.example)
instrumentation:com.example.android.apis/.app.LocalSampleInstrumentation (target=com.example.android.apis)

看倒数第二个instrumentation:com.example.tests,这是我们想要的。

第十步,运行你的测试:

adb shell am instrument -w com.example.tests/android.test.InstrumentationTestRunner

这会产生:

Test results for InstrumentationTestRunner=
Time: 0.0

OK (0 tests)

就这些。现在实现你的测试,像往常一样编译和安装。此外,您可以像这样删除它们:

adb shell pm uninstall com.example.tests

但是您将需要再次创建检测类以避免相同的错误。

于 2013-07-06T23:32:07.440 回答
2

更精确的解释/方法如下:

确保你这样做

adb install -r bin/<>-debug.apk 

来自测试应用程序目录。

之后ant test应该从测试目录工作。(我的猜测是测试包中缺少对应用程序的依赖 - 这导致了失败)。

除了上面的小技巧,我遵循的其余程序来自http://developer.android.com/上的 android 测试介绍。

于 2015-02-23T19:50:09.847 回答
1

确保您卸载以前的应用程序并重新安装或仅在卸载以前的应用程序后才开始测试

于 2015-10-15T15:54:54.743 回答