Android测试运行失败:没有测试结果。

22

你好,我正在尝试在我的项目中运行示例插桩测试类,但每次运行时都会收到以下错误:

Test running failed: no test results
Empty test suit

这是我的示例仪器化测试类

import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun useAppContext() {
        // Context of the app under test.
        val appContext = androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().context
        assertEquals("com.app", appContext.packageName)
    }
}

这是我的Gradle测试构建文件:

android {
  compileSdkVersion 28
    defaultConfig {
        applicationId "com.app"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        testInstrumentationRunnerArguments clearPackageData: 'true'

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    testOptions {
        execution 'ANDROID_TEST_ORCHESTRATOR'
    }
}

dependencies {

    def mockito = "2.18.3"
//android instrumental test
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.ext:truth:1.1.0'
    androidTestImplementation 'androidx.test:core:1.1.0'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.arch.core:core-testing:2.0.0'
    androidTestUtil 'androidx.test:orchestrator:1.1.1'

    androidTestImplementation 'com.jraska.livedata:testing-ktx:0.6.0'
    androidTestImplementation 'com.jraska.livedata:testing:0.6.0'

    androidTestImplementation "org.mockito:mockito-core:$mockito"
    androidTestImplementation "org.mockito:mockito-android:$mockito"
    androidTestImplementation "org.mockito:mockito-inline:$mockito"

    androidTestImplementation 'org.koin:koin-test:1.0.2'

    //unit test
    testImplementation 'junit:junit:4.12'
    testImplementation "org.mockito:mockito-core:$mockito"
    testImplementation "org.mockito:mockito-android:$mockito"
    testImplementation "org.mockito:mockito-inline:$mockito"
    testImplementation 'org.koin:koin-test:1.0.2'
    testImplementation 'androidx.test.ext:junit:1.1.0'
    testImplementation 'androidx.arch.core:core-testing:2.0.0'
    testImplementation 'com.jraska.livedata:testing-ktx:0.6.0'
    testImplementation 'com.jraska.livedata:testing:0.6.0'
    testImplementation 'androidx.test.ext:truth:1.1.0'
  }
}

你是怎么运行它的?只需要在Studio中右键单击测试,然后点击“运行...”即可运行测试。 - Diego Torres Milano
是的,我就是这样运行它的。我注意到在我的androidTest资源文件夹中有mockito inline函数,当我去掉它时,我会得到一个不同的错误,说什么mockito是os相关的错误,而mockito inline函数应该可以解决这个问题。 - Jono
有什么建议吗?还是出现相同的错误。 - Jono
1
这是最近Android Studio版本中Kotlin测试的一个错误。请确保您已将Android Studio更新到最新版本,目前为3.2.1。 - Can_of_awe
这个有很大的跟踪日志吗?如果我手动执行gradlew connectedAndroidTest也不起作用。 - Jono
3个回答

36

升级我的Android Studio到3.3版本,并移除

testOptions {
    execution 'ANDROID_TEST_ORCHESTRATOR'
}

工作过


3
Amazeballs - 这是关键!为什么Google不在他们自己的设置中添加这个提示,因为这是早期所需的内容... - slott
5
这是因为谷歌的原因。文档不够详细。 - Jono
1
这太荒谬了。没有任何可读的错误消息,删除这个Gradle选项是必要的。 - Mike76
1
但是...这不是一个答案!如果我需要设置我的测试,以便每个测试都在新安装状态下运行应用程序 - 我需要使用ANDROIDX_TEST_ORCHESTRATOR。 - whereismaxmax
4
在我的情况下,将androidx.test:orchestrator从1.1.0更新到1.2.0有所帮助。 - whereismaxmax
显示剩余2条评论

12

除了测试协调器之外,您完全可以使用AndroidX进行测试设置。而不是删除测试协调器(非常有用),请将

execution 'ANDROID_TEST_ORCHESTRATOR'

更改为

execution 'ANDROIDX_TEST_ORCHESTRATOR'


@Chisko 这个错误可能有很多不同的出现方式。我的回答是针对 OP 的 gradle 文件的。我建议查阅官方文档,确保您已经正确设置了一切:https://developer.android.com/training/testing/set-up-project - Bob Liberatore
兄弟,你以为我没有看过他们的文档吗?我的错误和 OP 的完全一样,你的解决方案不起作用,我不得不删除这行代码,即使我不想,现在 Espresso 可以工作了。 - Chisko
@Chisko 你的错误相同并不意味着它们具有相同的原因。如果这对你很重要,我建议你发布自己的问题,附上你的构建文件。 - Bob Liberatore
1
@Robert Liberatore,这就是我的问题。我甚至没有注意到我没有使用ANDROIDX。谢谢! - DevinM
将其完全删除对我来说解决了问题。 - Nicolas

0

我遇到了同样的问题,因为Android Studio无法安装APK,原因是存在冲突的内容提供者权限。卸载APK可以解决这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接