我正在使用新的安卓测试支持库(com.android.support.test:runner:0.2
)来运行工具测试(也称为设备或仿真器测试)。
我用@RunWith(AndroidJUnit4.class)
注释我的测试类,并使用Android Studio来运行它们。
对于我的测试用例,我需要一个Context
实例。我可以用InstrumentationRegistry
获得它,但它有两个上下文相关的方法,并且还不清楚区别是什么。
InstrumentationRegistry.getContext()
与InstrumentationRegistry.getTargetContext()
的区别是什么
发布于 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);
https://stackoverflow.com/questions/29969545
复制相似问题