在测试项目中存储测试文件测试、文件、项目

2023-09-04 05:49:59 作者:眼泪都在笑

我写了使用XML文件的一些测试。我有两个项目之一,code,第二个与测试。我想存储此XML文件(它们包含在测试期间使用的一些数据)中的测试项目。但它是不可能的,因为它似乎从SRC项目只有文件被加载到设备。有谁知道解决这个问题的方法是什么?

I wrote some tests that use XML files. I have two project one with code, the second with tests. I would like to store this XML files (they contain some data used during the tests) in the test project. But it is not possible because it seems that only files from src project are loaded to a device. Does anyone know the way to solve this problem ?

推荐答案

假设你俩Android项目和测试项目中获得的资产文件夹,你把XML文件中的资源文件夹中。在测试项目测试code,这将加载XML从Android项目资产的文件夹:

Suppose you got assets folder in both android project and test project, and you put the XML file in the assets folder. in your test code under test project, this will load xml from the android project assets folder:

getInstrumentation().getTargetContext().getResources().getAssets().open(testFile);

这将加载XML从测试项目资产文件夹:

This will load xml from the test project assets folder:

getInstrumentation().getContext().getResources().getAssets().open(testFile);

请确保您的TestCase类扩展 android.test.InstrumentationTestCase 而不是 android.test.AndroidTestCase 访问 this.getInstrumentation()方法。

Make sure your TestCase class extends android.test.InstrumentationTestCase instead of android.test.AndroidTestCase to access this.getInstrumentation() method.