Android 测试运行失败:无法找到以下的检测信息:ComponentInfo{} -- 尝试使用 Gradle 在 IntelliJ 中进行测试时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24002212/
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 07:42:53  来源:igfitidea点击:

Test running failed: Unable to find instrumentation info for: ComponentInfo{} -- error trying to test in IntelliJ with Gradle

androidgradleandroid-testing

提问by Nick Thoma

Everytime I try to run my tests the console says this:

每次我尝试运行测试时,控制台都会说:

Running tests
Test running startedTest running failed: Unable to find instrumentation info for:
ComponentInfo{com.employeeappv2.employeeappv2.test/android.test.InstrumentationTestRunner}
Empty test suite.

I've been stuck on this for a while and the solutions I've seen online so far have not helped. My project structure is set up like this:

我已经坚持了一段时间,到目前为止我在网上看到的解决方案都没有帮助。我的项目结构是这样设置的:

*Main Module -src *instrumentTest -java *main -java -manifest *build.gradle

*主模块 -src *instrumentTest -java *main -java -manifest *build.gradle

My build.gradle file looks like this:

我的 build.gradle 文件如下所示:

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

repositories {
    mavenCentral()
}

android {
compileSdkVersion 19
buildToolsVersion "19.1.0"

defaultConfig {
    minSdkVersion 16
    targetSdkVersion 19
    versionCode 1
    versionName "2.1.0"
    testPackageName "login.test"
    testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-   rules.txt'
    }
}

packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/scandit.zip')
compile project(':pullToRefresh')
compile 'com.android.support:appcompat-v7:19.+'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.1+'
compile 'org.springframework.android:spring-android-rest-template:1.0.1+'
compile 'org.json:json:20090211'
compile 'com.fasterxml.Hymanson.core:Hymanson-databind:2.3.1'
compile 'com.fasterxml.Hymanson.core:Hymanson-annotations:2.3.0'
compile 'com.fasterxml.Hymanson.core:Hymanson-core:2.3.1'
compile 'com.android.support:support-v4:19.1.+'
compile 'com.mcxiaoke.volley:library:1.0.+@aar'
androidTestCompile 'junit:junit:3.8'
}

Do you need to have a separate manifest for your tests directory? If so what would that look like?

您的测试目录是否需要单独的清单?如果是这样,那会是什么样子?

Edit: I tried adding a manifest to my instrumentTest directory with no luck. Note that I could not get IntelliJ to resolve the targetPackage name, so it appears red.

编辑:我尝试将清单添加到我的 instrumentTest 目录中,但没有成功。请注意,我无法让 IntelliJ 解析 targetPackage 名称,因此它显示为红色。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.employeeappv2.employeeappv2.src.instrumentTest"
      android:versionCode="1"
      android:versionName="1.0.0">
<application>
    <uses-library android:name="android.test.runner" />
</application>
<instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.employeeappv2.employeeappv2.src.main"/>
</manifest>

采纳答案by Nick Thoma

So the main problem was that when I created an androidTest folder under /src/, it wasn't being picked up by IntelliJ as a source folder for testing (java subdirectory should turn green). I was using IntelliJ 13.0.3 and after upgrading to 13.1.3, all of my troubles went away.

所以主要的问题是,当我在 /src/ 下创建一个 androidTest 文件夹时,它没有被 IntelliJ 选择作为测试的源文件夹(java 子目录应该变成绿色)。我使用的是 IntelliJ 13.0.3,升级到 13.1.3 后,我所有的麻烦都消失了。

*Note: do not try to add a manifest to your androidTest folder, the Gradle docs specifically state that the manifest should be auto-generated when you create the androidTest folder. The problem for me was that the file wasn't being generated as androidTest wasn't being recognized by IntelliJ/Gradle, thus throwing the no instrumentation error.

*注意:不要尝试将清单添加到您的 androidTest 文件夹中,Gradle 文档明确指出在您创建 androidTest 文件夹时应自动生成清单。对我来说的问题是文件没有被生成,因为 androidTest 没有被 IntelliJ/Gradle 识别,因此抛出了无检测错误。

回答by Peter Lamberg

I had the same error when I tried adding multiDexEnabled trueto build.gradle.

当我尝试添加multiDexEnabled truebuild.gradle.

