如何提供数据文件为Android单元测试单元测试、文件、数据、Android

2023-09-12 09:13:35 作者:▄︻┻═┳

我开发了采用Android的实现java.xml.parsers.DocumentBuilder和的DocumentBuilderFactory从XML文件加载信息的软件。我写我的对象的单元测试,我需要能够提供多种XML文件,将被测行使code。我使用的Eclipse和有一个独立的Andr​​oid测试项目。我不能找到一个办法把测试XML到测试项目中,使得被测code可以打开的文件。

如果我把/测试项目的文件资源,被测code还看不出来。 如果我把文件中的code中的/资产测试,它当然可以看到文件,但现在我搞乱了我的实际系统的测试只有数据文件。 如果我手里的文件复制到/ SD卡/ data目录,我可以从被测code打开它们,但是,随着自动化我的测试干扰。

如何有不同的XML测试文件的任何建议居住在测试包,但可见被测code会大大AP preciated。

下面是我试图构建单元测试:

 公共类AppDesc​​LoaderTest扩展AndroidTestCase
{

  私有静态最后弦乐SAMPLE_XML =sample.xml中;

  私人AppDesc​​Loader m_appDesc​​Loader;
  私人应用m_app;

  保护无效设置()抛出异常
  {
    super.setUp();
    m_app =新的应用程序();
    //下测试呼叫系统使用加载m_app
    //示例XML文件
    m_appDesc​​Loader =新AppDesc​​Loader(m_app,SAMPLE_XML,的getContext());
  }

  公共无效testLoad_ShouldPopulateDocument()抛出异常
  {
    m_appDesc​​Loader.load();

  }

}
 

此没有工作作为SAMPLE_XML文件是在试验的范围内,但AndroidTestCase正在提供的上下文被测系统,这是无法看到的资产从测试包

如何进行Android单元测试

这是每个答案因为工作的修改code:

 公共类AppDesc​​LoaderTest扩展InstrumentationTestCase
{
   ...
  保护无效设置()抛出异常
  {
    super.setUp();
    m_app =新的应用程序();
    //下测试呼叫系统使用加载m_app
    //示例XML文件
     m_appDesc​​Loader =新AppDesc​​Loader(m_app,SAMPLE_XML,getInstrumentation()的getContext());
  }
 

解决方案

选项1 :使用InstrumentationTestCase

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

  getInstrumentation()getTargetContext()getResources()getAssets()开(TESTFILE)。。
 

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

  getInstrumentation()的getContext()getResources()getAssets()开(TESTFILE)。。
 

选项2 :使用的ClassLoader

在您的测试项目中,如果资产文件夹添加到项目构建路径(这是自动ADT插件版本R14前完成),您可以加载资源或资产目录文件(项目构建路径下,即目录)没有上下文

 字符串文件=财产/ sample.xml中;
InputStream的时间= this.getClass()getClassLoader()的getResourceAsStream(文件)。;
 

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.

If I put the files in /assets of the test project, the code under test cannot see it. If I put the files in the /assets of the code under test, it can of course see the files, but now I'm cluttering up my actual system with test only data files. If I hand copy the files to the /sdcard/data directory, I can open them from the code under test, but that interferes with automating my tests.

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

  }

}

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

解决方案

Option 1: Use InstrumentationTestCase

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

This will load xml from the test project assets folder:

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

Option 2: Use ClassLoader

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