gpt4 book ai didi

java - 在 TestNG 中使用带有 DataProvider 的文本文件

转载 作者:行者123 更新时间:2023-11-28 21:05:07 25 4
gpt4 key购买 nike

我正在尝试使用带有 dataProvider 的 TestNG 对 java 程序进行一些测试。

当我手动填充 dataProvider 时,一切正常。现在我正在尝试将文本文件与数据提供者一起使用,其中每一行都是测试的输入。我需要的不仅仅是打印每一行,我还必须阅读每一行,操作并生成预期结果。因此,对于我将发送给测试的每一行,行本身和预期结果。然后在测试中,程序会对该线路进行测试,并生成实际结果,最终比较预期实际结果。 p>

我已经做了第一次尝试,但它并没有像我预期的那样工作,而且性能真的很差。

我在网上搜索过,但仍然找不到从文本文件 (.txt) 将数据绑定(bind)到 dataProvider 的正确方法

我当前的代码(简化版)是:

@DataProvider(name="fileData")
public Object[][] testData() throws IOException {
int numLines = 0;
int currentLine = 0;
String sended = "";
File file = new File("file.txt");

//counting lines from file
BufferedReader br = new BufferedReader(new FileReader(file));
while ((br.readLine()) != null){
numLines++;
}
br.close();

//extracting lines to send to test
String[][] testData = new String[numLines][2];
BufferedReader br2 = new BufferedReader(new FileReader(file));
while ((sended = br2.readLine()) != null){
String expected = sended.substring(50, 106) + "00" + sended.substring(106, 154);
testData[currentLine][0] = sended;
testData[currentLine][1] = expected;
currentLine++;
}
br2.close();
return testData;
}

希望你能帮帮我,谢谢

最佳答案

如果您使用 Java 7,这里有一些示例代码:

@DataProvider
public Iterator<Object[]> testData()
throws IOException
{
final List<Object[]> list = new ArrayList<>();

for (final String line: Files.readAllLines(Paths.get("whatever"),
StandardCharsets.UTF_8)
list.add(new Object[]{ line, process(line) };

return list.iterator();
}

private static Whatever process(final String line)
{
// whatever
}

关于java - 在 TestNG 中使用带有 DataProvider 的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21935598/

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