软件测试117301田志阳实验5.docx

上传人:b****5 文档编号:7878479 上传时间:2023-01-26 格式:DOCX 页数:29 大小:1.44MB
下载 相关 举报
软件测试117301田志阳实验5.docx_第1页
第1页 / 共29页
软件测试117301田志阳实验5.docx_第2页
第2页 / 共29页
软件测试117301田志阳实验5.docx_第3页
第3页 / 共29页
软件测试117301田志阳实验5.docx_第4页
第4页 / 共29页
软件测试117301田志阳实验5.docx_第5页
第5页 / 共29页
点击查看更多>>
下载资源
资源描述

软件测试117301田志阳实验5.docx

《软件测试117301田志阳实验5.docx》由会员分享,可在线阅读,更多相关《软件测试117301田志阳实验5.docx(29页珍藏版)》请在冰豆网上搜索。

软件测试117301田志阳实验5.docx

软件测试117301田志阳实验5

实验5JUnit测试框架的使用

一、实验目的

1、掌握Junit测试框架的使用

2、掌握测试用例的编写

二、实验内容

1、学习Junit框架的使用(通过以下两个示例进行学习)

A、Junit使用方法示例1

1)把Junit引入当前项目库中

新建一个Java工程—coolJUnit,打开项目coolJUnit的属性页->选择“JavaBuildPath”子选项->点选“AddLibrary…”按钮->在弹出的“AddLibrary”对话框中选择JUnit(图1),并在下一页中选择版本Junit4后点击“Finish”按钮。

这样便把JUnit引入到当前项目库中了。

图1为项目添加JUnit库

2)新建单元测试代码目录

单元测试代码是不会出现在最终软件产品中的,所以最好为单元测试代码与被测试代码创建单独的目录,并保证测试代码和被测试代码使用相同的包名。

这样既保证了代码的分离,同时还保证了查找的方便。

遵照这条原则,在项目coolJUnit根目录下添加一个新目录testsrc,并把它加入到项目源代码目录中。

(见图2、3)。

图2新建测试代码目录

图3添加测试代码目录

3)在工程中添加类

添加类SampleCaculator,类中有两个方法,分别计算加减法。

编译代码。

packagecn.edu.gzhu;

publicclassSampleCalculator{

//计算两整数之和

publicintadd(intaugend,intaddend){

returnaugend+addend;

}

//计算两整数之差

publicintsubtration(intminuend,intsubtrahend){

returnminuend-subtrahend;

}

}

4)写单元测试代码

为类SampleCalculator添加测试用例。

在资源管理器SampleCalculator.java文件处右击选new>选JunitTestCase(见图4),Sourcefoler选择testsrc目录,点击next,选择要测试的方法,这里把add和subtration方法都选上,最后点finish完成。

图4新建测试用例

Junit自动生成测试类SampleCalculatorTest,修改其中的代码(如下)。

其中assertEquals断言,用来测试预期目标和实际结果是否相等。

assertEquals([Stingmessage],expected,actual)

expected是期望值(通常都是硬编码的),actual是被测试代码实际产生的值,message是一个可选的消息,如果提供的话,将会在发生错误时报告这个消息。

如想用断言来比较浮点数(在Java中是类型为float或者double的数),则需指定一个额外的误差参数。

assertEquals([Stingmessage],expected,actual,tolerance)

其它断言参见课本和参考书介绍。

测试方法需要按照一定的规范书写:

1.测试方法必须使用注解org.junit.Test修饰。

2.测试方法必须使用publicvoid修饰,而且不能带有任何参数。

5)查看运行结果

在测试类上点击右键,在弹出菜单中选择RunAsJUnitTest。

运行结果如下图,绿色的进度条提示我们,测试运行通过了。

图5示例1运行结果

B、Junit使用方法示例2

1)在工程中添加类

类WordDealUtil中的方法wordFormat4DB()实现的功能见文件注释。

packagecn.edu.gzhu;

packagecn.edu.gzhu;

importjava.util.regex.Matcher;

importjava.util.regex.Pattern;

