Android Studio 单元测试:无法找到检测或类未找到前

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

Android Studio Unit Testing: unable to find instrumentation OR class not found ex

androidunit-testingjunitandroid-studio

提问by Whitney Imura

I have a test suite within my Android studio that has the following dir structure:

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

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

Within my AndroidManifest, I have the following:

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

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

When I run my tests from the command line using: ./gradlew connectedInstrumentTest, I simply get:

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

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

When I run the tests from inside Android Studio I get:

当我从 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.

I also have the testPackageName specified in my defaultConfig in the build.gradle file. Am running Android Studio 0.4.6 on Ubuntu 12.0.4.

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

Any ideas?

有任何想法吗?

回答by Kaschwenk

I had the same problem and could not find anything on the net. The answer was pretty simple: You have to edit the RunConfiguration of your test so that it is not a Android Test but a JUnit test.

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

So basically I did the following:

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

  1. Clicked "Edit Configuration"
  2. Clicked "Add new Configuration" and chose JUnit
  3. Inserted a name and the and chose "All in package" at Test kind.
  1. 点击“编辑配置”
  2. 单击“添加新配置”并选择 JUnit
  3. 在测试种类中插入名称和选择“All in package”。

That's it.

就是这样。

回答by BomberBus

I also get error like this and I added the following line in build.gradle file as shown in https://google.github.io/android-testing-support-library/docs/espresso/setup/index.htmland it's working. I hope it will also help you :-)

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

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

回答by MatPag

I had the same problem, what happened to me was an error with the configurations of tests.

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

Be sure that you have the correct base configurations for testing like those below in the image:

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

enter image description here

在此处输入图片说明

If you don't have an Android Instrumented Testsconfiguration, you can create one from the yellow arrow.

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

PS: Moreover check that classes referenced by configurations really exists and the package name is correct

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

回答by Kannan_SJD

Basically three things to cross check:

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

  1. Make sure your have added testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  1. 确保您已添加 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

in your defaultconfig of build.gradle of that project/ module (in case of sub module)

在该项目/模块的 build.gradle 的默认配置中(在子模块的情况下)

  1. Make sure you have added following test compile dependencies:

    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. Make sure you are adding your test files to the AndroidTest folders and not just to test folder.

  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 文件夹,而不仅仅是测试文件夹。

回答by Michael Kern

I tried various methods but none fixed the issue for me. What I did is simply make a copyof the java test file into the same exact folder but with a different name and deleted the original.

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

I believe this issue was originally caused by moving my test class from the project test folder into the androidTestfolder.

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

回答by Akshat

I removed the entry android.support.test.runner.AndroidJUnitRunnerfrom the Android Test configuration and it started to run the tests for me

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