实验二+016+兰东华
被测试代码链接:http://www.cnblogs.com/Tichocan/p/6610631.html
一、实验目的
掌握覆盖测试的基本方法和实践
二、实验要求
运用逻辑覆盖测试的覆盖准则设计被测程序的测试用例,并运行测试用例检查程序的正确与否,给出程序缺陷小结。
三、实验内容
(1)设计某程序的路径覆盖测试用例,如路径覆盖、条件覆盖、分支覆盖、循环覆盖等
被测试代码:package examOne;
import java.util.Scanner;
public class Date {
String ia, ib, ic;
int y, m, d;
private boolean Read(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份");
ia = sc.nextLine();
if( "-1".equals(ia) ){
sc.close();
return false;
}
System.out.println("请输入月份");
ib = sc.nextLine();
System.out.println("请输入日期");
ic = sc.nextLine();
return true;
}
private boolean isInputInteger(){
try{
y = Integer.valueOf(ia).intValue();
m = Integer.valueOf(ib).intValue();
d = Integer.valueOf(ic).intValue();
return true;
}
catch( Exception e){
System.out.println("输入不符合要求,请输入三个整数");
return false;
}
}
private boolean isInRange(){
if( m < 1 || m > 12 ){
System.out.println("月份超出范围");
return false;
}
if( d < 1 || d > 31 ){
System.out.println("日期超出范围");
return false;
}
if( y < 1912 || y > 2050 ){
System.out.println("年份超出范围");
return false;
}
return true;
}
private int isLeapYear(){
if( ( y % 4 == 0 && y % 100 != 0 ) || ( y % 400 == 0 ) ){
return 1;
}
return 0;
}
private boolean EndOfMouth( int flg ){
if( ( m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m ==12 ) && d == 31 ){
return true;
}
if( m == 2 && d == (28 + flg) ){
return true;
}
if( ( m == 4 || m == 6 || m == 9 || m == 11 ) && d == 30 ){
return true;
}
return false;
}
private String NextDate(){
int tag = 0;
if( EndOfMouth( isLeapYear() ) ){
tag = 1;
}
if( m == 12 && tag == 1 ){
y++;
m = 1;
d = 1;
}
else if( tag == 1 ){
m++;
d = 1;
}
else{
d++;
}
String ret = y + "年" + m + "月" + d + "日";
return ret;
}
public static void main( String args[]){
Date solve = new Date();
while( solve.Read() ){
if( solve.isInputInteger() && solve.isInRange() ){
System.out.println( solve.NextDate() );
}
}
}
}
(2)测试用例图:
(a)语句覆盖:
A-D-E-F-H-I和A-B-C-D-E-F-G-H-I
(b)测试用例
用例ID |
输入的数值 |
执行路径 |
预期输出结果 |
||
y |
m |
d |
|||
0 |
-1 |
|
|
AB |
退出 |
1 |
1.1 |
1.1 |
1.1 |
AC |
输入不符合要求,请输入三个整数 |
2 |
2002 |
13 |
21 |
ADP |
月份超出范围 |
3 |
2002 |
12 |
33 |
AEP |
日期超出范围 |
4 |
2060 |
12 |
30 |
AFP |
年份超出范围 |
5 |
2012 |
12 |
31 |
AGHJP |
2013年1月1日 |
6 |
2012 |
2 |
29 |
AGHKP |
2012年3月1日 |
7 |
2012 |
2 |
2 |
AGHLP |
2012年2月3日 |
8 |
2013 |
12 |
30 |
AGIMP |
2014年1月1日 |
9 |
2013 |
2 |
28 |
AGINP |
2014年3月1日 |
10 |
2013 |
2 |
2 |
AGIOP |
2013年2月3日 |
根据节点A展开:
编号 |
m<1 |
M>12 |
覆盖路径 |
11 |
T |
F |
QD |
12 |
F |
T |
QD |
13 |
T |
T |
QD |
编号 |
d<1 |
d>31 |
覆盖路径 |
14 |
T |
F |
QD |
15 |
F |
T |
QD |
16 |
T |
T |
QD |
编号 |
y<1912 |
y>2050 |
覆盖路径 |
17 |
T |
F |
QD |
18 |
F |
T |
QD |
19 |
T |
T |
QD |
(3)相应的Junit测试脚本、执行结果:
import static org.junit.Assert.*;
import org.junit.Test;
public class test02Test {
@Test
public void test1() {
assertEquals("输入不符合要求,请输入三个整数",solve.nextDate(1.1, 1.1, 1.1));
}
@Test
public void test2() {
assertEquals("月份超出范围",solve.nextDate(2002, 13, 21));
}
@Test
public void test3() {
assertEquals("日期超出范围",solve.nextDate(2002, 12, 33));
}
@Test
public void test4() {
assertEquals("2013年1月1日",solve.nextDate(2012, 12, 31));
}
@Test
public void test5() {
assertEquals("2012年3月1日",solve.nextDate(2012, 2, 29));
}
@Test
public void test6() {
assertEquals("2012年2月3日",solve.nextDate(2012, 2, 2));
}
@Test
public void test7() {
assertEquals("2014年1月1日",solve.nextDate(2013, 12, 30));
}
@Test
public void test8() {
assertEquals("2014年3月1日",solve.nextDate(2013, 2, 28));
}
@Test
public void test9() {
assertEquals("2013年2月3日",solve.nextDate(2013, 2, 2));
}
}
(4)测试小结:
原本被测试程序是用C++代码编写的,后面由于需要用java测试,程序员便把代码转换成了java语言。该程序逻辑清楚,不过太过简洁,相对难以理解。测试样例均通过,没有任何问题。