Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing instrumentation test resources

Tags:

I am trying to find a way for my instrumentation tests to have access to string resources generated within the test package.

More details:

I have an Android test case which inherits from ActivityInstrumentationTestCase2. I used Eclipse's New Android Test Project to create the test in the first place. The test project has resources (similar to the resources for a regular Android project). I am trying to find a way to programmatically access String resources in the test project in the various individual tests. I have tried:

String s = getInstrumentation().getContext().getString(R.string.blah);

and

String s = mActivity.getApplicationContext().getString(R.string.blah);

Both methods throw a NotFoundException. I have the string "blah" defined in my strings.xml. R in code above is an import from my test package and not the package of the application under test. I am able to access resources defined in the application package with the latter call.

It would be useful to figure out a way to access XML defined string resources in my tests (as I want to avoid typing strings into code). What am I doing wrong?

like image 897
Vasanth Asokan Avatar asked Mar 07 '11 06:03

Vasanth Asokan


People also ask

What are instrumentation tests?

Note: Instrumented test, also known as instrumentation tests, are initialized in a special environment that gives them access to an instance of Instrumentation. This class provides access to the application context and APIs to manipulate the app under test and gives instrumented tests their name.

What is instrumentation test runner?

An Instrumentation that runs various types of TestCase s against an Android package (application).

Which package do we need to use for writing Android test cases?

You will useAndroid studio to create an Android application under a package com. tutorialspoint.


2 Answers

Even if this post is no longer current - this addition might help someone:

To understand how this works, you have to keep in mind that the context your instrumentation lives in is another than the context of the application you're running your test against. As you stated correctly, you CAN'T access the resources of your target from the instumentation context. You can define some for your instrumentation context in a separate xml-file inside the test project, or - if you want to make use of the predefined resources - you can get them this way:

 Resources res = getInstrumentation().getTargetContext().getResources();
 res.getString(R.string.xxx); // get a string resource

Neither the instrumentation context (as Wujun wrote), nor the target context is available, until the testcase construction has fully completed.

like image 71
cody Avatar answered Sep 24 '22 11:09

cody


I tried:

String s = getInstrumentation().getContext().getString(com.myProject.test.R.string.blah);

where com.myProject is the package of the project to be tested and com.myProject.test is the package of the testing project.

It worked for me if I place the code inside setup(). It doesn't work if I place it inside the constructor.

like image 24
Wujun Avatar answered Sep 25 '22 11:09

Wujun