被测代码地址:http://www.cnblogs.com/linpanhuang/p/6611626.html
掌握基于覆盖理论与基本路径的基本白盒测试方法和实践
运用逻辑覆盖测试的覆盖准则设计被测程序的测试用例,并运行测试用例检查程序的正确与否,给出程序缺陷小结。
根据各位同学自己的被测程序,分别作出各类白盒测试技术的用例设计和相应的Junit脚本。
所有的覆盖的技术:语句覆盖、判定覆盖、条件覆盖、判定/条件覆盖、组合覆盖、路径覆盖,基本路径测试方法。
包括的内容有:
1) 被测原代码
package test01;
import java.util.Scanner;
public class aaa {
static int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};//静态数组存储月份的天数
public static void main(String[] args) {
// TODO Auto-generated method stub
int y=0 ,m=0,d=0;
System.out.println("请输入年 月 日(用空格隔开),-1退出:");
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
while(true){
y=scan.nextInt();
if(y == -1)System.exit(0);//-1退出程序
m=scan.nextInt();
d=scan.nextInt();
System.out.println(nextDate(y,m,d));
}
}
public static String nextDate(int year,int month,int day)//nextDate方法
{
String newDate = null;//用来接收日期
checkLeapYear(year);//如果是闰年则将m[2]置为29
switch(checkInput(year,month,day))//判断输入的的合法性属于哪种类型,如果输入合法再进行相关操作
{
case 1: return newDate = "月份超出范围";
case 2: return newDate = "日期超出范围";
case 3: return newDate = "年份超出范围";
default:
if(month==12 && day==31)//判断是否为最后一个月的最后一天,如果 是则年份加1,为1月1日
{
year++;
month = 1;
day = 1;
}
else if(day < m[month]) day++;//如果日期的天数小于本月天数执行day++;否则表示为当月的最后一天执行month++,day=1;
else
{
month++;
day =1;
}
newDate=year+"年"+month+"月"+day+"日";
m[2] = 28;//重置二月的天数
return newDate;
}
}
public static void checkLeapYear(int year)//如果是闰年则将m[2]置为29
{
if(year%4 == 0 && year%100 != 0)
m[2] = 29;//闰年2月29天
if(year%400 == 0)
m[2] = 29;
}
public static int checkInput(int year,int month, int day)//判断输入是否合法,并分成不同类别
{
if(month<1 || month>12)
return 1;
if(day<1 || day>m[month])
return 2;
if(year<1992 || year>2050)
return 3;
return 0;
}
}
2)依据覆盖技术,测试用例列表:
| 覆盖方式 | 序号 | 输入数据 | 期待结果 | 实际结果结果 | 通过 |
| 语句覆盖、判定/条件覆盖、路径覆盖 | 1 | y=2015 m=12 d=30 | 2015年12月31日 | 2015年12月31日 | √ |
| 语句覆盖、判定/条件覆盖、路径覆盖 | 2 | y=2016 m=1 d=1 | 2016年1月2日 | 2016年1月2日 | √ |
| 语句覆盖、判定/条件覆盖、路径覆盖 | 3 | y=2015 m=2 d=31 | 2016年1月1日 | 2016年1月1日 | √ |
| 语句覆盖、判定/条件覆盖、路径覆盖 | 4 | y=2016 m=2 d=28 | 2016年2月29日 | 2016年2月29日 | √ |
| 语句覆盖、判定/条件覆盖、路径覆盖 | 5 | y=2016 m=2 d=29 | 2016年3月1日 | 2016年3月1日 | √ |
| 语句覆盖、判定/条件覆盖、路径覆盖 | 6 | y=2016 m=2 d=32 | 日期超出范围 | 日期超出范围 | √ |
| 语句覆盖、判定/条件覆盖、路径覆盖 | 7 | -1 | 程序退出 | 程序退出 | √ |
| 语句覆盖、判定/条件覆盖、路径覆盖、组合覆盖 | 8 | y=1912 m=3 d=16 | 1912年3月17日 | 年份超出范围 | × |
| 语句覆盖、判定/条件覆盖、路径覆盖、组合覆盖 | 9 | y=2018 m=0 d=23 | 月份超出范围 | 月份超出范围 | √ |
| 组合覆盖 | 10 | y=2051 m=3 d=16 | 年份超出范围 | 年份超出范围 | √ |
| 组合覆盖 | 11 | y=2015 m=4 d=30 | 2015年5月1日 | 2015年5月1日 | √ |
| 组合覆盖 | 12 | y=2015 m=8 d=31 | 2015年9月1日 | 2015年9月1日 | √ |
| 组合覆盖 | 13 | y=2018 m=1 d=0 | 日期超出范围 | 日期超出范围 | √ |
| 组合覆盖 | 14 | y=2018 m=1 d=-1 | 日期超出范围 | 日期超出范围 | √ |
| 组合覆盖 | 15 | y=2015 m=13 d=32 | 月份超出范围 | 月份超出范围 | √ |
3)相应Junit测试脚本、执行结果
package test01;
import static org.junit.Assert.*;
import org.junit.Test;
public class unit_est {
@Test
public void test1() {
assertEquals("2015年12月31日",aaa.nextDate(2015, 12, 30));
}
@Test
public void test2() {
assertEquals("2016年1月2日",aaa.nextDate(2016, 1, 1));
}
@Test
public void test3() {
assertEquals("2017年1月1日",aaa.nextDate(2016, 12, 31));
}
@Test
public void test4() {
assertEquals("2016年2月29日",aaa.nextDate(2016, 2, 28));
}
@Test
public void test5() {
assertEquals("2016年3月1日",aaa.nextDate(2016, 2, 29));
}
@Test
public void test6() {
assertEquals("日期超出范围",aaa.nextDate(2016, 2, 32));
}
@Test
public void test8() {
assertEquals("1912年3月17日",aaa.nextDate(1912, 3, 16));
}
@Test
public void test9() {
assertEquals("月份超出范围",aaa.nextDate(2018, 0, 23));
}
@Test
public void test10() {
assertEquals("年份超出范围",aaa.nextDate(2051, 3, 16));
}
@Test
public void test11() {
assertEquals("2015年5月1日",aaa.nextDate(2015, 4, 30));
}
@Test
public void test12() {
assertEquals("2015年9月1日",aaa.nextDate(2015, 8, 31));
}
@Test
public void test13() {
assertEquals("日期超出范围",aaa.nextDate(2018, 1, 0));
}
@Test
public void test14() {
assertEquals("日期超出范围",aaa.nextDate(2018, 1, -1));
}
@Test
public void test15() {
assertEquals("月份超出范围",aaa.nextDate(2015, 13, 32));
}
}
4)给出测试参数化和打包测试的脚本,并生成执行结果

原文:http://www.cnblogs.com/mrlry/p/6697100.html