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

时间:2011-07-01 12:12:28

标签: android xml unit-testing android-testing

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

1 个答案:

答案 0 :(得分:11)

假设您在android项目和测试项目中都有资源文件夹,并将XML文件放在assets文件夹中。在测试项目下的测试代码中,这将从android项目资产文件夹中加载xml:

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

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

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

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

相关问题