测试 | 单元测试工具 | JUnit | 参数化
被测试类:
package project; public class MyCalendar2 { public int getNumberOfDaysInMonth(int year, int month) { if (month == 1 || month == 3 || month == 6 || month == 7 || month == 8 || month == 10 ) return 31; if (month == 4 || month == 5 || month == 9 || month == 11) return 30; if (month == 2) return 28; return 0; // If month is incorrect } }
设计测试数据:
输入 |
预计输出 |
2018 1 |
31 |
2018 2 |
28 |
2018 3 |
31 |
2018 4 |
30 |
2018 5 |
31 |
2018 6 |
30 |
2018 7 |
31 |
2018 8 |
31 |
2018 9 |
30 |
2018 10 |
31 |
2018 11 |
30 |
2018 12 |
31 |
2018 13 |
0 |
2008 2 |
29 |
一般测试:
package project; import static org.junit.Assert.*; import org.junit.Test; /** * * @author weiTangzhao * @Time * */ public class MyCalendar2Test { MyCalendar2 m = new MyCalendar2(); @Test public void testGetNumberOfDaysInMonth1(){ int days = m.getNumberOfDaysInMonth(2018, 1); assertEquals(31, days); } @Test public void testGetNumberOfDaysInMonth2(){ int days = m.getNumberOfDaysInMonth(2008, 2); assertEquals(29, days); } @Test public void testGetNumberOfDaysInMonth21(){ int days = m.getNumberOfDaysInMonth(2018, 2); assertEquals(28, days); } @Test public void testGetNumberOfDaysInMonth3(){ int days = m.getNumberOfDaysInMonth(2018, 3); assertEquals(31, days); } @Test public void testGetNumberOfDaysInMonth4(){ int days = m.getNumberOfDaysInMonth(2008, 4); assertEquals(30, days); } @Test public void testGetNumberOfDaysInMonth5(){ int days = m.getNumberOfDaysInMonth(2018, 5); assertEquals(31, days); } @Test public void testGetNumberOfDaysInMonth6(){ int days = m.getNumberOfDaysInMonth(2018, 6); assertEquals(30, days); } @Test public void testGetNumberOfDaysInMonth7(){ int days = m.getNumberOfDaysInMonth(2018, 7); assertEquals(31, days); } @Test public void testGetNumberOfDaysInMonth8(){ int days = m.getNumberOfDaysInMonth(2018,8); assertEquals(31, days); } @Test public void testGetNumberOfDaysInMonth10(){ int days = m.getNumberOfDaysInMonth(2018, 10); assertEquals(31, days); } @Test public void testGetNumberOfDaysInMonth9(){ int days = m.getNumberOfDaysInMonth(2018, 9); assertEquals(30, days); } @Test public void testGetNumberOfDaysInMonth11(){ int days = m.getNumberOfDaysInMonth(2018, 11); assertEquals(30, days); } @Test public void testGetNumberOfDaysInMonth12(){ int days = m.getNumberOfDaysInMonth(2018, 12); assertEquals(31, days); } @Test public void testGetNumberOfDaysInMonth13(){ int days = m.getNumberOfDaysInMonth(2018, 13); assertEquals(0, days); } }
参数化测试:
package project; import static org.junit.Assert.*; import java.util.Arrays; import java.util.Collection; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class MyCalendar2Test2 { private int input1; private int input2; private int expected; /** * 准备数据。数据的准备需要在一个方法中进行,该方法需要满足一定的要求: 1)该方法必须由Parameters注解修饰 2)该方法必须为public static的 3)该方法必须返回Collection类型 4)该方法的名字不做要求 5)该方法没有参数 * @return */ @Parameters @SuppressWarnings("unchecked") public static Collection prepareData(){ Object[][] object = {{2018,1,31},{2018,2,28},{2018,3,31},{2018,4,30},{2018,5,31},{2018,6,30}, {2018,7,31},{2018,8,31},{2018,9,30},{2018,10,31},{2018,11,30},{2018,12,31},{2018,13,0},{2008,2,29}}; return Arrays.asList(object); } public MyCalendar2Test2(int input1,int input2,int expected){ this.input1 = input1; this.input2 = input2; this.expected = expected; } @Test public void testGetNumberOfDaysInMonth(){ MyCalendar2 m = new MyCalendar2(); int result = m.getNumberOfDaysInMonth(input1,input2); Assert.assertEquals(expected,result); } }
参考博客:
https://www.cnblogs.com/byron0918/p/4801152.html