如何为android单元测试提供数据文件 [英] How to provide data files for android unit tests

查看:28
本文介绍了如何为android单元测试提供数据文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing software that loads information from XML files using Android's implementation of java.xml.parsers.DocumentBuilder and DocumentBuilderFactory. I am writing unit tests of my objects and I need to be able to provide a variety of xml files that will exercise the code under test. I am using Eclipse and have a separate Android Test Project. I cannot find a way to put the test xml into the test project such that the code under test can open the files.

  • 如果我把文件放在测试项目的/assets下,被测代码看不到.
  • 如果我将文件放在被测代码的/assets 中,它当然可以看到这些文件,但现在我的实际系统只包含测试数据文件.
  • 如果我将文件手动复制到/sdcard/data 目录,我可以从被测代码中打开它们,但这会干扰我的测试自动化.

关于如何将不同的 xml 测试文件驻留在测试包中但对被测代码可见的任何建议将不胜感激.

Any suggestions of how to have different xml test files reside in the test package but be visible to the code under test would be greatly appreciated.

这是我尝试构建单元测试的方式:

Here is how I tried to structure the unit test:

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 文件位于测试的上下文中,但 AndroidTestCase 为被测系统提供了一个上下文,该系统无法从测试包中看到资产.

This did not work as the SAMPLE_XML file is in the context of the test, but AndroidTestCase is providing a context for the system under test, which cannot see an asset from the test package.

这是根据给出的答案修改后的代码:

This is the modified code that worked per answer given:

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 项目和测试项目中都有 assets 文件夹,并将 XML 文件放在 assets 文件夹中.在测试项目下的测试代码中,这将从 android 项目资产文件夹加载 xml:

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);

选项 2:使用 ClassLoader

在您的测试项目中,如果将assets文件夹添加到项目构建路径(在r14版本之前由ADT插件自动完成),您可以从res或assets目录(即项目构建路径下的目录)加载文件而无需Context:

In your test project, if the assets folder is added to project build path (which was automatically done by ADT plugin before version r14), you can load file from res or assets directory (i.e. directories under project build path) without Context:

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

这篇关于如何为android单元测试提供数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