- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在使用新的 Android 测试支持库 (com.android.support.test:runner:0.2
) 来运行 Instrumentation Tests(又名设备或仿真器测试)。
我用 @RunWith(AndroidJUnit4.class)
注释我的测试类并使用 Android Studio 运行它们。
对于我的测试用例,我需要一个 Context
实例。我可以使用 InstrumentationRegistry
获得它,但它有两个与上下文相关的方法,不清楚有什么区别。
InstrumentationRegistry.getContext()
与 InstrumentationRegistry.getTargetContext()
有什么区别?
最佳答案
InstrumentationRegistry
is an exposed registry instance that holds a reference to the instrumentation running in the process and it's arguments and allows injection of the following instances:
InstrumentationRegistry.getInstrumentation()
, returns the Instrumentation currently running.InstrumentationRegistry.getContext()
, returns the Context of this Instrumentation’s package.InstrumentationRegistry.getTargetContext()
, returns the application Context of the target application.InstrumentationRegistry.getArguments()
, returns a copy of arguments Bundle that was passed to this Instrumentation. This is useful when you want to access the command line arguments passed to Instrumentation for your test.
编辑:
So when to use getContext() vs getTargetContext()?
文档没有很好地解释差异,所以这里来 self 的 POV:
您知道,当您在 Android 上进行仪器测试时,您有两个应用程序:
因此,当您编写测试并希望加载真实应用程序的资源 时,请使用 getTargetContext()
。
如果您想使用您的测试应用的资源(例如,您的一项测试的测试输入)然后调用 getContext()
。
关于android - getTargetContext() 和 getContext(在 InstrumentationRegistry 上)有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29969545/
当我将项目更新到 androidx 时,出现以下错误 我也搜索过,但找不到答案,尽管项目运行良好 @RunWith(AndroidJUnit4.class) public class ExampleI
我正在使用新的 Android 测试支持库 (com.android.support.test:runner:0.2) 来运行 Instrumentation Tests(又名设备或仿真器测试)。 我
AndroidX 迁移后,我的所有 Android 测试都扩展了 AndroidTestCase停止工作。我决定使用@RunWith(AndroidJUnit4.class)相反,但这也已被弃用,甚至
我需要从 Android 测试(AndroidJUnit4ClassRunner)中获取资源。推荐的方法是什么,为什么? String some_res_string = androidx.test.
我是一名优秀的程序员,十分优秀!