Android Studio“未找到测试”

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

Android Studio "No tests were found"

androidandroid-studiotestinggradle

提问by El Wexicano

Has anyone been able to get tests to run in Android Studio (from the GUI and not terminal), I have been unable to run tests from the GUI.

有没有人能够在 Android Studio 中运行测试(从 GUI 而不是终端),我一直无法从 GUI 运行测试。

Everytime I try to run tests through the GUI I just get the following message:

每次我尝试通过 GUI 运行测试时,我都会收到以下消息:

enter image description here

在此处输入图片说明

I am able to run the tests from terminal using the following command:

我可以使用以下命令从终端运行测试:

./gradlew connectedAndroidTest

I am running Android Studio 0.5.2with Gradle 1.11 with Plugin 0.9.0on Mac OSX

我在Mac OSX上运行带有Gradle 1.11 和插件 0.9.0 的Android Studio 0.5.2

My project structure is as follows;

我的项目结构如下;

MyProject/
   src/
      androidTest/
         java/
             com.myproject.app.test/
                … (tests source code) …
      main/
         java/
             com.myproject.app/
                … (source code) …
         res/
                … (resources code) …
   build.gradle

My build.gradle file looks similar to the following:

我的 build.gradle 文件类似于以下内容:

…
android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        versionCode 12
        versionName "2.0"
        minSdkVersion 9
        targetSdkVersion 19       
    testPackageName "com.test.foo"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }
}
…

If anyone has any suggestions, I will be more than happy to here them.

如果有人有任何建议,我会很高兴在这里他们。

采纳答案by El Wexicano

Ok, I found the cause of my problem.

好的,我找到了问题的原因。

In the misc.xmlfile which is located in the .idea folder of my project had an invalid attribute value for the ProjectRootManagercomponent, which looked like the following:

在位于我的项目的 .idea 文件夹中的misc.xml文件中,ProjectRootManager组件的属性值无效,如下所示:

  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="1.6 (3)" project-jdk-type="JavaSDK">
    <output url="file://$PROJECT_DIR$/build/classes" />
  </component>

So changing the project-jdk-name attribute value to just "1.6", fixed the problem for me. Below is what it looked like after I had updated the value.

因此,将 project-jdk-name 属性值更改为"1.6",为我解决了问题。下面是我更新值后的样子。

  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="1.6" project-jdk-type="JavaSDK">
    <output url="file://$PROJECT_DIR$/build/classes" />
  </component>

Hope this helps anyone out.

希望这可以帮助任何人。

回答by Diego Palomar

Today I had the same problem with some Espresso tests and it was getting me crazy because everything seemed normal. Finally I discovered the problem was because the method annotated with @BeforeClasswas throwing an exception. If something goes wrong in that method, the stacktrace of the exception is not shown in the Log windowof the Run tabbut in the Log windowof the Android Monitor tab

今天我在做一些 Espresso 测试时遇到了同样的问题,这让我很抓狂,因为一切似乎都很正常。最后我发现问题是因为注释的方法@BeforeClass抛出了异常。如果出现错误,该方法中,异常的堆栈跟踪未在显示登录窗口中的运行选项卡,但在登录窗口中的Android的监视器选项卡

If you want to reproduce the problem just add this to your testing class:

如果您想重现该问题,只需将其添加到您的测试类中:

@BeforeClass
public static void setupClass() {
    throw new RuntimeException("Sorry dude, you won't find any test!");
}

回答by Cristan

This can happen when the type of your run configuration is incorrect.

当您的运行配置类型不正确时,就会发生这种情况。

With me, this goes wrong when running an Espresso test with Kotlin: it somehow creates an Android JUnit test which is the wrong type. Manually creating an Android Instrumented Test solves the problem.

对我来说,使用 Kotlin 运行 Espresso 测试时会出错:它以某种方式创建了一个错误类型的 Android JUnit 测试。手动创建 Android Instrumented Test 解决了这个问题。

回答by rdsarna

Just for posterity sakes, here's the solution that worked for me -

只是为了后代的缘故,这是对我有用的解决方案-

The error you're seeing happens if AS for some reason thinks that the test that you're about to run is a unit and not an instrumentation test. Often this error goes away buy just clicking on the package instead of the actual class.

如果 AS 由于某种原因认为您将要运行的测试是一个单元而不是仪器测试,就会发生您看到的错误。通常这个错误会消失,只需点击包而不是实际的类。

Found at https://github.com/googlecodelabs/android-testing/issues/27#issuecomment-219074863

位于https://github.com/googlecodelabs/android-testing/issues/27#issuecomment-219074863

I just ran the tests by right clicking on the package name and choosing "Run 'Tests in {packageName}'" and it worked.

