在eclipse中运行android junit test时将数据保存到文件中

时间:2013-03-13 10:07:49

标签: java android eclipse junit

我在Eclipse中运行Android JUnit测试。我有一个在JUnit运行期间测试的函数。在函数内部,我将一些值保存到本地文件中。但它似乎没有被拯救。是什么原因,以及如何从该功能在控制台中输出一些数据?

它基本上与机器人开发人员提供的示例应用程序中的单元测试代码相同。

public void testDisplayBlackBox() {
        //Enter any integer/decimal value for first editfield, we are writing 10
        solo.enterText(0, "10");
        //Enter any interger/decimal value for first editfield, we are writing 20
        solo.enterText(1, "20");
        //Click on Multiply button
        solo.clickOnButton("Multiply");
        //Verify that resultant of 10 x 20
        assertTrue(solo.searchText("200"));
        ArrayList <View> aview= solo.getCurrentViews();
        for (View v:aview)
        {
            //System.out.println(v.toString());
            //solo.enterText(0, v.toString());
            try {
                FileUtils.writeStringToFile(new File("test.txt"), v.toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //FileUtils 
        }
        //aview.get(0).
    }

1 个答案:

答案 0 :(得分:0)

在测试执行期间,您无法在本地保存文件。你可以将它保存在例如SD卡上,然后你可以通过adb(更多帮助:http://developer.android.com/tools/help/adb.html):

public void testDisplayBlackBox() {
    //Enter any integer/decimal value for first editfield, we are writing 10
    solo.enterText(0, "10");
    //Enter any interger/decimal value for first editfield, we are writing 20
    solo.enterText(1, "20");
    //Click on Multiply button
    solo.clickOnButton("Multiply");
    //Verify that resultant of 10 x 20
    assertTrue(solo.searchText("200"));
    ArrayList <View> aview= solo.getCurrentViews();
    StringBuilder sB = new StringBuilder();
    for (View v:aview)
    {           
        sB.append(v.toString());
    }
    saveToFile(sB.toString())
}


public void saveToFile(String text) {
    try {
        String path = Environment.getExternalStorageDirectory();
        File file = new File(path, test.txt);
        FileOutputStream fos = new FileOutputStream(file);
        byte[] data = text.getBytes();
        fos.write(data);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

对于调试,您可以使用android.util.Log:

Log.d("TEST", "text");