unit-testing - 为什么 Android Studio 会说 "Test events were not received"?

标签 unit-testing android-studio junit gradle android-gradle-plugin

我正在尝试在我的 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 studio 仍然无法识别我的单元测试。

这是我在 app 中的 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 控制台”,您会看到所有内容都显示“UP-TO-DATE”。但是,他对脚本参数添加“--rerun-tasks”选项的建议对我来说还不够。

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

File > Settings > Build, Execution, Deployment > Compiler

然后取消选中“使用进程内构建”选项。希望 Android Studio 的 future 版本能够解决这个问题,我们可以重新启用该选项。

关于unit-testing - 为什么 Android Studio 会说 "Test events were not received"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30394256/

相关文章:

ios - 我无法在 UnitTest 中获取 BundleVersionKey、BundleIdentiferKey

c - 基本单元测试和 C,我如何开始?

java - 显示包含弃用 Android Studio 的类

java - 如何从命令行在 JUnit 中运行测试用例?

javascript - 如何检查是否使用 sinon.useFakeTimers 调用了 clearTimeout?

Android studio 3.0 缺少可绘制文件夹

android-studio - android studio 中的 flutter 说找不到要转到的声明......我正在使用它的最新版本 v1.7.8+hotfix.4

java - 使用泛型模拟服务方法

java - NoClassDefFoundError - RunListener : Maven-surefire/JUnit

spring - 使用Spring和Jersey测试框架进行单元测试