有人知道如何在Android junit测试用例(扩展了AndroidTestCase)中获得Test project的上下文吗.
注:本测试不是仪器测试.
注2:我需要测试项目的上下文,而不是被测试的实际应用程序的上下文.
我需要这个来从测试项目的assets资源 中加载一些文件.
有人知道如何在Android junit测试用例(扩展了AndroidTestCase)中获得Test project的上下文吗.
注:本测试不是仪器测试.
注2:我需要测试项目的上下文,而不是被测试的实际应用程序的上下文.
我需要这个来从测试项目的assets资源 中加载一些文件.
Android测试支持库(目前有androidx.test:runner:1.1.1
个)有了新的方法.Kotlin更新的示例:
class ExampleInstrumentedTest {
lateinit var instrumentationContext: Context
@Before
fun setup() {
instrumentationContext = InstrumentationRegistry.getInstrumentation().context
}
@Test
fun someTest() {
TODO()
}
}
如果您还想运行应用程序上下文:
InstrumentationRegistry.getInstrumentation().targetContext
完整运行示例:https://github.com/fada21/AndroidTestContextExample
Look here: What's the difference between getTargetContext() and getContext (on InstrumentationRegistry)?