gpt4 book ai didi

java - 使用 JUnitCore 运行参数化测试

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:06:54 36 4
gpt4 key购买 nike

是否可以使用 JUnitCore API 运行参数化测试类?

我有一个名为 Fibonacci 的待测类,一个名为 TestFibonacci 的参数化测试类,以及一个简单的 Java 类 (JUnitParameterized)使用 JUnitCore API 执行 TestFibonacci 类。如果我使用 JUnit 插件或命令行执行 TestFibonacci,它会通过。但是,当我使用我的 JUnitParameterized 类执行它时,它失败了。

被测类

public class Fibonacci {

public static int compute(int n) {
if (n <= 1) {
return n;
}
return compute(n-1) + compute(n-2);
}
}

测试类

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.util.Arrays;

@RunWith(Parameterized.class)
public class TestFibonacci {

@Parameters(name = "{index}: fib({0})={1}")
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 input;
private int expected;

public TestFibonacci(int input, int expected) {
this.input = input;
this.expected = expected;
}

@Test
public void test() {
assertEquals(expected, Fibonacci.compute(input));
}
}

Java程序

import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;

public class JUnitParameterized {

public static void main(String[] args) throws ClassNotFoundException {

Class<?> testClass = JUnitParameterized.class.getClassLoader().loadClass(TestFibonacci.class.getCanonicalName());

Result result = (new JUnitCore()).run(Request.method(testClass, "test"));
System.out.println("Number of tests run: " + result.getRunCount());
System.out.println("The number of tests that failed during the run: " + result.getFailureCount());
System.out.println("The number of milliseconds it took to run the entire suite to run: " + result.getRunTime());
System.out.println("" + (result.wasSuccessful() == true ? "Passed :)" : "Failed :("));
}
}

最佳答案

当一个 JUnit 测试类用 @Parameterized 注释时,作为描述的测试方法名称会用大括号中的数字、冒号和替换的 name 丰富在 @Parameters 注释中提供。

在您的情况下,要执行单个测试,即设置一个参数,您会

Result result = (new JUnitCore()).run(Request.method(TestFibonacci.class, "test[6: fib(6)=8]"));

在这种情况下,提供第 6 个参数集(从零开始)。
请注意,您必须将您在方法名称中提供的数字与您声明为参数的数字完全匹配。序列号也很重要。因此以下内容不起作用:

 "test[3: fib(6)=8]" (wrong sequence .. <6, 8> pair is 6th, not 3rd)
"test[6: fib(50)=100]" (the pair <50, 100> is not declared in parameters)

为了避免对 @Parameters(name=?) 值进行不必要的解析,我建议只声明不带 name 的参数:

@Parameters // no name=?
public static Iterable<Object[]> data() {
return Arrays.asList(
new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
}

然后测试方法描述就是test[6]:

Result result = (new JUnitCore()).run(Request.method(TestFibonacci.class, "test[6]"));

要使用所有参数运行单个测试方法,我建议在一个循环中执行(并可能汇总结果):

int parametersCount = Request.aClass(TestFibonacci.class).getRunner().getDescription().getChildren().size();
for (int i = 0; i < parametersCount; ++i) {
Result result = (new JUnitCore()).run(Request.method(testClass, "testFloat[" + i + "]"));
System.out.println("Result " + result.wasSuccessful());
}

关于java - 使用 JUnitCore 运行参数化测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35351863/

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