I'm adding my experience here, because this is one of the first Google hits when searching with the ... Unable to find ... ComponentInfo ...error message.

我在这里添加我的经验,因为这是使用... Unable to find ... ComponentInfo ...错误消息进行搜索时第一个 Google 命中之一。

In my case adding testInstrumentationRunnerlike here did the trick:

在我的情况下,testInstrumentationRunner像这里一样添加诀窍:

...
android {
    ...
    defaultConfig {
        ...
        testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
    }
}

(I have com.android.tools.build:gradle:1.5.0)

(我有com.android.tools.build:gradle:1.5.0

回答by Liuting

I am using Android Studio 1.1 and the following steps solved this issue for me:

我使用的是 Android Studio 1.1,以下步骤为我解决了这个问题:

  1. In Run - Edit Configurations - Android Tests
    Specify instrumentation runner as android.test.InstrumentationTestRunner

  2. Then in the "Build variants" tool window (on the left), change the test artifact to Android Instrumentation Tests.

  1. 在将检测运行器Run - Edit Configurations - Android Tests
    指定为android.test.InstrumentationTestRunner

  2. 然后在“构建变体”工具窗口(左侧)中,将测试工件更改为Android Instrumentation Tests.

No testInstrumentationRunner required in build.gradle and no instrumentation tag required in manifest file.

build.gradle 中不需要 testInstrumentationRunner 并且清单文件中不需要检测标记。

回答by VikingGlen

When I created a new package, Studio created an ApplicationTest class. Using us.myname.mypackageas an example, the following directory structure was created:

当我创建一个新包时,Studio 创建了一个 ApplicationTest 类。以 us.myname.mypackage为例,创建如下目录结构:

app/[myPackage]/src/androidTest/java/us/myname/mypackage/ApplicationTest.class

Initially it worked out of the box. It quit working after I installed product flavors. I subsequently made the following changes to build.gradle:

最初它是开箱即用的。在我安装了产品口味后,它就停止工作了。我随后对 build.gradle 进行了以下更改:

defaultConfig {
   ...
   testInstrumentationRunner "android.test.InstrumentationTestRunner"
}

some prefer

有些人更喜欢

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

(With my current configuration, I have to use ...InstrumentationTestRunnerwhen in debug, and AndroidJUnitRunnerwhile using release build type.)

(使用我当前的配置,我必须...InstrumentationTestRunner在调试AndroidJUnitRunner时使用,并在使用发布构建类型时使用。)

The above configuration only works with the debug build type. If you wish to use it with release or with a custom build type, you can include the following in build.gradle:

上述配置仅适用于调试构建类型。如果您希望在发布或自定义构建类型中使用它,您可以在 build.gradle 中包含以下内容:

buildTypes {
     release {
         ...
     }
 debug {
     ...
 }
}
testBuildType "release"

In the Build Variants tab on the lower left side of Studio, make sure you have Android Instrumentation Testsand the correct buildTypeselected.

在 Studio 左下角的 Build Variants 选项卡中,确保您选择Android Instrumentation Tests了正确的buildType选项。

回答by LEO

For what it's worth, AS 2.3 got hung up when i created a custom test runner after using the regular test runner. I got the same error as posted in the question.

就其价值而言,当我在使用常规测试运行程序后创建自定义测试运行程序时,AS 2.3 挂断了。我得到了与问题中发布的相同的错误。

Deleting the Debug Configurations for ALLAndroid Instrumented Tests and rebuilding fixed it. I believe the problem lied in the fact you no longer can choose a custom runner in the Debug Configurations because it's most likely built in via gradle.

删除所有Android Instrumented 测试的调试配置并重建修复它。我相信问题在于您不再可以在调试配置中选择自定义运行器,因为它很可能是通过 gradle 内置的。

回答by Rock Lee

I had to do a combination of VikingGlen's answer, Liuting's answer, and thisanswer. This answer works for Android Studio version 2.1.

我不得不将 VikingGlen 的回答、流亭的回答这个回答结合起来。此答案适用于 Android Studio 2.1 版。

  1. Run -> Edit Configurations... -> General -> Specific instrumentation runner (optional): "android.support.test.runner.AndroidJUnitRunner"

  2. In build.gradle(the one with all your dependencies), put this:

    defaultConfig { testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' }

  1. 运行 -> 编辑配置... -> 常规 -> 特定检测运行器(可选):“android.support.test.runner.AndroidJUnitRunner”

  2. build.gradle(具有所有依赖项的那个)中,输入:

    defaultConfig { testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' }

Then it could run tests of this type:

然后它可以运行这种类型的测试:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class ApplicationTest {

    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void login() {
        //Open Drawer to click on navigation.
        onView(withId(R.id.drawer_layout))
                .check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed.
                .perform(open()); // Open Drawer
    }
}

回答by Daniel

I had this problem and fixed it by going to Run -> Edit Configurations -> Green '+' button at the top left -> JUnit

我遇到了这个问题并通过转到运行 -> 编辑配置 -> 左上角的绿色“+”按钮 -> JUnit 来修复它

From there, set the 'use the classpath mod...' to 'app' (or your default app name, which is the one that appears to the left of the run (play button) when you run the app)

从那里,将“使用类路径模块...”设置为“应用程序”(或您的默认应用程序名称,即运行应用程序时出现在运行(播放按钮)左侧的名称)

Finally, put your test class name in the 'class:' textbox. Click apply and okay.

最后,将您的测试类名称放在“class:”文本框中。单击应用并确定。

At this point, if the test class doesn't have other errors, it should work.

此时,如果测试类没有其他错误,它应该可以工作。

回答by Agnit

This is what I noticed in my project, in my app(main) module build.gradle I had the following buildType configuration

这是我在我的项目中注意到的,在我的 app(main) 模块 build.gradle 我有以下 buildType 配置

buildTypes {
        debug {
            multiDexEnabled true
        }

        mock {
            initWith(buildTypes.debug)
        }
    }
testBuildType "mock"

When I used AndroidJUnitRunneras the test runner(both from Android Studio) and as testInstrumentationRunnerin build.gradle, tests ran without hitch.

当我使用AndroidJUnitRunner作为测试运行程序(都来自 Android Studio)和build.gradle中的 testInstrumentationRunner 时,测试运行顺利。

In a submodule that had multiDexEnabled trueas defaultConfig

multiDexEnabled true作为defaultConfig的子模块中

defaultConfig {
    multiDexEnabled true
    ....
}

I ran into the problem of

我遇到了问题

Test running startedTest running failed: Unable to find instrumentation info for:{mypackage.x.y/android.support.test.runner.AndroidJUnitRunner"}

when I specified AndroidJUnitRunnerin IDE and the submodule build.gradle. And this was fixed by specifying MultiDexTestRunneras the test runner in IDE/build.gradle.

当我在 IDE 和子模块 build.gradle 中指定AndroidJUnitRunner时。这是固定通过指定MultiDexTestRunner如IDE /的build.gradle测试运行。

To summarize, Use MultiDexTestRunnerto run tests when multiDexEnabled trueis specified in build.gradle, else use AndroidJUnitRunneras the test runner.

总而言之,当build.gradle中指定multiDexEnabled true时,使用MultiDexTestRunner运行测试,否则使用AndroidJUnitRunner作为测试运行器。

回答by Sababado

Make sure the app has been uninstalled for all users.

确保已为所有用户卸载该应用程序。

Go to settings -> apps (all apps) -> If your app is there then tap on it -> menu -> uninstall for all users.

转到设置-> 应用程序(所有应用程序)-> 如果您的应用程序在那里,则点击它-> 菜单-> 为所有用户卸载。

My issue was that the app was at one point uninstalled, however still on the device; meaning it was not uninstalled for all users. (Another issue, not sure how to resolve. I'm the only user on my device)

我的问题是该应用程序曾一度被卸载,但仍在设备上;这意味着它没有为所有用户卸载。(另一个问题,不知道如何解决。我是我设备上唯一的用户)

Because of this, the app wouldn't re-install, and the test suite had nothing to run against.

因此,该应用程序不会重新安装,并且测试套件没有任何可运行的内容。

回答by Dino Tw

The solution for my problem is to change the method name from

我的问题的解决方案是将方法名称从

@Test
public void test() {
    ...
}

to

@Test
public void testSomething() {
    ...
}

Hope it helps someone.

希望它可以帮助某人。