首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >getTargetContext()和getContext (在InstrumentationRegistry上)有什么区别?

getTargetContext()和getContext (在InstrumentationRegistry上)有什么区别?
EN

Stack Overflow用户
提问于 2015-04-30 21:49:21
回答 1查看 16.7K关注 0票数 40

我正在使用新的安卓测试支持库(com.android.support.test:runner:0.2)来运行工具测试(也称为设备或仿真器测试)。

我用@RunWith(AndroidJUnit4.class)注释我的测试类,并使用Android Studio来运行它们。

对于我的测试用例,我需要一个Context实例。我可以用InstrumentationRegistry获得它,但它有两个上下文相关的方法,并且还不清楚区别是什么。

InstrumentationRegistry.getContext()InstrumentationRegistry.getTargetContext()的区别是什么

EN

回答 1

Stack Overflow用户

发布于 2017-04-20 18:38:45

您可能需要InstrumentationRegistry.getContext()来访问测试用例的原始资源。

例如,要在TestCase中访问app/src/androidTest/res/raw/resource_name.json

final Context context = InstrumentationRegistry.getContext(); InputStream is = context.getResources().openRawResource(com.example.package.test.R.raw.resource_name);

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29969545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档