问题描述
有没有办法为每个测试使用不同的testInstrumentationRunner?我必须为其创建一个 Task
吗?我的跑步者如下:
Is there any way to have different testInstrumentationRunner for each test? Do I have to create a Task
for it? I have my runner as follows :
class MyTestRunner : AndroidJUnitRunner() {
override fun newApplication(
cl: ClassLoader?,
className: String?,
context: Context?
): Application {
return super.newApplication(cl, MyTestApp::class.java.name, context)
}
}
但是,如果我想使用真实的应用程序运行某些软件,该怎么办?我该如何选择呢?
But what if I want to run some using the real app? how can I choose it?
我一直在阅读,这样做可能我在测试中使用 @RunWith(AndroidJUnit4ClassRunner :: class)
,但我知道如果执行此操作,它将使用defaultConfig之一
I've been reading that is possible doing it with@RunWith(AndroidJUnit4ClassRunner::class)
on my tests I'm using it but I understand if I do this it takes the one of the defaultConfig
defaultConfig {
testInstrumentationRunner("mypackage.MyTestRunner")
}
但是我不能使用像 Required:KClass< out Runner!>
@RunWith(MyTestRunner::class)
如果我能做到这一点,我会明白如何使用不同的跑步者.
In case I could do this I'd understand how can I use different runners.
推荐答案
尝试扩展 androidx.test.ext.junit.runners.AndroidJUnit4
:
import androidx.test.ext.junit.runners.AndroidJUnit4
class MyTestRunner : AndroidJUnit4() {
...
}
要使用的依赖项:
// https://mvnrepository.com/artifact/androidx.test.ext
androidTestImplementation "androidx.test.ext:junit:1.1.2"
文档建议将其作为默认运行程序,因此Kotlin的互操作性应该被给予. .kt
文件中的 @RunWith
批注仅接受 KClass< out Runner!>
.
The documentation suggests it as the default runner, therefore Kotlin interoperability should be given. Only KClass<out Runner!>
is being accepted by the @RunWith
annotation in a .kt
file.
这篇关于如何结合不同的testInstrumentationRunner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!