2012-03-27 26 views
44

我正在開發使用Android的java.xml.parsers.DocumentBuilder和DocumentBuilderFactory的實現從XML文件加載信息的軟件。我正在寫我的對象的單元測試,我需要能夠提供各種xml文件,這些文件將用於測試下的代碼。我使用Eclipse並有一個單獨的Android測試項目。我無法找到將測試xml放入測試項目的方式,以便測試下的代碼可以打開文件。如何爲android單元測試提供數據文件

  • 如果我把文件放在測試項目的/ assets中,測試下的代碼看不到它。
  • 如果我把這些文件放在被測代碼的/ assets中,它當然可以看到這些文件,但是現在我只用測試數據文件來弄亂我的實際系統。
  • 如果我手動將文件複製到/ sdcard/data目錄,我可以從測試中的代碼打開它們,但會干擾自動執行我的測試。

任何有關如何讓不同的xml測試文件駐留在測試包中但是對被測代碼可見的建議將不勝感激。

這裏是我如何努力構建單元測試:

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被測試爲系統提供一個背景下,無法從測試包中看到資產。

這是因爲每答案的工作修改後的代碼:

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

回答

65

選項1:使用InstrumentationTestCase

假設你在這兩個Android項目和測試項目有資產的文件夾,你把資產文件夾中的XML文件。在測試項目中的測試代碼,這會從Android項目資產文件夾中加載XML:

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

選項2

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

這將從測試項目資產文件加載XML使用ClassLoader

在您的測試項目中,如果資產文件夾被添加到項目構建路徑(這是由版本r14之前的ADT插件自動完成的),則可以從res或assets目錄(即。在項目構建路徑)目錄,而語境:

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

感謝:

public final class DataStub { private static final String BASE_PATH = resolveBasePath(); // e.g. "./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(); } } 

我投入到下一個路徑中的所有原始文件。這工作,只有我第一次不得不將我的測試案例更改爲InstrumentationTestCase。 – 2012-03-27 23:33:59

+0

我修改了我的問題以顯示工作的代碼。謝謝你的幫助。 – 2012-03-27 23:41:22

+0

選項2爲+1。剛剛在res級別添加了一個資產目錄,並且在Android Studio構建中新代碼「剛剛工作」。不需要構建路徑。 – 2014-08-19 11:23:02

7

對於Android和JVM單元測試我使用下列內容:".../project_root/mymodule/src/test/resources/"

+0

您可以在訪問這些方法時顯示您的測試類的樣子嗎? – JPM 2016-03-18 16:59:01

相關問題