仪器测试未在 Android 中运行 测试通过 0 通过

Roo*_*ore 7 testing android junit4

我一直在尝试运行 androidTest 包中存在的测试用例。但是当我执行测试时,模拟器启动并且测试通过:0 通过。

并在 logcat 中收到此错误

E/AndroidJUnitRunner: An unhandled exception was thrown by the app.


E/InstrumentationResultPrinter: Failed to mark test No Tests as finished after process crash
Run Code Online (Sandbox Code Playgroud)

这是我的代码。

import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.test.assertEquals

@RunWith(AndroidJUnit4::class)
class MyAndroidTest {

    @Test
     fun test_simple() {
        assertEquals(2, 1+1)
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经添加了所有必需的依赖项。

testImplementation 'junit:junit:4.12'
androidTestImplementation 'junit:junit:4.12'

testImplementation "androidx.test.ext:junit-ktx:1.1.3"
androidTestImplementation "androidx.test.ext:junit-ktx:1.1.3"
androidTestImplementation 'androidx.test.ext:junit:1.1.3'

testImplementation "org.robolectric:robolectric:4.6"

testImplementation "com.google.truth:truth:1.1.3"
androidTestImplementation "com.google.truth:truth:1.1.3"


testImplementation 'org.mockito:mockito-core:2.24.5'
// required if you want to use Mockito for Android tests
androidTestImplementation 'org.mockito:mockito-android:2.24.5'
Run Code Online (Sandbox Code Playgroud)

当我在测试包中运行相同的测试用例时,它运行成功。一件事是,我自己创建了 androidTest 包。之前不知何故被删除了..

运行测试用例后我得到了这个,

在此输入图像描述 在此输入图像描述

请帮忙。

Nul*_*ion 2

在 gradle 模块文件中添加testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"defaultConfig 块

该项目中缺少它,但它是必要的。

  • 这已经存在,但我仍然遇到与OP相同的问题 (2认同)