publicclassWordDealUtil{

/**

*将Java对象名称(每个单词的头字母大写)按照

*数据库命名的习惯进行格式化

*格式化后的数据为小写字母,并且使用下划线分割命名单词

*

*例如:

employeeInfo经过格式化之后变为employee_info

*

*@paramnameJava对象名称

*/

publicstaticStringwordFormat4DB(Stringname){

Patternp=Ppile("[A-Z]");

Matcherm=p.matcher(name);

StringBufferstrBuffer=newStringBuffer();

while(m.find()){

//将当前匹配子串替换为指定字符串,

//并且将替换后的子串以及其之前到上次匹配子串之后的字符串段添加到一个StringBuffer对象里

m.appendReplacement(strBuffer,"_"+m.group());

}

//将最后一次匹配工作后剩余的字符串添加到一个StringBuffer对象里

returnm.appendTail(strBuffer).toString().toLowerCase();

}

}

2)写单元测试代码

3)进一步完善测试用例

单元测试的范围要全面,如对边界值、正常值、错误值的测试。

运用所学的测试用例的设计方法,如:

等价类划分法、边界值分析法,对测试用例进行进一步完善。

继续补充一些对特殊情况的测试:

//测试null时的处理情况

@TestpublicvoidwordFormat4DBNull(){

Stringtarget=null;

Stringresult=WordDealUtil.wordFormat4DB(target);

assertNull(result);

}

//测试空字符串的处理情况

@TestpublicvoidwordFormat4DBEmpty(){

Stringtarget="";

Stringresult=WordDealUtil.wordFormat4DB(target);

assertEquals("",result);

}

//测试当首字母大写时的情况

@TestpublicvoidwordFormat4DBegin(){

Stringtarget="EmployeeInfo";

Stringresult=WordDealUtil.wordFormat4DB(target);

assertEquals("employee_info",result);

}

//测试当尾字母为大写时的情况

@TestpublicvoidwordFormat4DBEnd(){

Stringtarget="employeeInfoA";

Stringresult=WordDealUtil.wordFormat4DB(target);

assertEquals("employee_info_a",result);

}

//测试多个相连字母大写时的情况

@TestpublicvoidwordFormat4DBTogether(){

Stringtarget="employeeAInfo";

Stringresult=WordDealUtil.wordFormat4DB(target);

assertEquals("employee_a_info",result);

}

4)查看分析运行结果,修改错误代码

再次运行测试。

JUnit运行界面提示我们有两个测试情况未通过测试(见图6),当首字母大写时得到的处理结果与预期的有偏差,造成测试失败(failure);而当测试对null的处理结果时,则直接抛出了异常——测试错误(error)。

显然,被测试代码中并没有对首字母大写和null这两种特殊情况进行处理,修改如下:

//修改后的方法wordFormat4DB

 

图6示例2运行结果

2、使用Junit框架对类Date和类DateUtil进行单元测试。

只对包含业务逻辑的方法进行测试,包括:

类Date中的

isDayValid(intyear,intmonth,intday)

isMonthValid(intmonth)

isYearValid(intyear)

类DateUtil中的

isLeapYear(intyear)

getDayofYear(Datedate)

Date类

package cn.edu.gzhu;

public class Date{

    public Date(){

   }

