标签:junit4 测试 教程 教程 单元测试 eclipse
Junit 4 参数化测试 允许通过变化范围的参数值来测试方法。参数擦测试可以通过以下简单的步骤实现:EvenNumberChecker.java 校验输入的数字是否为偶数:
package in.co.javatutorials; /** * @author javatutorials.co.in */ public class EvenNumberChecker { /** * Is input number even. * * @param i input number * @return <code>true</code> if input is even number; otherwise return false */ public boolean isEven(int i) { if (i % 2 == 0) { return true; } else { return false; } } }
package in.co.javatutorials; import static org.junit.Assert.*; 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.Parameters; /** * @author javatutorials.co.in */ // Step 1 @RunWith(Parameterized.class) public class EvenNumberCheckerTest { // Step 2: variables to be used in test method of Step 5 private int inputNumber; private boolean isEven; // Step 3: parameterized constructor public EvenNumberCheckerTest(int inputNumber, boolean isEven) { super(); this.inputNumber = inputNumber; this.isEven = isEven; } // Step 4: data set of variable values @Parameters public static Collection<Object[]> data() { Object[][] data = new Object[][] { { 2, true }, { 5, false }, { 10, false } }; return Arrays.asList(data); } @Test public void test() { System.out.println("inputNumber: " + inputNumber + "; isEven: " + isEven); EvenNumberChecker evenNumberChecker = new EvenNumberChecker(); // Step 5: use variables in test code boolean actualResult = evenNumberChecker.isEven(inputNumber); assertEquals(isEven, actualResult); } }
在Eclipse junit 窗口的输出为:
inputNumber: 2; isEven: true inputNumber: 5; isEven: false inputNumber: 10; isEven: false
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Junit 4 Tutorials(Junit 4 教程) 四、Junit4 参数化测试,布布扣,bubuko.com
Junit 4 Tutorials(Junit 4 教程) 四、Junit4 参数化测试
标签:junit4 测试 教程 教程 单元测试 eclipse
原文地址:http://blog.csdn.net/luanlouis/article/details/37563265