简体   繁体   English

无法在Android Studio中运行单元测试

[英]Can't run unit tests in Android Studio

I'm trying to make my unit tests work again for two days now, every time that I try to run them I get this error: 我正在尝试让我的单元测试再次工作两天,每次我尝试运行它们时都会出现此错误:

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

This happens trying to run both the test class, or just the methods. 尝试运行测试类或仅运行方法时会发生这种情况。 I've tried to clean all the test configurations and rebuild the project. 我试图清理所有测试配置并重建项目。 Furthermore, I was able to find other people having the same problem , therefore, the proposed solutions were not effective to fix my issue. 此外,我能够找到其他人遇到同样的问题 ,因此,提出的解决方案无法解决我的问题。

This is my gradle file: 这是我的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'
}

And the beginning of the class with the tests: 并且测试类的开头是:

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

Thanks! 谢谢!

Update 更新

I was able to fix partially the problem by changing the references in my AndroidMaifest file. 我能够通过更改AndroidMaifest文件中的引用来部分解决问题。

Therefore, changing the references from: 因此,更改引用来自:

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

To: 至:

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

However, I can't create new tests or even rename my tests methods. 但是,我无法创建新测试甚至重命名我的测试方法。 For example, if I rename from: 例如,如果我重命名为:

@Test
public void testVideoListToTimedClips() throws Exception {

to: 至:

@Test
public void testVideoListToTimedClips2() throws Exception {

I get the error: 我收到错误:

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

Update 2 更新2

Trying things out, seems like Android Studio is keeping the reference to the an old version of the tests. 尝试一下,似乎Android Studio保留了对旧版本测试的引用。

I changed the name of the test method from testVideoListToTimedClips to testVideoListToTimedClips2 resulting in a test class like that: 我将测试方法的名称从testVideoListToTimedClipstestVideoListToTimedClips2从而产生如下测试类:

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

And when I run the test class, Android Studio runs the old method: 当我运行测试类时,Android Studio会运行旧方法:

在此输入图像描述

Are you using Android Studio 2.3.0-beta1? 您使用的是Android Studio 2.3.0-beta1吗? If so, there's a bug that causes your unit tests to not get built when you try to run them. 如果是这样,那么当您尝试运行它们时,会出现导致单元测试无法构建的错误。

You can work around the issue by first running Make project and then Run each time you want to run the unit tests. 您可以通过首先运行Make project然后Run每次要运行单元测试时运行来解决此问题。

This issue is supposed to be fixed in the next canary/beta version of Android studio. 这个问题应该在Android工作室的下一个canary / beta版本中修复。

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

I was facing similar type of issue. 我面临着类似的问题。

I was building using following command : 我正在使用以下命令构建:

gradle clean build -x test

This skips the test cases. 这会跳过测试用例。

instead try : gradle clean build 而是尝试: gradle clean build

Clean your project in IDE. 在IDE中清理项目 This will load latest class file and refresh its references. 这将加载最新的类文件并刷新其引用。

I was able to fix the problem by changing the references in the Manifest file from: 我能够通过更改清单文件中的引用来解决此问题:

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

To: 至:

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

Also, after every change on the tests I need to make my project manually. 此外,在每次测试更改后,我都需要手动创建项目。

I had similar issue. 我有类似的问题。 You need to check whether the issue is in the code or android studio. 你需要检查问题是在代码还是android工作室。 try run unit test using command prompt / terminal 尝试使用命令提示符/终端运行单元测试

./gradlew testDevelopDebugUnitTest

DevelopDebug is flavour that I have in the code so it might be different with yours. DevelopDebug是我在代码中的味道,所以它可能与你的不同。 If that command is executed successfully then the issue is in android studio. 如果该命令成功执行,则问题出在android studio中。

Once I run this command manually then the issue fix itself ( I'm not sure the reason though ). 一旦我手动运行此命令,然后问题自行解决(我不确定原因)。 Hopefully can help you guys. 希望可以帮助你们。 Thank you 谢谢

At some point you should declare your test runner in the module gradle config. 在某些时候,您应该在模块gradle配置中声明您的测试运行器。 For example inside defaultConfig : 例如,在defaultConfig

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM