Code-Apprentice
Code-Apprentice

Reputation: 83517

Android assets used for testing

I have a CSV file that I want to make available for my Android testing suite. I don't want it to be available for the main app, though. I saved it in the assets folder of my test project. Then I try to access it from a test using

context.getAssets().open(DATA_ASSET);

where DATA_ASSET is declared as

DATA_ASSET = "cards.csv"

I set the context to refer to the Activity which I am testing. However, this obviously won't work as I have it since it will look in the assets of the main app. I see two possibilities to fix this:

  1. Provide the correct path to the asset installed with the test project
  2. Obtain a Context which refers to the test project's resources and assets

I haven't found any way to do either of these yet. Perhaps there is a third solution that I haven't thought of. How can I access the assets that are installed with the test project?

Upvotes: 0

Views: 380

Answers (1)

acj
acj

Reputation: 4821

If you're subclassing InstrumentationTestCase or a similar class, you can obtain a Context for the test project by doing the following:

Context ctx = getInstrumentation().getContext();

Upvotes: 1

Related Questions