【问题标题】:Android Studio Unit Testing: unable to find instrumentation OR class not found exAndroid Studio 单元测试:无法找到仪器或未找到前类
【发布时间】:2014-02-28 22:16:34
【问题描述】:

我的 Android 工作室中有一个测试套件,其目录结构如下:

-MainProject
 -src
  --com
  --tests
    --java.xx.xxx.xx.test

在我的 AndroidManifest 中,我有以下内容:

<instrumentation
    android:name="android.test.InstrumentationTestRunner"
    android:targetPackage="tests.java.xxx.xxx.xxx.test" />

当我从命令行使用:./gradlew connectedInstrumentTest 运行测试时,我得到:

Tests on Nexus 5 - 4.4.2 failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'

当我从 Android Studio 内部运行测试时,我得到:

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

我还在 build.gradle 文件的 defaultConfig 中指定了 testPackageName。我在 Ubuntu 12.0.4 上运行 Android Studio 0.4.6。

有什么想法吗?

【问题讨论】:

  • 尝试在 defaultConfig 中设置testInstrumentationRunner
  • 包括您的完整项目结构。

标签: android unit-testing junit android-studio


【解决方案1】:

我尝试了各种方法,但都没有为我解决问题。我所做的只是将 java 测试文件的 副本 复制到相同的文件夹中,但名称不同,然后删除原始文件。

我相信这个问题最初是由于将我的测试类从项目测试文件夹移动到androidTest 文件夹中引起的。

【讨论】:

    【解决方案2】:

    我也收到这样的错误,我在 build.gradle 文件中添加了以下行,如 https://google.github.io/android-testing-support-library/docs/espresso/setup/index.html 所示,它正在工作。我希望它也能帮助你:-)

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

    【讨论】:

    • 还将androidTestCompile 'com.android.support.test:rules:0.5'androidTestCompile 'com.android.support.test:runner:0.5' 添加到您正在测试的模块的依赖项中。 (如果使用共享库,如果应用程序是被测试的应用程序,这些行必须在共享库和消费应用程序中——androidTestCompile 由于打包机制的“测试”性质,包含不会自动包含.)
    • 这让我感到困惑,因为我已经在 gradle 中拥有了 testInstrumentationRunner,并且昨天测试工作正常。 Clean and Rebuild 项目什么也没做。我的诀窍:编辑该行,然后再次将其更改回来,所以没有任何变化,但我现在可以运行 gradle Sync(您只需使用菜单中的按钮)。这样就解决了。
    • 我通过在模块的 build.gradle 和应用程序的 build.gradle 中添加 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 来解决我的问题。就我而言,测试位于模块中。
    【解决方案3】:

    我遇到了同样的问题,发生在我身上的是测试配置错误。

    确保您具有正确的基本配置以进行如下图所示的测试:

    如果您没有Android Instrumented Tests 配置,您可以从黄色箭头处创建一个。

    PS:另外检查配置引用的类是否真的 存在且包名正确

    【讨论】:

      【解决方案4】:

      基本上要交叉检查三件事:

      1. 确保您已添加testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

      在该项目/模块的 build.gradle 的默认配置中(如果是子模块)

      1. 确保您已添加以下测试编译依赖项:

        androidTestCompile 'com.android.support.test:rules:0.5'
        androidTestCompile 'com.android.support:support-annotations:25.2.0'
        androidTestCompile 'com.android.support.test:runner:0.5'
        
      2. 确保将测试文件添加到 AndroidTest 文件夹,而不仅仅是测试文件夹。

      【讨论】:

        【解决方案5】:

        我遇到了同样的问题,在网上找不到任何东西。答案很简单:您必须编辑测试的 RunConfiguration,使其不是 Android 测试而是 JUnit 测试。

        所以基本上我做了以下事情:

        1. 点击“编辑配置”
        2. 单击“添加新配置”并选择 JUnit
        3. 插入名称并在测试种类中选择“All in package”。

        就是这样。

        【讨论】:

        • 这个问题发生在我之前工作的测试中,所以不是重新创建所有运行配置的最佳解决方案
        【解决方案6】:

        我从 Android 测试配置中删除了 android.support.test.runner.AndroidJUnitRunner 条目,它开始为我运行测试

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-09-02
          • 1970-01-01
          • 2013-06-07
          • 1970-01-01
          • 2016-03-16
          • 1970-01-01
          • 2018-08-05
          • 1970-01-01
          相关资源
          最近更新 更多