我只是通过右键单击包名称并选择“在 {packageName} 中运行‘测试’”来运行测试并且它工作正常。

回答by gingo

For me it was stupid thing. Simply switching from Releasebuild variant to Debugsolved the problem.

对我来说这是愚蠢的事情。只需从Release构建变体切换即可Debug解决问题。

回答by Muhammad Maqsood

What happened in my case.

我的情况发生了什么。

FYIwe have 2 types of unit tests

仅供参考,我们有两种类型的单元测试

  1. Local unit test (test/java folder for the android studio project)
  2. Instrumentation unit test (androidTest/java folder for the android studio project)
  1. 本地单元测试(android studio 项目的 test/java 文件夹)
  2. 仪器单元测试(android Studio 项目的 androidTest/java 文件夹)

I mistakenly create the Instrumentation test in the local unit test and then I directly copied moved that test from local unit to Instrumentation test. It start saying "No test were found".

我错误地在本地单元测试中创建了 Instrumentation 测试,然后我直接将该测试从本地单元复制到了 Instrumentation 测试。它开始说“没有找到测试”

Solution:Just created a new test class for the Instrumentation test.

解决方案:刚刚为 Instrumentation 测试创建了一个新的测试类。

Always try to create a new test class in a specific test folder and start writing tests in it.

总是尝试在特定的测试文件夹中创建一个新的测试类并开始在其中编写测试。

回答by jakub-adamczewski

Go to:

去:

File => Invalidate caches / Restart... => Invalidate and restart

文件 => 使缓存无效/重新启动... => 使缓存无效并重新启动

This did the trick for me.

这对我有用。

回答by astha

The issue resolved for me just by wiping data from my emulator. In logs, I could see there was not enough space. It was not any config issue for me because the test I ran before running the test that gave me empty suite error worked fine and both test were at the same location in the project.

只需从我的模拟器中擦除数据,这个问题就为我解决了。在日志中,我可以看到没有足够的空间。这对我来说不是任何配置问题,因为我在运行测试之前运行的测试给我空套件错误工作正常,并且两个测试都在项目中的同一位置。

回答by m0skit0

In my case a was using a wrong testing dependency. I had the following in my build.gradle

在我的情况下, a 使用了错误的测试依赖项。我的 build.gradle 中有以下内容

testImplementation 'io.kotlintest.kotlintest-runner-junit5:3.4.2'

As you can see, this is for JUnit5. I changed it to the following

如您所见,这是针对 JUnit5 的。我把它改成了以下

testImplementation 'io.kotlintest.kotlintest-runner-junit4:3.4.2'

And begone, error.

并且过去了,错误。

回答by Rafael Ruiz Mu?oz

In my case, USING KOTLIN, it didn't throw anything. If I run in the terminal the command that it was trying to do:

在我的情况下,使用 KOTLIN,它没有抛出任何东西。如果我在终端中运行它试图执行的命令:

$ adb shell am instrument -w -r -e debug false -e class xx.xxxxxxx.xx.xx.xxx.xxx.HomeActivityTests xx.xxxxxxx.xx.xx.debug.test/android.support.test.runner.AndroidJUnitRunner

$ adb shell am instrument -w -r -e debug false -e class xx.xxxxxxxx.xx.xx.xxx.xxx.HomeActivityTests xx.xxxxxxx.xx.xx.debug.test/android.support.test.runner.AndroidJUnitRunner

it will throw an error:

它会抛出一个错误:

Caused by: org.mockito.exceptions.base.MockitoException:

Cannot mock/spy class xx.xxxxxxx.xx.xx.xxx.xxx.xxx.ProductUseCase

Mockito cannot mock/spy because : - final class at xx.xxxxxxx.xx.xx.rules.useCases.ProductUseCaseRule.(ProductUseCaseRule.kt:36) at xx.xxxxxxx.xx.xx.xxx.xxx.HomeActivityTests.(HomeActivityTests.kt:25) ... 20 more

FAILURES!!!

引起:org.mockito.exceptions.base.MockitoException:

无法模拟/间谍类 xx.xxxxxxx.xx.xx.xxx.xxx.xxx.ProductUseCase

Mockito 无法模拟/间谍,因为: - xx.xxxxxxx.xx.xx.rules.useCases.ProductUseCaseRule.(ProductUseCaseRule.kt:36) at xx.xxxxxxx.xx.xx.xxx.xxx.HomeActivityTests.(HomeActivityTests. kt:25) ... 20 多个

失败!!!

and that shown me that I didn't openmy class, as that's a mustwhen testing with Kotlin

这让我知道我没有open上课,因为在使用 Kotlin 进行测试时这是必须的