    public Date(int year, int month, int day){

       super();

       if (this.isDayValid(year,month,day)&& this.isMonthValid(month)

             && this.isYearValid(year)){

           this.year=year;

           this.month=month;

           this.day=day;

      }else{

           throw new IllegalArgumentException("Pleasecheckyourinput!

");

      }

   }

   /**

    *0

    */

    private int year=-1;

   /**

    *1<=month<=12

    */

    private int month=-1;

   /**

    *1<=day<=31thedayshouldinthescope[1,30]whenthemonthis:

4,

    *6,,9,11thedayshouldinthescope[1,31]whenthemonthis:

1,3,5,

    *7,8,10,12thedayshouldinthescope[1,28]whenthemonthis2and

    *theyearisnotleapyear @see thedayshouldinthescope[1,29]when

    *themonthis2andtheyearisleapyear @see

    */

    private int day=-1;

    public boolean isDayValid(int year, int month, int day){

       if ((month==4||month==6||month==9||month==11)&&(day<=30&&day>=1)) return true;

       if ((month==4||month==6||month==9||month==11)&&(day>30||day<1)) return false;

       if ((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&(day<=31||day>=1)) return true;

       if ((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&(day>31||day<1)) return false;

       if(month==2&&DateUtil.isLeapYear(year)&&(day>=1||day<=29)) return true;

       if(month==2&&DateUtil.isLeapYear(year)&&(day<1||day>29)) return false;

       if(month==2&&!

DateUtil.isLeapYear(year)&&(day>=1&&day<=28)) return true;

       if(month==2&&!

DateUtil.isLeapYear(year)&&(day<1||day>28)) return false;

       return false;

   }

    public boolean isMonthValid(int month){

       return month>=1&&month<=12;

   }

    public boolean isYearValid(int year){

       return year>0;

   }

    public int getYear(){

       return year;

   }

    public void setYear(int year){

       if (this.isYearValid(year)){

           this.year=year;

      }else{

           throw new IllegalArgumentException("Pleasecheckyourinput!

");

      }

   }

    public int getMonth(){

       return month;

   }

    public void setMonth(int month){

       if (this.isMonthValid(month)){

           this.month=month;

      }else{

           throw new IllegalArgumentException("Pleasecheckyourinput!

");

      }

   }

    public int getDay(){

       return day;

   }

    public void setDay(int day){

       if (this.year==-1|| this.month==-1) throw new IllegalStateException("Youshouldsettheyearandmonthbeforeday!

");

       if (this.isDayValid(year,month,day)){

           this.day=day;

      }else{

           throw new IllegalArgumentException("Pleasecheckyourinput!

");

      }

   }

}

 

1)在工程中添加类

2)写单元测试代码

 

3)进一步完善测试用例

 

4)查看分析运行结果,修改错误代码

修改后的运行结果:

 

DateUtil类:

public class DateUtil{

    public DateUtil(){

   }

   /**

    * @param year

    * @return trueifyear%4==0andyear%100!

=0trueifyear%100==0

    *        andyear%400==0falseotherwise

    */

    public static boolean isLeapYear(int year){

       if(year<1)

      {

           return false;

      }

       if (year%4==0&&year%100!

=0)

           return true;

       if (year%100==0&&year%400!

=0)

           return false;

       if (year%100==0&&year%400==0)

           return true;

       return false;

   }

   /**

    *

    * @param d

    * @return dayofyear,改日期是一年中的第几天

    */

    public static int getDayofYear(java.util.Dated){

       int sum=0;

       if (isLeapYear(d.getYear())){

           switch (d.getMonth()){

           case 1:

             sum=0;

              break;

           case 2:

             sum=31;

              break;

           case 3:

             sum=60;

              break;

           case 4:

             sum=91;

              break;

           case 5:

             sum=121;

              break;

           case 6:

             sum=152;

              break;

           case 7:

             sum=182;

              break;

           case 8:

             sum=213;

              break;

           case 9:

             sum=244;

              break;

           case 10:

             sum=274;

              break;

          case 11:

             sum=305;

              break;

           case 12:

             sum=335;

              break;

           default:

             System.out.print("dataerror");

              break;

          }

      } else {

           switch (d.getMonth()){

           case 1:

             sum=0;

              break;

           case 2:

             sum=31;

              break;

           case 3:

             sum=59;

              break;

           case 4:

             sum=90;

              break;

           case 5:

             sum=120;

              break;

           case 6:

             sum=151;

              break;

           case 7:

             sum=181;

              break;

           case 8:

             sum=212;

              break;

           case 9:

             sum=243;

              break;

           case 10:

             sum=273;

              break;

           case 11:

             sum=304;

              break;

           case 12:

             sum=334;

              break;

           default:

             System.out.print("dataerror");

              break;

          }

      }

      sum=sum+d.getDay();

       return sum;

   }

}

1)在工程中添加类

2)写单元测试代码

 

2)进一步完善测试用例

 

4)查看分析运行结果,修改错误代码

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 表格类模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1