获得测试项目的情况下在Android的JUnit测试用例 [英] Get context of test project in Android junit test case

查看:206
本文介绍了获得测试项目的情况下在Android的JUnit测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道你怎么能得到的测试项目在Android的JUnit测试案例(扩展AndroidTestCase)的情况下。

Does anyone know how can you get the context of the Test project in Android junit test case (extends AndroidTestCase).

注:测试不是仪器测试

注2:我所需要的测试项目中,被测试的实际应用的不上下文的上下文

Note 2: I need the context of the test project, not the context of the actual application that is tested.

我需要这个从测试项目加载从资产的一些文件。

I need this to load some files from assets from the test project.

推荐答案

在一些研究的唯一可行的解​​决方案似乎是一个yorkw指出了。你不得不延长InstrumentationTestCase,然后您可以访问使用getInstrumentation()测试应用程序的上下文的getContext() - 这是使用上述建议的简要code代码片段:

After some research the only working solution seems to be the one yorkw pointed out already. You'd have to extend InstrumentationTestCase and then you can access your test application's context using getInstrumentation().getContext() - here is a brief code snippet using the above suggestions:

public class PrintoutPullParserTest extends InstrumentationTestCase {

    public void testParsing() throws Exception {
        PrintoutPullParser parser = new PrintoutPullParser();
        parser.parse(getInstrumentation().getContext().getResources().getXml(R.xml.printer_configuration));
    }
}

这篇关于获得测试项目的情况下在Android的JUnit测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