软件编程
位置:首页>> 软件编程>> java编程>> Java黑盒测试之nextDate函数测试

Java黑盒测试之nextDate函数测试

作者:Starzkg  发布时间:2023-02-10 20:31:08 

标签:Java,nextDate,黑盒,测试

一、实验目的

(1)掌握应用黑盒测试技术进行测试用例设计。

(2)掌握对测试用例进行优化设计方法。

二、实验内容

日期问题

测试以下程序:该程序有三个输入变量month、day、year(month、day和year均为整数值,并且满足:1≤month≤12、1≤day≤31和1900≤year≤2050),分别作为输入日期的月份、日、年份,通过程序可以输出该输入日期在日历上隔一天的日期。例如,输入为2004 年11月30日,则该程序的输出为2004年12月1日。

(1)划分等价类,按照等价类划分法设计测试用例;

(2)编写nextDate函数;

(3)掌握Junit4的用法,使用Junit4测试nextDate函数。

JUnit4是JUnit框架有史以来的最大改进,其主要目标便是利用Java5的Annotation特性简化测试用例的编写。
掌握Junit4定义的一些常见Annotations:


org.junit.Test
org.junit.Before
org.junit.After
org.junit.BeforeClass
org.junit.AfterClass
org.junit.Ignore
org.junit.runner.RunWith
org.junit.runners.Suite.SuiteClasses
org.junit.runners.Parameterized.Parameters

三、实验要求

(1)根据题目要求编写测试用例;

(2)准备nextDate函数,使用Junit4测试执行测试;

(3)撰写实验报告。

四、实验过程

(1)根据题目要求编写测试用例

 1)划分等价类并编号

输入数据有效等价类无效等价类
(1) 1900到2050内的闰年整数(10) year<1900

(2) 1900到2050内的平年整数(11) year>2050


(12) 其他输入
(3) 1,3,5,7,8,10,12内的整数(13) month<1

(4) 4,6,9,11内的整数(14) 12<month

(5) 2(15) 其他输入
(6) 1~28(16) day<1

(7) 29(17) year为闰年 month=2时,29<day

(8) 30(18) year为非闰年 month=2时,28<day

(9) 31(19) month=1,3,5,7,8,10,12时,31<day


(20) month=4,6,9,11时,30<day


(21) day>31


(22) 其他输入

 2)为有效等价类设计测试用例

序号测试数据期望结果覆盖范围
12016 2 29下一天是2016年3月1日!(1)(5)(7)
22017 1 28下一天是2017年1月29日!(2)(3)(6)
32017 1 31下一天是2017年2月1日!(2)(3)(9)
42017 4 30下一天是2017年5月1日!(2)(4)(8)
52017 12 31下一天是2018年1月1日!(2)(3)(9)

 3)为每一个无效等价类至少设计一个测试用例

序号输入数据期望结果覆盖范围
61899 3 1年的值不在指定范围之内(10)
72051 3 1年的值不在指定范围之内(11)
8205% 3 1无效的输入日期!(12)
91901 -1 1月的值不在指定范围之内(13)
101901 13 1月的值不在指定范围之内(14)
111901 1% 1无效的输入日期!(15)
121901 1 -1日的值不在指定范围之内(16)
132016 2 30日的值不在指定范围之内(17)
142017 2 29日的值不在指定范围之内(18)
152017 3 32日的值不在指定范围之内(19)
162017 4 31日的值不在指定范围之内(20)
172017 4 32日的值不在指定范围之内(21)
182017 4 3%无效的输入日期!(22)

(2)编写nextDate函数,使用Junit4测试执行测试

被测代码


package io.shentuzhigang.demo.blackbox;

import java.util.regex.Pattern;

