Parameterized (参数化)的测试运行器允许你使用不同的参数多次运行同一个侧试。
运行此测试的必备条件:
1.必须使用@RunWith(Parameterized.class)
2.必须声明测试用到的变量
3.提供一个@Parameterized注解的方法
例如:
public class Calculator {
public double add(double i,double j){
return i+j;
}
}
package com.laoxu.gamedog.util;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.*;
/**
* 参数化测试
*
* @author xusucheng
* @create 2018-12-16
**/
@RunWith(Parameterized.class)
public class ParameterizedTest {
private double expected;
private double p1;
private double p2;
@Parameterized.Parameters
public static Collection<Integer[]> getTestParamters() {
return Arrays.asList(new Integer[][]{
{2, 1, 1}, {3, 2, 1}, {4, 3, 1}
});
}
public ParameterizedTest(double expected, double p1, double p2) {
this.expected = expected;
this.p1 = p1;
this.p2 = p2;
}
@Test
public void testAdd() {
Calculator calculator = new Calculator();
assertEquals(expected, calculator.add(p1, p2), 0);
}
}
运行结果:
Addition with parameters : 1 and 2
Addition with parameters : 2 and 3
Addition with parameters : 3 and 4
Addition with parameters : 4 and 5
继续阅读与本文标签相同的文章
上一篇 :
智能合约编程资料合集
下一篇 :
信息论与逻辑回归代价函数
-
史上最详细Java内存区域讲解
2026-05-19栏目: 教程
-
Hystrix Dashboard:断路器执行监控
2026-05-19栏目: 教程
-
AGV为什么要选择视觉导航
2026-05-19栏目: 教程
-
《Absolute Java 中文版》| 每日读本书
2026-05-19栏目: 教程
-
汽车圈微信聊天记录大曝光
2026-05-19栏目: 教程
