Unit testing 为什么安卓工作室会说;“未收到测试事件”;?

Unit testing 为什么安卓工作室会说;“未收到测试事件”;?,unit-testing,android-studio,junit,gradle,android-gradle-plugin,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

我正在尝试在我的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仍然不承认我的单元测试,尽管构建是成功的

这是我的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版,目前最新版本

我认为你的代码没有任何问题。根据,问题似乎与gradle缓存以前的结果而不再运行它有关。如果你看“Gradle控制台”,你会看到所有东西都在说“最新”。但是,他建议在脚本参数中添加“-reruntasks”选项,这对我来说是不够的

在--“重新运行任务”中,我必须关闭进程内构建并强制它调用外部
gradlew
工具。要执行此操作,请转到

File > Settings > Build, Execution, Deployment > Compiler

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

我尝试了您的建议,现在可以看到消息“未找到给定包含的测试”,尽管我的测试代码与上面相同。至少这是一个不同的问题,因此我们正在取得进展。我相信你的考试在错误的目录上。我在
src/test/java/…
(即与main并行)中有我的
src/androidTest/java
用于在设备本身上运行的测试。在报告中,单元测试成功,但如上所述,未收到测试事件。根据您的建议,我刚刚禁用了“使用进程内构建”,现在我在中看到了结果。我讨厌工具里的这种东西。为什么它们不能像预期的那样开箱即用,而不让用户花费数小时寻找未知错误,然后最终在web上搜索修复程序。正如我前面提到的,它是有效的,但我有一个奇怪的问题。在报告中,单元测试似乎只运行了一次。但在AS中,有“Gradle测试执行器1”和“Gradle测试执行器2”分别运行相同的测试两次。这对我来说不是一个严重的问题,但我能解决这个问题吗?@SinJeong hun我刚刚在Android Studio 1.2.2上试过这个,事实上只有“进程内构建”选项起作用。
--reruntasks
开关没有改变任何东西。我感觉到了你的痛苦,而不是做实际的工作,我被迫处理这些事情。