gpt4 book ai didi

java - 在 eclipse 中运行 android junit 测试时将数据保存到文件

转载 作者:行者123 更新时间:2023-12-01 04:48:46 25 4
gpt4 key购买 nike

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

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

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).
}

最佳答案

测试执行期间无法在本地保存文件。您可以将其保存在例如 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");

关于java - 在 eclipse 中运行 android junit 测试时将数据保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15382215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com