【问题标题】:Can't run unit tests in Android Studio无法在 Android Studio 中运行单元测试
【发布时间】:2017-05-16 19:18:39
【问题描述】:

我现在正在尝试让我的单元测试再次运行两天,每次我尝试运行它们时都会收到此错误:

Process finished with exit code 1
Class not found: "XXXXX.XXXXX.XXXXX.XXX"Empty test suite.

试图同时运行测试类或仅运行方法时会发生这种情况。我试图清理所有测试配置并重建项目。此外,我能够找到其他拥有same problem 的人,因此,建议的解决方案无法有效解决我的问题。

这是我的 gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.1'

    defaultConfig {
        applicationId "XXXXXXXXXX"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0.0"
        multiDexEnabled true
    }
    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    dataBinding {
        enabled = true
    }
}

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        // The Fabric Gradle plugin uses an open ended version to react
        // quickly to Android tooling updates
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'me.tatarka:gradle-retrolambda:3.4.0'
    }
}

apply plugin: 'io.fabric'
apply plugin: 'me.tatarka.retrolambda'

ext {
    supportLibVersion = '25.1.0'  // variable that can be referenced to keep support libs consistent
    jomlVersion = '1.8.4'
}

repositories {
    jcenter()
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile "com.android.support:appcompat-v7:${supportLibVersion}"
    compile "com.android.support:design:${supportLibVersion}"
    compile "com.android.support:recyclerview-v7:${supportLibVersion}"
    compile "com.android.support:cardview-v7:${supportLibVersion}"
    compile "com.android.support:support-v4:${supportLibVersion}"
    compile('com.crashlytics.sdk.android:crashlytics:2.6.5@aar')
    {
        transitive = true;
    }
    compile project(':circular-slider')
    compile project(':gpuimage-plus')
    compile 'de.hdodenhof:circleimageview:2.1.0'
    compile 'net.protyposis.android.mediaplayer:mediaplayer:4.2.2-rc1'
    compile 'net.protyposis.android.mediaplayer:mediaplayer-dash:4.2.2-rc1'
    compile 'com.google.code.gson:gson:2.7'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.writingminds:FFmpegAndroid:0.3.2'
    compile 'com.makeramen:roundedimageview:2.3.0'
    compile 'io.reactivex:rxjava:1.2.4'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'com.minimize.android:rxrecycler-adapter:1.2.2'
    compile "org.joml:joml:${jomlVersion}"
    provided 'org.glassfish:javax.annotation:10.0-b28'
    compile 'org.jetbrains:annotations-java5:15.0'
    compile 'com.annimon:stream:1.1.3'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile 'org.powermock:powermock-api-mockito:1.6.5'
    testCompile 'org.powermock:powermock-module-junit4-rule-agent:1.6.5'
    testCompile 'org.powermock:powermock-module-junit4-rule:1.6.5'
    testCompile 'org.powermock:powermock-module-junit5:1.6.5'
    compile 'com.google.android.gms:play-services-appindexing:8.4.0'
    compile 'com.android.support:multidex:1.0.1'
}

和测试类的开始:

@PrepareForTest({XXXXXX.class, XXXXXX.class, XXXXXX.class, XXXXXX.class})
@RunWith(PowerMockRunner.class)
public class VideoPlayerModelUnitTest {

谢谢!

更新

通过更改我的 AndroidMaifest 文件中的引用,我能够部分解决问题。

因此,将引用更改为:

<activity android:name="com.myapp.MusicSelector.MusicSelectorActivity"/>

收件人:

<activity android:name=".MusicSelector.MusicSelectorActivity"/>

但是,我无法创建新测试,甚至无法重命名我的测试方法。例如,如果我从以下位置重命名:

@Test
public void testVideoListToTimedClips() throws Exception {

到:

@Test
public void testVideoListToTimedClips2() throws Exception {

我得到错误:

java.lang.Exception: No tests found matching Method testVideoListToTimedClips2(io.collect.collect.functional.VideoPlayerModelIntegrationTest) from org.junit.internal.requests.ClassRequest@15eb5ee5

更新 2

试一试,Android Studio 似乎保留了对旧版本测试的引用。

我将测试方法的名称从 testVideoListToTimedClips 更改为 testVideoListToTimedClips2,从而生成了这样的测试类:

@PrepareForTest({VideoPlayerModel.class, MediaPlayer.class, Common.class})
@RunWith(PowerMockRunner.class)
public class testVideoPlayerModelIntegration {
    @Test
    public void testVideoListToTimedClips2() throws Exception {

当我运行测试类时,Android Studio 运行旧方法:

【问题讨论】:

    标签: android android-studio gradle junit powermock


    【解决方案1】:

    您使用的是 Android Studio 2.3.0-beta1 吗?如果是这样,则有一个错误会导致您尝试运行单元测试时无法构建它们。

    您可以通过在每次要运行单元测试时先运行Make project 然后运行Run 来解决此问题。

    这个问题应该会在 Android Studio 的下一个 Canary/Beta 版本中得到修复。

    来源: https://code.google.com/p/android/issues/detail?id=230688

    【讨论】:

    • 是的,这正是我的情况。经过一番努力,我找到了解决方案,但是,由于有价值的信息,我会将您的答案标记为正确。还是谢谢!
    • 您找到的解决方案是什么?可能会帮助我:P
    • 我已经创建了运行/调试配置,并将所需的任务手动添加到 Gradle 感知制作中。
    • 谢谢,make project 对我来说是最后一块拼图。还应该提到我的 MultiDexTestRunner 课程不是公开的,这在我解决这个问题后给我带来了一些问题。所以也要公开你的测试运行器?
    【解决方案2】:

    我遇到了类似的问题。

    我正在使用以下命令进行构建:

    gradle clean build -x test
    

    这会跳过测试用例。

    改为尝试:gradle clean build

    在 IDE 中

    清理您的项目。这将加载最新的类文件并刷新其引用。

    【讨论】:

    • 感谢您的建议,但是我尝试通过Android Studio清理项目,但没有成功解决问题。
    【解决方案3】:

    我有类似的问题。您需要检查问题是在代码中还是在 android studio 中。尝试使用命令提示符/终端运行单元测试

    ./gradlew testDevelopDebugUnitTest
    

    DevelopDebug 是我在代码中的风格,因此它可能与您的不同。如果该命令成功执行,则问题出在 android studio 中。

    一旦我手动运行此命令,问题就会自行解决(虽然我不确定原因)。希望能帮到大家。谢谢

    【讨论】:

      【解决方案4】:

      我能够通过更改清单文件中的引用来解决问题:

      <activity android:name="com.myapp.MusicSelector.MusicSelectorActivity"/>
      

      收件人:

      <activity android:name=".MusicSelector.MusicSelectorActivity"/>
      

      此外,每次更改测试后,我都需要手动创建我的项目。

      【讨论】:

        【解决方案5】:

        右键单击要测试的类,然后单击运行。

        【讨论】:

          【解决方案6】:

          在某些时候,您应该在模块 gradle 配置中声明您的测试运行器。比如里面defaultConfig:

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

          【讨论】:

          • 即使添加上面的配置错误仍然存​​在,有一种方法可以验证是否应用了instrumentation runner?
          • 此外,我认为 Instrumentation runner 规范不适用于这里,因为我正在运行本地单元测试。
          • 是的,如果您运行常规单元测试,那么您实际上不需要这样做
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-04-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多