、目的和要求
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:
三、测试用例的编写
等价类表:
输入数据 | 有效等价类 | 无效等价类 |
year | 1:[1900,2050]范围内的世纪闰年 2:[1900,2050]范围内的普通闰年 3:[1900,2050]范围内的平年 | 4:(-∞,1900)的整数 5:(2050,+∞)的整数 6:其他输入(如字符、浮点数等) |
month | 7:2 8:4,6,9,11 9:1,3,5,7,8,10 10:12 | 11:(- ∞,1)的整数 12:(12,+∞)的整数 13:其他输入(如字符、浮点数等) |
day | 14:[1,27]的整数 15:28 16:29 17:30 18:31 | 19:(-∞,1)的整数 20:(31,+ ∞)的整数 21:其它输入(如字符、浮点数等) 22:29 23:30 24:31 |
测试用例:
序号 | 测试用例 描述 | 输入参数 | 期望输出 | 覆盖范围 | ||||
year | month | day | year | month | day |
| ||
1 | 有效等价类 | 2004 | 12 | 25 | 2004 | 12 | 26 | 2,10,14 |
2 | 有效等价类 | 2001 | 2 | 28 | 2001 | 3 | 1 | 3,7,15 |
3 | 有效等价类 | 2000 | 2 | 29 | 2000 | 3 | 1 | 1,7,16 |
4 | 有效等价类 | 2001 | 4 | 30 | 2001 | 5 | 1 | 3,8,17 |
5 | 有效等价类 | 2001 | 5 | 31 | 2001 | 6 | 1 | 3,9,18 |
6 | 无效等价类 | 1899 | 6 | 1 |
|
|
| 4 |
7 | 无效等价类 | 2051 | 6 | 1 |
|
|
| 5 |
8 | 无效等价类 | 1999 | 0 | 1 |
|
|
| 11 |
9 | 无效等价类 | 1999 | 13 | 1 |
|
|
| 12 |
10 | 无效等价类 | 1999 | 1 | 0 |
|
|
| 19 |
11 | 无效等价类 | 1999 | 1 | 32 |
|
|
| 20 |
12 | 无效等价类 | 1999 | 1 | a |
|
|
| 21 |
13 | 无效等价类 | 2001 | 2 | 29 |
|
|
| 22 |
14 | 无效等价类 | 2000 | 2 | 30 |
|
|
| 23 |
15 | 无效等价类 | 2001 | 4 | 31 |
|
|
| 24 |
16 | 无效等价类 | a | 6 | 1 |
|
|
| 6 |
17 | 无效等价类 | 1999 | a | 1 |
|
|
| 13 |
源代码:
项目结构
******************Datatime.java****************
package heihe;
class Datatime {
private static int[] bigmonth = new int[]{1,3,5,7,8,10,12};
private static int[] smallmonth = new int[]{4,6,9,11};
private int year;
private int month;
private int day;
public String nextDate(String datastr) {
String[] numbers = datastr.split("/");
if(numbers.length!=3) {
return "输入非法";
}
try{
this.year = Integer.parseInt(numbers[0]);
}catch (NumberFormatException e){
return "输入非法";
}
try{
this.month = Integer.parseInt(numbers[1]);
}catch (NumberFormatException e){
return "输入非法";
}
try{
this.day = Integer.parseInt(numbers[2]);
}catch (NumberFormatException e){
return "输入非法";
}
String updateResult = checkdate();
if(updateResult.equals("success")) {
int daynumber=getdaynumber();
int nyear = 0,nmonth=0,nday=0;
if(day<daynumber) {
nyear=year;
nmonth=month;
nday=day+1;
}
else if(month<12) {
nyear=year;
nmonth=month+1;
nday=1;
}
else if(month==12) {
nyear=year+1;
nmonth=1;
nday=1;
}
return nyear+"/"+nmonth+"/"+nday;
}
else {
return updateResult;
}
}
private String checkdate() {
if(year<=2050&&year>=1900) {
int daynumber=getdaynumber();
if(daynumber==0) {
return "无此日";
}
if(day>0&&day<=daynumber) {
return "success";
}
else {
return "无此日";
}
}
return "无此日";
}
private int getdaynumber(){
for(int i=0;i<7;i++) {
if(bigmonth[i]==month) {
return 31;
}
}
for(int i=0;i<4;i++) {
if(smallmonth[i]==month) {
return 30;
}
}
if(month==2) {
if(year%4!=0||(year%4==0&&year%100!=0)) {
return 28;
}
else {
return 29;
}
}
return 0;
}
}
******************DatatimeTest.java****************
package heihe;
class Datatime {
private static int[] bigmonth = new int[]{1,3,5,7,8,10,12};
private static int[] smallmonth = new int[]{4,6,9,11};
private int year;
private int month;
private int day;
public String nextDate(String datastr) {
String[] numbers = datastr.split("/");
if(numbers.length!=3) {
return "输入非法";
}
try{
this.year = Integer.parseInt(numbers[0]);
}catch (NumberFormatException e){
return "输入非法";
}
try{
this.month = Integer.parseInt(numbers[1]);
}catch (NumberFormatException e){
return "输入非法";
}
try{
this.day = Integer.parseInt(numbers[2]);
}catch (NumberFormatException e){
return "输入非法";
}
String updateResult = checkdate();
if(updateResult.equals("success")) {
int daynumber=getdaynumber();
int nyear = 0,nmonth=0,nday=0;
if(day<daynumber) {
nyear=year;
nmonth=month;
nday=day+1;
}
else if(month<12) {
nyear=year;
nmonth=month+1;
nday=1;
}
else if(month==12) {
nyear=year+1;
nmonth=1;
nday=1;
}
return nyear+"/"+nmonth+"/"+nday;
}
else {
return updateResult;
}
}
private String checkdate() {
if(year<=2050&&year>=1900) {
int daynumber=getdaynumber();
if(daynumber==0) {
return "无此日";
}
if(day>0&&day<=daynumber) {
return "success";
}
else {
return "无此日";
}
}
return "无此日";
}
private int getdaynumber(){
for(int i=0;i<7;i++) {
if(bigmonth[i]==month) {
return 31;
}
}
for(int i=0;i<4;i++) {
if(smallmonth[i]==month) {
return 30;
}
}
if(month==2) {
if(year%4!=0||(year%4==0&&year%100!=0)) {
return 28;
}
else {
return 29;
}
}
return 0;
}
}
运行结果: