Accessing resources in an android test project

后端 未结 4 1159
天涯浪人
天涯浪人 2020-12-06 10:06

I have setup an android test project that runs junit tests. It\'s using two eclipse projects \"Application\" and \"ApplicationTest\" where my tests are in the \"ApplicationT

4条回答
  •  情深已故
    2020-12-06 10:18

    I would suggest extending ActivityTestCase instead of AndroidTestCase. You can than access test project resources via

    getInstrumentation().getContext().getResources().openRawResource(R.raw.your_res).

    Dummy test case example:

    public class Test extends ActivityTestCase {
    
       public void testFoo() {  
    
          // .. test project environment
          Context testContext = getInstrumentation().getContext();
          Resources testRes = testContext.getResources();
          InputStream ts = testRes.openRawResource(R.raw.your_res);
    
          assertNotNull(testRes);
       }    
    }
    

    And then in test methods use getInstrumentation().getTargetContext() wherever you used getContext() in your AndroidTestCase extension.

提交回复
热议问题