/**
* @author ShenTuZhiGang
* @version 1.0.0
* @date 2021-05-06 15:43
*/
public class Date {
   private static final Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");

public static String nextDate(String s_year, String s_month, String s_day) {
       //检测是否存在无效字符
       if (!(isInteger(s_year) && isInteger(s_month) && isInteger(s_day))) {
           return "无效的输入日期!";
       }
       //将字符串转为int
       int year = Integer.parseInt(s_year);
       int month = Integer.parseInt(s_month);
       int day = Integer.parseInt((s_day));
       boolean flag = false;
       if (year < 1900 || year > 2050) {
           return ("年的值不在指定范围之内");
       } else if (month > 12 || month < 1) {
           return ("月的值不在指定范围之内");
       } else if (day > 31 || day < 1) {
           return ("日的值不在指定范围之内");
       }

switch (month) {
           case 1:
           case 3:
           case 5:
           case 7:
           case 8:
           case 10:
               if (day == 31) {
                   day = 1;
                   month = month + 1;
               } else {
                   day = day + 1;
               }
               break;
           case 4:
           case 6:
           case 9:
           case 11:
               if (day == 30) {
                   day = 1;
                   month = month + 1;
               } else if (day == 31) {
                   flag = true;
               } else {
                   day = day + 1;
               }
               break;
           case 12:
               if (day == 31) {
                   day = 1;
                   month = 1;
                   year = year + 1;
               } else {
                   day = day + 1;
               }
               break;
           case 2: {
               if (((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) {
                   // 闰年
                   if (day == 29) {
                       day = 1;
                       month = 3;
                   } else if (day < 29) {
                       day = day + 1;
                   } else {
                       flag = true;
                       // day超过29
                   }
               } else {
                   //非闰年
                   if (day == 28) {
                       day = 1;
                       month = 3;
                   } else if (day < 28) {
                       day = day + 1;
                   } else {
                       flag = true;
                   }
               }

}
           break;
           default:
       }

if (year > 2050) {
           return ("年的值不在指定范围之内");
       } else if (flag) {
           return ("日的值不在指定范围之内");
       } else {
           return ("下一天是" + year + "年" + month + "月" + day + "日!");
       }
   }

/**
    * 判断输入字符串是否是整数型
    *
    * @param str
    * @return
    */
   public static boolean isInteger(String str) {
       return pattern.matcher(str).matches();
   }
}

测试代码


package io.shentuzhigang.demo.blackbox;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.util.Arrays;
import java.util.Collection;

/**
* @author ShenTuZhiGang
* @version 1.0.0
* @date 2021-05-06 15:43
*/
@RunWith(Parameterized.class)
public class DateTests {
   private String input1;
   private String input2;
   private String input3;
   private String expected;

@Parameters
   public static Collection<?> prepareData(){
       String [][] object = {
               // 有效等价类
               {"2016","2","29","下一天是2016年3月1日!"},
               {"2017","1","28","下一天是2017年1月29日!"},
               {"2017","1","31","下一天是2017年2月1日!"},
               {"2017","4","30","下一天是2017年5月1日!"},
               // 无效等价类
               {"1899","3","1","年的值不在指定范围之内"},
               {"2051","3","1","年的值不在指定范围之内"},
               {"205%","3","1","无效的输入日期!"},
               {"1901","-1","1","月的值不在指定范围之内"},
               {"1901","13","1","月的值不在指定范围之内"},
               {"1901","1%","1","无效的输入日期!"},
               {"1901","1","-1","日的值不在指定范围之内"},
               {"2016","2","30","日的值不在指定范围之内"},
               {"2017","2","29","日的值不在指定范围之内"},
               {"2017","3","32","日的值不在指定范围之内"},
               {"2017","4","31","日的值不在指定范围之内"},
               {"2017","4","32","日的值不在指定范围之内"},
               {"2017","4","3%","无效的输入日期!"}
       };
       return Arrays.asList(object);
   }
   public DateTests(String input1,String input2,String input3,String expected){
       this.input1 = input1;
       this.input2 = input2;
       this.input3 = input3;
       this.expected = expected;

}
   @Test
   public void testDate(){
       String result = Date.nextDate(input1,input2,input3);
       Assert.assertEquals(expected,result);
   }
}

测试结果

Java黑盒测试之nextDate函数测试

五、缺陷分析

1.用例?发生故障的原因是程序先判断day为29就变为3月1日,而不先判断是否为闰年,于是用例?的输出结果为2007-3-1而不是无效输入日期。

2.用例?发生故障的原因是程序没有先判断接收的三个参数是否在指定范围内,而是先根据month进行数据处理,再判断处理后的参数是否在指定范围内,用例?的参数因为month为3所以直接day+1由0变成1再判断day在指定范围内,所以用例?的输出结果为1998-3-1而不是日的值不在指定范围内。

来源:https://blog.csdn.net/weixin_43272781/article/details/116454710

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com