如何为androidunit testing提供数据文件

我正在开发使用Android的java.xml.parsers.DocumentBuilder和DocumentBuilderFactory实现从XML文件加载信息的软件。 我写我的对象的unit testing,我需要能够提供各种XML文件,将执行testing的代码。 我正在使用Eclipse,并有一个单独的Androidtesting项目。 我找不到一种方法将testingxml放入testing项目中,以便testing下的代码可以打开文件。

  • 如果我将这些文件放在testing项目的/资产中,则testing中的代码将无法看到它。
  • 如果我把这些文件放在被测代码的/ assets中,那么当然可以看到这些文件,但是现在我只用testing数据文件来弄乱我的实际系统。
  • 如果我手动将文件复制到/ sdcard / data目录,我可以从testing中的代码中打开它们,但会干扰自动化testing。

任何关于如何让不同的xmltesting文件驻留在testing包中的build议,但是被testing的代码都是可见的,我们将不胜感激。

以下是我如何构buildunit testing:

public class AppDescLoaderTest extends AndroidTestCase { private static final String SAMPLE_XML = "sample.xml"; private AppDescLoader m_appDescLoader; private Application m_app; protected void setUp() throws Exception { super.setUp(); m_app = new Application(); //call to system under test to load m_app using //a sample xml file m_appDescLoader = new AppDescLoader(m_app, SAMPLE_XML, getContext()); } public void testLoad_ShouldPopulateDocument() throws Exception { m_appDescLoader.load(); } } 

这不起作用,因为SAMPLE_XML文件在testing环境中,但是AndroidTestCase为被testing的系统提供了一个上下文,无法从testing包中看到一个资产。

这是修改后的代码,每个答案给出:

 public class AppDescLoaderTest extends InstrumentationTestCase { ... protected void setUp() throws Exception { super.setUp(); m_app = new Application(); //call to system under test to load m_app using //a sample xml file m_appDescLoader = new AppDescLoader(m_app, SAMPLE_XML, getInstrumentation().getContext()); } 

选项1 :使用InstrumentationTestCase

假设你在android项目和testing项目中都有了assets文件夹,并且把XML文件放到assets文件夹中。 在testing项目下的testing代码中,这将从android项目资产文件夹中加载xml:

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

这将从testing项目资产文件夹加载xml:

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

选项2 :使用ClassLoader

在您的testing项目中,如果资产文件夹被添加到项目构buildpath(由版本r14之前的ADT插件自动完成),则可以在没有上下文的情况下从res或资产目录(即项目构buildpath下的目录)加载文件:

 String file = "assets/sample.xml"; InputStream in = this.getClass().getClassLoader().getResourceAsStream(file); 

对于Android和JVMunit testing,我使用以下内容:

 public final class DataStub { private static final String BASE_PATH = resolveBasePath(); // eg "./mymodule/src/test/resources/"; private static String resolveBasePath() { final String path = "./mymodule/src/test/resources/"; if (Arrays.asList(new File("./").list()).contains("mymodule")) { return path; // version for call unit tests from Android Studio } return "../" + path; // version for call unit tests from terminal './gradlew test' } private DataStub() { //no instances } /** * Reads file content and returns string. * @throws IOException */ public static String readFile(@Nonnull final String path) throws IOException { final StringBuilder sb = new StringBuilder(); String strLine; try (final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"))) { while ((strLine = reader.readLine()) != null) { sb.append(strLine); } } catch (final IOException ignore) { //ignore } return sb.toString(); } } 

所有原始文件放到下一个path: ".../project_root/mymodule/src/test/resources/"