android.util.AndroidException : INSTRUMENTATION_FAILED:

标签 android instrumentation

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

  1. 使用 eclipse
  2. 使用 CLI

问题:

当我使用 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
        at com.android.commands.am.Am.runInstrument(Am.java:586)
        at com.android.commands.am.Am.run(Am.java:117)
        at com.android.commands.am.Am.main(Am.java:80)
        at com.android.internal.os.RuntimeInit.finishInit(Native Method)
        at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:263)
        at dalvik.system.NativeStart.main(Native Method)
INSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation target package: com.abc.xyz
INSTRUMENTATION_STATUS_CODE: -1

AndroidMAnifest.xml contains:

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

    inside instrumentation tag

谁能帮帮我

最佳答案

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

第一步,创建项目:

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)

第六步,列出你的instrumentation clases并确保你当前的项目缺失:

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

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

关于android.util.AndroidException : INSTRUMENTATION_FAILED:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14269687/

相关文章:

java - 使用 Javassist 实现 ClassFileTransformer

ruby-on-rails - 如何检测 Rails 应用程序并识别内存/性能瓶颈?

android - 如何直接从 Intent 打开字体大小设置?

Android Google 登录无法在另一台计算机上运行

Android ScrollView fillViewport 不工作

android - 运行仪器测试 ionic Gitlab CI 时出现不兼容的 AVD 错误

android - 单元/仪器测试 Android Gradle。仪器测试不会运行

Java 字节码检测 : NullPointerException in reflective call to defineClass

java - Android图像淡入淡出动画

android - 导航组件,检查返回堆栈中是否存在 fragment