为什么 Android Studio 说 "Test events were not received"?

Why does Android Studio say "Test events were not received"?

我正尝试在我的 android 应用程序中进行单元测试,这是我正在做的简单测试教程。

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class ServerListManagerTest extends AndroidTestCase{

   @Test
   public void testTrueIsTrue() throws Exception {
    assertEquals(true, true);
    }
}

目录是这样的,src\main\androidTest\java\some packages\ServerListManagerTest.java

我尝试更改此目录,并构建配置。 但是 android 虽然构建成功,但工作室仍然无法识别我的单元测试。

这是我在应用程序中的build.gradle,

apply plugin: 'com.android.application'

android {
   compileSdkVersion 21
   buildToolsVersion "21.1.2"

   defaultConfig {
       applicationId "com.kaist.se.pmpapp"
       minSdkVersion 16
       targetSdkVersion 21
       versionCode 1
       versionName "1.0"
   }

buildTypes {
       release {
          minifyEnabled false
          proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
         }
      }
sourceSets { main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] } } }



dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    androidTestCompile 'org.robolectric:robolectric:2.4'
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile group: 'junit', name: 'junit', version: '4.12'
  }

我的代码有什么问题????

我假设您使用的是 Android Studio 版本 1.2,这是目前最新的。

我认为您的代码没有任何问题。根据 Jason Atwood's post,问题似乎与 gradle 缓存以前的结果有关,而不是 运行 再次缓存。如果您查看 "Gradle console",您会看到所有内容都显示 "UP-TO-DATE"。但是,他建议在脚本参数中添加“--rerun-tasks”选项对我来说还不够。

在“--rerun-tasks”的添加中,我不得不关闭进程内构建并强制它调用外部gradlew 工具。为此,请转到...

File > Settings > Build, Execution, Deployment > Compiler

然后取消选中 "Use in-process build" 选项。希望 Android Studio 的未来版本能够解决这个问题,我们可以重新启用该选项。

我遇到了同样的问题Test events were not received,我检查了我写的函数,发现有一个编译错误。

我的解决方法是check the files/functions you are testing可能存在编译错误

这可能会对遇到同样问题的人有所帮助。

有时您可能遇到简单的编译器问题并以这条消息结束。这发生在我身上。没有直接指出编译器问题必须向下滚动堆栈跟踪才能找到问题所在。

如果您有多种口味,您可能选择了另一种口味,而不是您测试的口味。例如。如果你有 proddebug 风格,并且你的测试在 debug 中,你可能已经切换到 prod 之一,然后你会得到同样的错误。选择 debug 风格将解决问题。

就我而言,除了解决编译问题外,none 的答案都有效。但是,使 Android Studio 的缓存无效就可以了。

  1. 转到File
  2. Invalidate Caches...
  3. 点击Invalidate and Restart

就我而言,问题是方法声明中缺少我编写的测试方法之一 public。喜欢

@Test
void testSomething() {
   //
}

相对于

@Test
public void testSomething() {
}

有趣的是,即使我试图在同一个 class 中调试其他一些正确定义的方法,框架也找不到 class.

中的任何测试

所以不仅仅是语法错误,单元测试框架的语义问题也让一切都变得糟糕。

在我的例子中,解决方案是去 androidStudio -> 首选项 -> 构建、执行、部署 -> 构建工具 -> Gradle 然后,更改 Gradle JDK(从 15 到 13)

此致