27

私は単純な Android アプリを持っており、携帯電話を使用してテストしています。したがって、それを行うには2つの方法があります。

  1. 日食の使用
  2. CLI の使用

問題:

Eclipse を使用して単体テスト ケースを実行すると、実行時に電話にアプリがインストールされ、junit テストが実行されます。その後、CLI でコマンドを使用すると、adb -d shell am instrument -w com.abc.xyz.test/android.test が実行されます。 .InstrumentationTestRunner、正常に動作します。

ただし、最初に Eclipse で単体テスト ケースを実行せずに CLI で上記のコマンドを直接実行すると、エラーが発生します。

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

1月以降は解決されていると思いますが、コマンドラインツールを使用して同様の問題(エラーメッセージは異なります)を見つけ、次の手順で説明するように解決しました。空のテストを含むダミー プロジェクトの作成から、テストの実行が成功するまでのプロセス全体を行います。誰かに役立つことを願っています:

最初のステップで、プロジェクトを作成します。

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

2 番目のステップで、テスト プロジェクトを作成します。

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

3 番目のステップでは、プロジェクト ディレクトリにアクセスしてビルドし、プロセスが正常に終了することを確認します。

cd MyExample && ant debug

ステップ 4で、エミュレーターにインストールします。

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

5 番目のステップでは、テスト プロジェクト ディレクトリにアクセスし、テストを実行してみます。

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)

ステップ 6で、インストルメンテーション クラスを一覧表示し、現在のプロジェクトが欠落していることを確認します。

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は存在しないため、作成する必要があります。

7 番目のステップでは、テスト プロジェクトをビルドし、正常に実行されたことを確認します。

ant debug

ステップ 8で、エミュレータにインストールします。

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

ステップ 9では、インストルメンテーション クラスを一覧表示し、プロジェクトの 1 つを探します。

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)

最後から 2 番目を見てください、instrumentation:com.example.testsそれは私たちが望んでいたものです。

10 番目のステップで、テストを実行します。

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 に答える