gpt4 book ai didi

unit-testing - 适用于所有测试的 JUnit 理论 - 仅需要参数化设置方法

转载 作者:行者123 更新时间:2023-11-28 20:32:59 25 4
gpt4 key购买 nike

我在一个类中进行了一些测试,这些测试都依赖于设置方法中设置的变量。因此,如果我可以使用不同的全局变量多次运行所有测试,那就太好了。

我知道您可以将 Theory 用于单个测试,但我想将它用于所有测试。我的第一种方法导致设置方法不能有任何参数的错误。

我有解决办法吗?我不想注释类中的每个测试并添加一些配置逻辑,如果这可以在中央设置方法中处理的话。

最佳答案

你看过Parameterized了吗? ?

@RunWith(Parameterized.class)
public class FibonacciTest {
@Parameters
public static Iterable<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));
}
}

Each instance of FibonacciTest will be constructed using the two-argument constructor and the data values in the @Parameters method.

关于unit-testing - 适用于所有测试的 JUnit 理论 - 仅需要参数化设置方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9608784/

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