如下所述,I有大量的测试:
对于jUnit测试,预期值(y_position_expected)是硬编码的。测试向方法发送一个值(x_position),该方法执行一些统计并返回结果(y_position_actual)。此结果是与期望值进行比较的实际值。
public class PositionNormalizerTest {
public Normalizers norman ;
@Before
public void beforeFunction() {
norman = DislocationUtils.getPositionService().getNormalizers() ;
}
@Test
public void testAmountForNumberString1() {
String y_position_expected = 100.0d ;
double x_position = <A DOUBLE GOES HERE> ;
double y_position_actual = norman.normalizeYPosition(x_position).getAmount() ;
assertEquals(y_position_expected, y_position_actual, 0.001) ;
}
}
x_position的值来自映射的值,该映射的值要大得多,但与下图相似:
checkpoints = {"alpha":[0.0d, 10.0d,200.0d], "beta":[50.0d, 44.0d,12.0d]}
这个映射的键是字符串,值是双精度列表。因此,测试必须针对每个单个值中的每个单个元素运行。
问题:
考虑到映射(检查点)的大小和测试的数量,手动创建所有测试需要很长时间。因此,我正在寻找一种方法,使具有多个测试用例的单个jUnit测试类自动迭代映射的值并运行测试。我尝试了正常循环,但一旦断言得出结论,可能是失败,也可能是通过,测试用例将结束而不会继续循环。有没有办法做到这一点?我能用注解来做吗?谢谢。
发布于 2019-03-20 04:17:31
使用参数化测试:
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class PositionNormalizerTest {
@Parameters
public static Collection<Object[]> data() {
//here you create and return the collection of your values
return Arrays.asList(new Object[][]{{"alpha", 0.0d, 10.0d, 200.0d}, {"beta", 50.0d, 44.0d,12.0d}});
}
@Parameter
public String key; //alpha
@Parameter(1)
public double d1; //0.0d
@Parameter(2)
public double d2; //10.0d
@Parameter(3)
public double d3; //200.0d
@Test
public void test() {
//here's your test
//this method will be executed for every element from the data list
}
}
您可以在此处获得有关参数化测试的更多信息:Parameterized tests
https://stackoverflow.com/questions/55248967
复制相似问题