zoukankan      html  css  js  c++  java
  • 测试 | 单元测试工具 | 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

  • 相关阅读:
    基于android的远程视频监控系统——实现Camera预览
    安卓巴士移动开发者周刊第十三期
    【图片处理工具】Android游戏开发的好帮手
    [No0000112]ComputerInfo,C#获取计算机信息(cpu使用率,内存占用率,硬盘,网络信息)
    【笔记】Eclipse and Java for Total Beginners—013
    【笔记】Eclipse and Java for Total Beginners—003
    【笔记】Stanford OpenCourse—CS106A:Programming Methodology—002
    【笔记】Stanford OpenCourse—CS106A:Programming Methodology—005
    【笔记】Eclipse and Java for Total Beginners—008
    【笔记】Eclipse and Java for Total Beginners—004
  • 原文地址:https://www.cnblogs.com/jj81/p/9856791.html
Copyright © 2011-2022 走看看