我一直在尝试运行 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
这是我的代码。
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)
}
}
我已经添加了所有必需的依赖项。
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'
当我在 test 包中运行相同的测试用例时,它运行成功。 一件事是,我自己创建了 androidTest 包。之前不知何故被删除了..
运行测试用例后我得到了这个,
请帮忙。
在 gradle 模块文件内的 defaultConfig 块中添加
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
该项目中缺少它,但它是必要的。
对于我来说,我需要添加这个依赖项。我不知道为什么它没有显示任何错误。但看起来测试运行器本身仍然需要单独添加。
androidTestImplementation("androidx.test:runner:1.5.2")