gpt4 book ai didi

java - 从 .txt 文件对整个数据集运行 JUnit (JAVA) 测试

转载 作者:行者123 更新时间:2023-11-30 06:53:18 24 4
gpt4 key购买 nike

JUnit 对我来说是新事物,我正在按如下方式测试我的应用程序:

@Before
code...

@After
code...

@Test
test for data1

@Test
test for data2...

@Test
test for dataN

但我不希望我的测试需要几百行,因为我只是针对不同的参数测试相同的方法。我想做这样的事情,但我想获得每个测试的结果

@Test
public final void testAll(){

String data = load from file
String bool = load from file
boolean expectedResult;
if(bool.equals("T")
expectedResult = true;
else
expectedResult = false;
assertEquals(expectedResult, testedMethod(data);
}

testAll() 中对所有数据进行某种循环。

最佳答案

您需要的是 Parameterized测试。例如(来自 wiki)这个:

@RunWith(Parameterized.class)
public class FibonacciTest {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }
});
}

private int fInput;

private int fExpected;

public FibonacciTest(int input, int expected) {
fInput= input;
fExpected= expected;
}

@Test
public void test() {
assertEquals(fExpected, Fibonacci.compute(fInput));
}
}

就是一个人的样子。

您可能还想查看 TestNG's Data Providers哪个更有用。

关于java - 从 .txt 文件对整个数据集运行 JUnit (JAVA) 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37725545/

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