Software testing.docx
- 文档编号:12865207
- 上传时间:2023-06-08
- 格式:DOCX
- 页数:27
- 大小:63.88KB
Software testing.docx
《Software testing.docx》由会员分享,可在线阅读,更多相关《Software testing.docx(27页珍藏版)》请在冰点文库上搜索。
Softwaretesting
Softwaretesting
目录
1.SpecializedUnitTesting1
1.1mockobject(模拟对象)1
1.2refactoring(重构)2
1.3Roles(角色定义)3
2.JUnit4
2.1WhatisJUnit4
2.2ThefeaturesofJUnit4
2.3Whatisthetestcase(测试用例)4
2.4Usingatestfixture(测试夹具)5
2.5JUnitforsingletests5
2.6WhatisaTestSuite(测试套件)6
3.Unit3black-boxtesting6
3.1FUNCTIONALTESTING(功能测试)6
3.2STRESSTESTING(压力测试)7
3.3LOADTESTING(负载测试)7
3.4AD-HOCTESTING(随机测试)7
3.5EXPLORATORYTESTING(探索性测试)8
3.6USABILITYTESTING(可用性测试)8
4.Unit4black-boxtesting8
4.1FACTS–TESTING8
4.2SMOKETESTING(烟雾测试)9
4.3RECOVERYTESTING(恢复性测试)9
4.4VOLUMETESTING(容量测试)9
4.5DOMAINTESTING(域测试)9
4.6SCENARIOTESTING(场景测试)9
4.7 REGRESSIONTESTING(回归测试)10
4.8USERACCEPTANCETESTING(用户验收测试-
测试)10
4.9ALPHATESTING(
测试)11
4.10BETATESTING(
测试)11
5.Unit5white-boxtesting11
5.1Whatiswhite-boxtesting11
5.2Advantagesanddisadvantagesofwhite-boxtesting11
5.3UNITTESTING(单元测试)12
5.4STATIC&DYNAMICANALYSIS(静态动态分析)13
5.5STATEMENTCOVERAGE(语句覆盖)13
5.6BRANCHCOVERAGE(分支覆盖)13
5.7SECURITYTESTING(安全测试)13
5.8MUTATIONTESTING(变异测试)13
6.Unit6BoundaryvalueanalysisandEquivalencepartitioning,explainedwithsimpleexample13
6.1EquivalencePartitioning(等价划分)14
6.2Boundaryvalueanalysis(边界值分析)14
6.3Testinformationflow(测试信息流)14
6.4LoopTesting(循环测试)15
6.5Blackboxtestingtriestofinderrorsinthefollowingcategories15
7.TestingforReal-TimeSystems16
7.1WhatisReal-timeSystems(实时系统)16
7.2AutomatedTestingTools(自动化测试工具)16
7.3CriteriaforCompletionTesting(测试完成标准)17
1.SpecializedUnitTesting
1.1mockobject(模拟对象)
1.Whatismockobject
Amockobjectis,asthenameimplies,asimulationofsomerealobject.InJava,amockobjectwouldtypicallybeanimplementation,withverylimitedfunctionality,ofanInterface.Mockobjectsplayasignificantroleinunittesting,foranumberofreasons.
2.Whywemakemockobject
You'dconsiderusingamockobjectforyourunittestiftherealobjecthasacomplexset-up,usesmanysystemresources(likeCPUpower),ordoesn'tyetexist!
Ifyou'rewritinganapplicationthatusesa database,there'snoneedtowaitfortheteamthatdevelopsthedatabasemodulesbeforeyoustartcodingtherestoftheapplication.Whatyouneedisamockobjectthatbehaveslikethedatabasemodules.Ifyouhaveinterfacesdefinedtothedatabasemodulesyou'rereadytogo.CodingwiththeseinterfacesmightevenbringsomeproblemsintheInterfacedesigntothesurfacebeforethecodingofthedatabasemoduleshasevenstarted!
Unittestinghasbecomewidelyacceptedasa"bestpractice"forsoftwaredevelopment.Whenyouwriteanobject,youmustalsoprovideanautomatedtestclasscontainingmethodsthatputtheobjectthroughitspaces,callingitsvariouspublicmethodswithvariousparametersandmakingsurethatthevaluesreturnedareappropriate.(在编写对象的同时,要提供验证该对象的测试类,该测试类要能够调用该对象的所有公共方法和属性。
)
Whenyou'redealingwithsimpledataorserviceobjects,writingunittestsisstraightforward.However,manyobjectsrelyonotherobjectsorlayersofinfrastructure.Whenitcomestotestingtheseobjects,itisoftenexpensive,impractical,orinefficienttoinstantiate(实例化)thesecollaborators(协作者).
Forexample,tounittestanobjectthatusesadatabase,itmaybeburdensometoinstall,configure,andseedalocalcopyofthedatabase,runyourtests,thentearthelocaldatabasedownagain.Mockobjectsprovideawayoutofthisdilemma.
Amockobjectconformstotheinterfaceoftherealobject,buthasjustenoughcodetofoolthetestedobjectandtrackitsbehavior.Forexample,adatabaseconnectionforaparticularunittestmightrecordthequerywhilealwaysreturningthesamehardwiredresult.
Aslongastheclassbeingtestedbehavesasexpected,itwon'tnoticethedifference,andtheunittestcancheckthattheproperquerywasemitted.
3.Mockinthemiddle
Thecommoncodingstylefortestingwithmockobjectsisto:
ØCreateinstancesofmockobjects
ØSetstateandexpectationsinthemockobjects
ØInvokedomaincodewithmockobjectsasparameters
ØVerifyconsistencyinthemockobjects
1.2refactoring(重构)
Reference:
重构(Refactoring)就是在不改变软件现有功能的基础上,通过调整程序代码改善软件的质量、性能,使其程序的设计模式和架构更趋合理,提高软件的扩展性和维护性.
Cleaningupcode,perhapsbymovingafieldfromoneclasstoanother,pushingsomecodeupordownahierarchy.Mostoftenitistheprocessofsplittingoutrepetitivecodetoturnitintoitsownseparatemethod.
Arefactoringisacodechangethatleavestheoriginalfunctionalityintact(破坏了原来功能的完整性),butchangesthedesignofthecodesothatit'scleaner,moreefficient,andeasiertotest.
Refactoringisnotalwaysdesirable(可取的),nordoesitalwaysresultincodethatiscleaneroreasiertounderstand.Inmanycases,changingthemethodsignaturesothecollaboratorbecomesaparameterwillresultinaconfusing,untestedsnarlofcodeinsidethemethod'soriginalcallers.
Problem:
Theobjectbeingtestedcreatesacollaboratorobject.Thiscollaboratormustbereplacedwithamockobject.(正在被测试的对象创建了一个协作对象,这个协作对象必须被虚拟对象所替换)
classApplication{
...
publicvoidrun(){
Viewv=newView();//创建了一个协作对象,这个对象必须被mock对象所替换
v.display();
...
}
}
Solution:
Extractthecreationcodeintoafactorymethod,overridethisfactorymethodinatestsubclass,andthenmaketheoverriddenmethodreturnamockobjectinstead.Finally,ifpractical,addaunittestthatrequirestheoriginalobject'sfactorymethodtoreturnanobjectofthecorrecttype:
【将创建代码提取到一个工厂方法,在测试子类覆盖这个工厂方法,然后让重写的方法返回一个mock对象。
最后如果可能的话增加一个单元测试让原始对象的工厂方法返回一个对象的正确类型】
classApplication{
...
publicvoidrun(){
Viewv=createView();//将需要创建的mock对象封装到一个方法中
v.display();
...
protectedViewcreateView(){//重写这个方法,让它返回一个mock对象
returnnewView();
}
...
}
classApplicationTestextendsTestCase{//增加一个单元测试,让原始对象的工厂方法返回一个对象的正确类型
MockViewmockView=newMockView();
publicvoidtestApplication
{
Applicationa=newApplication()
{
protectedViewcreateView()
{
returnmockView;
}
};
a.run();
mockView.validate();
}
privateclassMockViewextendsView
{
booleanisDisplayed=false;
publicvoiddisplay(){
isDisplayed=true;
}
publicvoidvalidate(){
assertTrue(isDisplayed);
}
}
}
1.3Roles(角色定义)
Targetobject:
Theobjectbeingtested
Collaboratorobject:
Theobjectcreatedorobtainedbythetarget
Mockobject:
Asubclass(orimplementation)ofthecollaboratorthatfollowsthemockobjectpattern.
Specializationobject:
Asubclassofthetargetthatoverridesthecreationmethodtoreturnamockinsteadofacollaborator.
Inyourtestcode,createamockobjectimplementingthesameinterfaceasthecollaborator.
Inyourtestcode,createaspecializationobjectthatextends(specializes)thetarget.
Inthespecializationobject,overridethecreationmethodtoreturnamockobjectthataccommodatesyourtest.
Optional(可选的):
createaunittesttoassurethattheoriginaltargetobject'sfactorymethodstillreturnsthecorrect,non-mockobject.
2.JUnit
JavahasanexcellenttestingsuitecalledJUnit.Inordertousethis,youwillneedtohaveaJavacompiler,Ant,andJUnitinstalled.
2.1WhatisJUnit
JUnitisasimpleopensourceJavatestingframeworkusedtowriteandrunrepeatableautomatedtests.Itisaninstance(实例)ofthexUnitarchitectureforunittestingframework.Eclipsesupportscreatingtestcasesandrunningtestsuites,soitiseasytouseforyourJavaapplications.[JUNIT-JUnit是一个简单的开源Java测试框架用来编写和运行重复自动化测试。
这是一个作为单元测试框架的xUnit架构的实例。
Eclipse支持创建测试用例,运行测试套件,所以它很容易为你的Java应用程序使用。
]
2.2ThefeaturesofJUnit
ØAssertionsfortestingexpectedresults(为预期的测试结果申明断言)
ØTestfixturesforsharingcommontestdata(Testfixtures工具来共享常用的测试数据)
ØTestsuitesforeasilyorganizingandrunningtests(测试套件来更容易的组织和运行测试)
ØGraphicalandtextualtestrunners(结果图形化、文本化)
2.3Whatisthetestcase(测试用例)
Atestcaseisaclasswhichholdsanumberoftestmethods.Forexampleifyouwanttotestsomemethodsofaclass Book youcreateaclassBookTest whichextendstheJUnit TestCase classandplaceyourtestmethodsinthere.
CreateasubclassofTestCase:
publicclassBookTestextendsTestCase{
//..
}
Writeatestmethodtoassertexpectedresultsontheobjectundertest,thenamingconventionforatestmethodistestXXX()
publicvoidtestCollection(){
Collectioncollection=newArrayList();
assertTrue(collection.isEmpty());
}
Writea suite() methodthatusesreflectiontodynamicallycreateatestsuitecontainingallthe testXXX() methods:
publicstaticTestsuite(){
returnnewTestSuite(BookTest.class);
}
2.4Usingatestfixture(测试夹具)
Atestfixtureisusefulifyouhavetwoormoretestsforacommonsetofobjects.Usingatestfixtureavoidsduplicatingthetestcodenecessarytoinitializeandcleanupthosecommonobjectsforeachtest.[测试夹具是有用的,如果一套共同的对象有两个或更多的测试。
使用一个测试夹具避免为每个必须初始化和清理的常见的对象的测试来复制测试代码。
]
Tocreateatestfixture,definea setUp() methodthatinitializescommonobjectanda tearDown() methodtocleanupthoseobjects.
TheJUnitframeworkautomaticallyinvokesthe setUp() methodbeforeaeachtestisrunandthe tearDown() methodaftereachtestisrun.
publicclassBookTest2extendsTestCase{
privateCollectioncollection;
protectedvoidsetUp(){
collection=newArrayList();
}
protectedvoidtearDown(){
collection.clear();
}
publicvoidtestEmptyCollection(){
assertTrue(collection.isEmpty());
}
}
2.5JUnitforsingletests
JUnitsupportstwoways(staticanddynamic)ofrunningsingletests.
1.Staticway
Instaticwayyouoverridethe runTest() methodinherited(继承)formTestCaseclassandcallthedesiredtestcase.Aconvenientw
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Software testing