错误=无法找到以下内容的检测信息:ComponentInfo {}

时间:2014-01-22 22:02:37

标签: java android android-espresso

我想尝试espresso测试,但我一直收到这个错误:

INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{com.mikeestrada.test/android.test.InstrumentationTestRunner}

它工作过一次,但我无法正确地重新创建报告。他们只是空白,没有测试任何东西。 我尝试了很多命令,包括

adb shell am instrument -w -r com.mikeestrada.test/android.test.InstrumentationTestRunner

adb shell am instrument -w -r   com.mikeestrada.test/com.google.android.apps.common.testing.testrunner.GoogleInstrumentation TestRunner

以下是我的代码段:

的AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.myapplication"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="7"
            android:targetSdkVersion="19" />

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.myapplication.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
                <instrumentationandroid:name="com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"                   
    android:targetPackage="com.mikeestrada.test"/>

TestStartScreen.java

    package com.mikeestrada.test;

    import android.test.ActivityInstrumentationTestCase2;
    import android.test.ActivityUnitTestCase;
    import android.test.AndroidTestCase;
    import android.test.suitebuilder.annotation.LargeTest;
    import android.view.View;

    import com.example.myapplication.MainActivity;
    import com.example.myapplication.R;
    import com.google.android.apps.common.testing.ui.espresso.Espresso;
    import com.google.android.apps.common.testing.ui.espresso.ViewAssertion;
    import com.google.android.apps.common.testing.ui.espresso.ViewInteraction;
    import com.google.android.apps.common.testing.ui.espresso.action.ViewActions;
    import com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions;
    import com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers;

    import junit.framework.Assert;
    import org.hamcrest.Matcher;


    public class TestStartScreen extends ActivityInstrumentationTestCase2<MainActivity> {

        public TestStartScreen() {
            super(MainActivity.class);
        }

        @LargeTest
        public void testHelloWorld(final Matcher<View> viewMatcher) {
            // Find
            //ViewInteraction button1 = onView(ViewMatchers.withId(R.id.button1)); // Find the button
            ViewInteraction helloWorldText = Espresso.onView(ViewMatchers.withText("Hello world!")); // Find the text

            // Action
            //button1.perform(ViewActions.click()); // Click the button
            helloWorldText.perform(ViewActions.typeText("Bye World!"));
            Espresso.onView(ViewMatchers.withText(R.id.withText));

            // Check
            helloWorldText.check(ViewAssertions.matches((ViewMatchers.isDisplayed())));  // Hello world text is hidden

            //Espresso.onView(withId(R.id.my_view)).check(matches(withText("Hello  world!")));
        }
    }

的build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 19
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            assets.srcDirs = ['assets']
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    instrumentTestCompile files('libs/espresso-1.1-bundled.jar')
}
android {
    defaultConfig {
        testInstrumentationRunner     "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    }
}
task wrapper (type: Wrapper) {
    gradlerVersion = '1.9'
}

如果它意味着什么 - 清单中<instrumentation>的属性被涂成红色,就好像IntelliJ无法识别它们一样。

任何帮助都很棒,谢谢!

7 个答案:

答案 0 :(得分:69)

您需要检查设备上已安装的仪器包:

 adb shell pm list instrumentation

然后验证 com.mikeestrada.test 是否实际列在那里。

答案 1 :(得分:6)

查看build.gradle文件,问题实际上是你在defaultConfig部分中没有以下配置:

testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"

Espresso需要GoogleInstrumentationTestRunner才能运作。

同样使用gradle构建系统,测试项目不需要AndroidManifest.xml,因为它是自动生成的。

答案 2 :(得分:4)

如果缺少检测包,请从上面的答案中使用以下命令安装它:

$ gradle :{$project}:installDebugAndroidTest

gradle task showing location of installDebugAndroidTest

答案 3 :(得分:0)

此外,您的应用包看起来像 com.mikeestrada 。 因此,在AndroidManifest中将android:targetPackage设置为android:targetPackage="com.mikeestrada"

我希望这会有所帮助。

答案 4 :(得分:0)

问题在于你错过了一个空格:

instrumentationandroid:name

应该是

instrumentation android:name

答案 5 :(得分:0)

https://developer.android.com/studio/test/command-line

看起来您需要确保已安装应用程序和测试apk。

答案 6 :(得分:0)

要正确运行仪器测试,请按照以下说明操作:

  1. 在设备/模拟器上安装基础包。例如,如果要测试 devDebug {flavorBuildType} 组合,请执行 ./gradlew installDevDebug。将 devDebug 字符串替换为您项目中的风味和 buildType。

  2. 在设备/模拟器上安装测试包。如果您安装了 devDebug 现在执行 ./gradlew installDevDebugAndroidTest,默认情况下这将安装添加 .test 后缀

    的基本包
  3. 验证检测是否正确安装,运行 adb shell pm list instrumentation 它应该打印一行包含您的测试包和可用运行器,例如:androidx.test.runner.AndroidJUnitRunner

  4. 启动仪器测试。例如,如果您想从您的包中测试单个测试类,您可以使用:adb shell am instrument -w -e class com.your.base.package.MyClassTest com.your.base.package.test/androidx.test.runner.AndroidJUnitRunnerCheck the doc here for all the available options

  5. 可选:测试完成后,您可以使用 ./gradlew uninstallDevDebug uninstallDevDebugAndroidTest

    卸载软件包
相关问题