




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
软件质量保证与测试
实验指导计算机工程学院
测试环境配置1.settingJunit(1)startEclipseSelectwindows-preferences-java-buildpath-lasspathvariablesClasspathVanabllBStypefiltertextClasspathVanabllBStypefiltertextGeneralAntHelpin我日11/updM白AppearanceBuildPathclasspathvariatUserLibrariesCodeStyleCompilerDebugEditorInstalledJRJEsJUnllPropertiesFilesEdURun/DebugTasksTeamUsageDataCoHectorNew...RESflcinonn:odifiab.g.dsprscatfdj-m.TiptYj.(2)clicknew,thefigureofnewvariableentryisshown.手PreferencesAclasspathvariablecanbeaddedtoaproject'sclas-5-path.ItcanbeugedtadefinethelocationofaJARfilethatisn'tpamafth&workspace.Nonmodifiableclasspathvariablesaresetinternally(forexamplejre_ub.jre_sr.c.andjrejmrootdependonthsJREsetting).Definedcla^spettivariable5:还」REJJB(nonmodillalNe,deprecated)-C:\Prograrr(3)nameJUNIT_LIB
selectfile-选择JUnit插件所对应的JAR文件所在地,在Eclipse的安装目录的plugins目录中r号PreferencestypefiltertextGeneralAntHelpInstall/UpdateJavaAppearanceBuildPathClasspathVariatUserLibrariesCodeStyletypefiltertextGeneralAntHelpInstall/UpdateJavaAppearanceBuildPathClasspathVariatUserLibrariesCodeStyleCompilerDebugEditorInstalledJREJUnitPropertiesFiRun/DebugTasksTeamUsageDataColClasspathVariablesAclasspathvariablecanbeaddedtoaproject'sclasspath.ItcanbeusedtodefinethelocationofaJARfilethatisn'tpartoftheworkspace-Nonmodifiableclasspathvariablesaresetinternally(forexample,JRE_UE,JRE_SRQandJRE_SRCROOTdependontheJREsetting^.Definedclasspathvariables:0」RE」J8(nonmodifiable,deprecated}-C:\Prograrra」RE_SRC(nonmodifiable,deprecated)-(empty)2^JRE_SRCROOT(nonmodifiable,deprecated)-(emf2^JUNITHOME(nonmodifiable,deprecated)-F滋4New...Edit...rrrRemove名称修改日期awtui2OO&/3/315:22.extensions2OO&/3/315;22文低framework2O&&/2/315:22.runn&r2OO&/3/315:22.swingui2OO&/3/315:22交彳犊textui2OO&/3/315:22其中,junit.framework和junit.runner是两个核心包。junit.framework负责整个测试对象的框架junit.runner负责测试驱动Junit的框架又可分为:A、被测试的对象。B、对测试目标进行测试的方法与过程集合,可称为测试用例(TestCase)。C、测试用例的集合,可容纳多个测试用例(TestCase),将其称作测试包(TestSuite)oD、测试结果的描述与记录。(TestResult)。E、每一个测试方法所发生的与预期不一致状况的描述,称其测试失败元素(TestFailure)F、JUnitFramework中的出错异常(AssertionFailedError)。JUnit框架是一个典型的Composite模式:TestSuite可以容纳任何派生自Test的对象;当调用TestSuite对象的run()方法是,会遍历自己容纳的对象,逐个调用它们的run()方法。3.JUnit中常用的接口和类Test接口——运行测试和收集测试结果Test接口使用了Composite设计模式,是单独测试用例(TestCase),聚合测试模式(TestSuite)及测试扩展(TestDecorator)的共同接口。它的publicintcountTestCases()方法,它来统计这次测试有多少个TestCase,另外一个方法就是publicvoidrun(TestResult),TestResult是实例接受测试结果,run方法执行本次测试。TestCase抽象类定义测试中固定方法TestCase是Test接口的抽象实现,(不能被实例化,只能被继承)其构造函数TestCase(stringname)根据输入的测试名称name创建一个测试实例。由于每一个TestCase在创建时都要有一个名称,若某测试失败了,便可识别出是哪个测试失败。TestCase类中包含的setUp()、tearDown()方法。setUp()方法集中初始化测试所需的所有变量和实例,并且在依次调用测试类中的每个测试方法之前再次执行setUp()方法。tearDown()方法则是在每个测试方法之后,释放测试程序方法中引用的变量和实例。开发人员编写测试用例时,只需继承TestCase,来完成run方法即可,然后JUnit获得测试用例,执行它的run方法,把测试结果记录在TestResult之中。Assert静态类——一系列断言方法的集合Assert包含了一组静态的测试方法,用于期望值和实际值比对是否正确,即测试失败,Assert类就会抛出一个AssertionFailedError异常,JUnit测试框架将这种错误归入Failes并加以记录,同时标志为未通过测试。如果该类方法中指定一个String类型的传参则该参数将被做为AssertionFailedError异常的标识信息,告诉测试人员改异常的详细信息。JUnit提供了6大类31组断言方法,包括基础断言、数字断言、字符断言、布尔断言、对象断言。其中assertEquals(Objectexpcted,Objectactual)内部逻辑判断使用equals()方法,这表明断言两个实例的内部哈希值是否相等时,最好使用该方法对相应类实例的值进行比较。而assertSame(Objectexpected,Objectactual)内部逻辑判断使用了Java运算符“==”,这表明该断言判断两个实例是否来自于同一个引用(Reference),最好使用该方法对不同类的实例的值进行比对。asserEquals(Stringmessage,Stringexpected,Stringactual)该方法对两个字符串进行逻辑比对,如果不匹配则显示着两个字符串有差异的地方。ComparisonFailure类提供两个字符串的比对,不匹配则给出详细的差异字符。TestSuite测试包类多个测试的组合TestSuite类负责组装多个TestCases。待测得类中可能包括了对被测类的多个测试,而TestSuit负责收集这些测试,使我们可以在一个测试中,完成全部的对被测类的多个测试。TestSuite类实现了Test接口,且可以包含其它的TestSuites。它可以处理加入Test时的所有抛出的异常。TestSuite处理测试用例有6个规约(否则会被拒绝执行测试)A测试用例必须是公有类(Public)B测试用例必须继承与TestCase类C测试用例的测试方法必须是公有的(Public)D测试用例的测试方法必须被声明为VoidE测试用例中测试方法的前置名词必须是testF测试用例中测试方法无任何传递参数TestResult结果类和其它类与接口TestResult结果类集合了任意测试累加结果,通过TestResult实例传递每个测试的Run()方法。TestResult在执行TestCase时如果失败会异常抛出TestListener接口是个事件监听规约,可供TestRunner类使用。它通知listener的对象相关事件,方法包括测试开始startTest(Testtest),测试结束endTest(Testtest),错误,增加异常addError(Testtest,Throwablet)和增加失败addFailure(Testtest,AssertionFailedErrort)TestFailure失败类是个“失败”状况的收集类,解释每次测试执行过程中出现的异常情况。其toString()方法返回“失败”状况的简要描述4.利用Junit开发一个简单的Java程序File-new-Javaproject,名称为“HelloWorldWithJUnit”建立两个文件夹,分别为src和junittestsrc存放实现主要功能的文件junittest存放测试功能文件与Java-HeItoW□rldWithJUnit/j"unittest/FileEditSourceRefactorNaviga-目也j舞熠PackageExploirerHi&rarcjGt-HellloWorldWrthJUnit[>西srct@Junittest[>gJRESystemLibraryjrefi]|i>兰JUnit3创建测试类选中“HelloWorldWithJUnit”项目中的junittest包文件夹,右键并选择new-other-Junit-JunitTestCase
(3)选择“next”,在NewJunitTestCase中的name中输入“HelloWorldTest”,在package中输入“junittest”,然后单击“finish”。5NewJUnitTestCase|回JUnitTestCaseSelectthenameofthenewJUnittestcase.YouhavetheoptionstospecifyaNewJUnit3testNewJUnit4testSourceiold&r:HelIoWorldWilhJUnit/|unittestBrowie-...Package:[d&fault]Browse-...Name:Supe-rclass:junit.framework.TestCase-Browae-...Whichm&tthodstubswouldyoulik&totreats?setUpBefor&ClassQUtearDownAfterClass.0回s9tUpO[7]tearDoLvnOconstructorDoyouwanttoaddcommenls?[Gonfgurete-rnplat?5anddefaultvaluehereDGene-ratecomments-Browse-...Cla55undertest:
Browse-...(4)现在初步计划被测试文件功能非常简单,只有一个方法ReturnValue,作用是返回“HelloWorld”,所以测试类中有对ReturnValue这个方法进行测试的类。当然,测试要能进行,该测试类必须为主类,存在main方法。HelloWorldTest.java的源代码如下:ITHelloWorldTest.java忘"缶calcustorTest.java]约packagejarLi;importjanit.liainewtul!:.TestCase;piiblicclassHelloWoildTest.ezKbendsTestCdse{pntoliGHelloWoiIdTest(Stringnante}■[super(name);:-ptiblicvoidtestReturnVal'je()-[HelloWoildHelloWorId.();t,rHello^0x11]%'潮,Retur^Valae())r?piiblicstaticvoidmain(String-[]arg's){janit.text'al.Te^tRanner.rizn.(jielloWorldTest..class)选择run-runas-Junittest弹出一个Junit窗口,发现在该窗口中有一个红条,这说明存在错误。(5)创建HelloWorld类建立一个HelloWorld类,并输入以下代码:[J]HelloWorldT&st.javaJH&lloWorld.javapao-ltag^eBrerpni>liqcls-ssjielloHorld-[pnJali□StringRetamValJE(]-[return(p,HelloWorld],r);(6)在HelloWorldTest.java中加入importsrc.*;此时以Junit测试的方式来运行HelloWorldTest.java,出现了含有绿色的窗口,测试成功。
assertEqualspublicstaticvoidassertEquals([java.lang.Stringmessage],java.lang.Objectexpected,java.lang.Objectactual)Assertsthattwoobjectsareequal.Iftheyarenot,anAssertionErroristhrownwiththegivenmessage.Ifexpectedandactualarenull,theyareconsideredequal.Parameters:message-theidentifyingmessagefortheAssertionError(nullokay)expected-expectedvalueactual-actualvalueassertFalsepublicstaticvoidassertFalse(java.lang.Stringmessage,booleancondition)Assertsthataconditionisfalse.Ifitisn'titthrowsanAssertionErrorwiththegivenmessage.Parameters:message-theidentifyingmessagefortheAssertionError(nullokay)condition-conditiontobecheckedassertTruepublicstaticvoidassertTrue(java.lang.Stringmessage,booleancondition)Assertsthataconditionistrue.Ifitisn'titthrowsanAssertionErrorwiththegivenmessage.Parameters:message-theidentifyingmessagefortheAssertionError(nullokay)condition-conditiontobecheckedassertNullpublicstaticvoidassertNull(java.lang.Stringmessage,java.lang.Objectobject)Assertsthatanobjectisnull.Ifitisnot,anAssertionErroristhrownwiththegivenmessage.Parameters:message-theidentifyingmessagefortheAssertionError(nullokay)object-ObjecttocheckornullassertNotNullpublicstaticvoidassertNotNull(java.lang.Stringmessage,java.lang.Objectobject)Assertsthatanobjectisn'tnull.IfitisanAssertionErroristhrownwiththegivenmessage.Parameters:message-theidentifyingmessagefortheAssertionError(nullokay)object-ObjecttocheckornullassertSamepublicstaticvoidassertSame([java.lang.Stringmessage],java.lang.Objectexpected,java.lang.Objectactual)Assertsthattwoobjectsrefertothesameobject.Iftheyarenot,anAssertionErroristhrownwiththegivenmessage.Parameters:message-theidentifyingmessagefortheAssertionError(nullokay)expected-theexpectedobjectactual-theobjecttocomparetoexpectedassertNotSamepublicstaticvoidassertNotSame(java.lang.Stringmessage,java.lang.Objectunexpected,java.lang.Objectactual)Assertsthattwoobjectsdonotrefertothesameobject.Iftheydorefertothesameobject,anAssertionErroristhrownwiththegivenmessage.Parameters:message-theidentifyingmessagefortheAssertionError(nullokay)unexpected-theobjectyoudon'texpectactual-theobjecttocomparetounexpectedfailpublicstaticvoidfail(java.lang.Stringmessage)Failsatestwiththegivenmessage.Parameters:message-theidentifyingmessagefortheAssertionError(nullokay)SeeAlso:AssertionError实验一白盒测试方法一、实验目的1、掌握白盒测试基本技术,并能够应用白盒测试技术设计测试用例2、掌握白盒测试中的逻辑覆盖和路径测试方法二、实验任务使用白盒测试方法为下面的程序设计测试用例(使用逻辑覆盖和路径测试方法):程序要求:10个铅球中有一个假球(比其他铅球的重量要轻),用天平三次称出假球。程序设计思路:第一次使用天平分别称5个球,判断轻的一边有假球;拿出轻的5个球,取出其中4个第二次称,两边分别放2个球:如果两边同重,则剩下的球为假球;若两边不同重,拿出轻的两个球称第三次,轻的为假球。构入ITg•的I习前图1判断假球的程序流程图三、实验要求1、做好实验预习,掌握并熟悉本实验中所使用的测试环境及相应的测试软件2、写出实验报告,内容是:(1)实验目的(2)实验内容实验源代码(或测试脚本)可不写出,但是一定要写出实验中出现的错误,以及解决错误的方法(3)出错信息及处理方法(4)实验结果,包括实验处理结果和设计心得。单元测试覆盖率-使用CloverClover是对单元测试覆盖率进行统计的软件,在Eclipse中使用Clover,首先使用从/download.jspa下载Clover,还需要申请一个license,否则无法使用。目前免费下载的基本都是30天试用版。下载后把Clover压缩包里的两个文件夹,features和plugins,复制到Eclipse安装目录中替换原来的两个文件夹。1.打开Eclipse,从Window->Showview->other,可以看到Clover的信息:导ShrnirVieiJJGerLer:£LAntJ••旧?C10TRtjl--•〔■JCewer:ageIxplorer卜■■■■?...}..3,CVSDebugH&lp,TavaBrowsiFDEFDEEuniiineSY1IT四邳Uze72todi5pLa.ythedescriptiunforaselectedviOKCancelOK2.还需要导入clover.licenseo从Preferences->Clover->License进入:并将申请到的的license粘帖到这里,至此Clover已经安装完成,开始进入单元测试。可以对之前已经做过的project进行覆盖率的计算。也可新建了一个project,结构如下:JVJuititlnAction*•片ReferstlcedLibr:=□:■lee4com.lyoe.s;idriplel>■■■■[f;S:uTiple.javai一一src/jtesi:com.lyoe.z:=diiple.java>S,.JFLESysEmLibraiy[jrel.6.O_UE:••…&lit5••…&srcSample.java和SampleTest.java内容分别是:Java代码packagecom.lyoe.sample;2.publicclassSample(publicIntegeradd(Integera,Integerb)(if(a==nullIIb==null)(returnnull;TOC\o"1-5"\h\z}Integersum=newInteger(Value()+Value());returnsum;}}Java代码packagecom.lyoe.sample;2.importcom.lyoe.sample.Sample;importjunit.framework.TestCase;5.publicclassSampleTestextendsTestCase(7.protectedvoidsetUp()throwsException(super.setUp();}11.publicvoidtestAdd()(Samplesample=newSample();Integera=newInteger(1);Integerb=newInteger(2);Integerc=sample.add(a,b);assertNotNull(c);TOC\o"1-5"\h\z}19.protectedvoidtearDown()throwsException(super.tearDown();}23.}右击JunitInAction工程,选择Properties->Clover。上面有一个EnableCloverinthisproject,勾上,apply->OK.曷FrotlemsShow:AllclassesElerngnt%TOTALCoverageAverageMet...Corripl&xityjJunitlriXctiungcom.lyot:.E:=iJTiplF::•…曷FrotlemsShow:AllclassesElerngnt%TOTALCoverageAverageMet...Corripl&xityjJunitlriXctiungcom.lyot:.E:=iJTiplF::•…』Sample,ja^a[>■■■■国S:dJiipleTe5t.java0.0%0.0%0.0%0.0%1.06.06.03.03.0■时幻I■啊函可剧囹□Togglewkn+ht:!-CloveririEtriojTierLti+iijrLisp^rfurTit!ddm-ingTipil=iti.Mlfirthwproj点击红框内的按钮,会弹出重新构建工程的提示,点击”是”即可。To在CoverageExplorer界面下,我们会看到:@Javadoc|^jDeclii_atiunE■jCoverageExplorerFrublene@Javadoc右击SampleTest.java,选择Runas->JUnitTest,结果如下:Frublene@JavadocElemerLt▼%TOTALCoverageA.verageMei...Complexity■t日JuniIIilActionJ-£0com-lyg.s:diiiple1:■88.2%1.56.06.0b―Jj5:=jTiple.java71.4%3.03.0阳场国|W的p侦T白gt.j:.11100.0%1.03.0Show:Allclasses|飓,DslaratiDn?(■JCoverageExplorer以可以看到Sample.java的覆盖率为71.4%.Clover还可以生成三种测试报告:PDF/HTML/XMLO按钮可以在CoverageExplorer那排的选项按钮里找到(runnewreport)o
实验二黑盒测试一、实验目的1、掌握黑盒测试基本技术,并能够应用黑盒测试方法设计测试用例2、熟悉黑盒测试中的等价类测试方法和边界值测试方法。二、实验任务使用等价类方法设计下面程序的设计用例:输入三个整数作为边,分别满足一般三角形、等腰三角形和等边三角形。三、实验内容和步骤1、使用等价类划分法设计下面的测试用例:(1)源程序packagepl;importjava.io.*;publicclassJTriangle(privateintb,c,a;privatestaticintx1,x2,x3;privatestaticStrings1,s2,s3;publicvoidsetA(inta)(this.a=a;}publicvoidsetB(intb)(this.b=b;}publicvoidsetC(intc)(this.c=c;}publicbooleanIsTriangle()(if(a+b>c&&a+c>b&&b+c>a&&a!=0&&b!=0&&c!=0)returntrue;else,re'””false;publicJTriangle(int_a,int_b,int_c)(setA(a);setB(_b),setC(_c);publicStringJudgeTriangle()if(IsTriangle())'一return〃等边三角形el—5return〃等腰三角形〃;else}re'""I*"'elsereturn〃不能组成三角形〃;publicbooleanISNumble(Strings)if(s.length()==0){returnfalse;else(charch[]=s.toCharArray();:or(""…出)if(ch[i]>='0'&&ch[i]<='9')continue;elsereturnfalse;}returntrue;}}publicvoidSetTriangle()(try(Buffe
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论