![unity单元测试-使用Unity测试工具以光速进行单元测试_第1页](http://file4.renrendoc.com/view/5d36db917bb06de81632680270700fc8/5d36db917bb06de81632680270700fc81.gif)
![unity单元测试-使用Unity测试工具以光速进行单元测试_第2页](http://file4.renrendoc.com/view/5d36db917bb06de81632680270700fc8/5d36db917bb06de81632680270700fc82.gif)
![unity单元测试-使用Unity测试工具以光速进行单元测试_第3页](http://file4.renrendoc.com/view/5d36db917bb06de81632680270700fc8/5d36db917bb06de81632680270700fc83.gif)
![unity单元测试-使用Unity测试工具以光速进行单元测试_第4页](http://file4.renrendoc.com/view/5d36db917bb06de81632680270700fc8/5d36db917bb06de81632680270700fc84.gif)
![unity单元测试-使用Unity测试工具以光速进行单元测试_第5页](http://file4.renrendoc.com/view/5d36db917bb06de81632680270700fc8/5d36db917bb06de81632680270700fc85.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
unity单元测试_使⽤Unity测试⼯具以光速进⾏单元测试unity单元测试It’stimetotellalittlebitmoreaboutNSubstitutelibrarythatshipswithourandpatternsofit’seffectiveusage.现在该介绍⼀下我们附带的NSubstitute库及其有效使⽤模式的更多信息。Eachsoftwaresystemconsistsofunitsoffunctionality.Inobjectorientedlanguages,theminimalunitoffunctionalityisamethod.Thesemethodsusuallydependonothermethodsandclasses.Ifyouhavetotestamethod,somechallengeswillarise.每个软件系统都包含功能单元。在⾯向对象的语⾔中,功能的最⼩单位是⼀种⽅法。这些⽅法通常取决于其他⽅法和类。如果您必须测试⼀种⽅法,将会遇到⼀些挑战。Unittestisabouttestingunitoffunctionalityinanisolatedenvironment.Isolatedmeanswithalldependenciesmockedup.Whichmeansthattestworksinatestspecificenvironmentwhereonlyoneexecutionpathispossible.单元测试是关于在隔离的环境中测试功能单元。孤⽴的意味着模拟了所有依赖关系。这意味着测试只能在⼀个特定的测试环境中⼯作,该环境只能有⼀个执⾏路径。Testdoublessubstituterealdependenciesofunitundertest,formingtestspecificenvironmentandmakingunittestsfastandrobust.测试将替代被测单元的实际依赖性加倍,从⽽形成了特定于测试的环境,并使单元测试变得快速⽽健壮。Therearefivetestdoublepatterns:Dummyobject,Teststub,Testspy,MockandFake.有五种测试双重模式:虚拟对象,测试存根,测试间谍,模拟和伪造。Dummyobject虚拟对象ThisarticleusesasimplegameclonetodemonstratetheusageoftestdoublesandtheNSubstitutelibrary.Thisgameiscentredaroundthespaceshipcontrolledbytheplayer.本⽂使⽤⼀个简单的游戏克隆来演⽰测试双打和NSubstitute库的⽤法。该游戏以玩家控制的飞船为中⼼。Theplayercanequipweaponstofightpirates.Thespaceshiphasweaponslotsandaweaponcanbeequippedifanemptyweaponslotisavailable.玩家可以装备武器来打击海盗。飞船有武器插槽,如果有空武器插槽,则可以装备武器。Testscenario:makesurethatweaponslotisoccupiedwhenweaponisequipped.测试场景:装备武器时,请确保武器插槽已被占⽤。Testwithdummyobjectcreatedmanually.使⽤⼿动创建的虚拟对象进⾏测试。TestwithNSubstitutebaseddummyobject使⽤基于NSubstitute的虚拟对象进⾏测试Weaponobjectisnominalinthisscenariowhichmeansthatitsmethodsandpropertiesarenotintendedtobeusedinthisexecutionpath.ItisjustarequiredparameterandanythingthatimplementsIWeaponinterfacecanbeused,includingnullwhenmethodhasnonullparametercheck.Thisobjectusageiscalledpattern.在这种情况下,武器对象是名义上的,这意味着该⽅法的路径和属性不打算在此执⾏路径中使⽤。它只是⼀个必需参数,可以使⽤实现IWeapon接⼝的任何东西,包括当⽅法没有空参数检查时包括空值。该对象⽤法称为模式。Therearetwoapproachestocreatedummyobjects.有两种创建虚拟对象的⽅法。Thefirstapproachistocreatedummyobjectmanually.Itmakessensetohaveonlyonedummyobjectperrealobject/interfaceandIDEfunctionswillhelpyoutogenerateclassesthatimplementinterface.Creatingdummyobjectsthiswayandstoringthemwithyourtestsisnotabigdeal.第⼀种⽅法是⼿动创建虚拟对象。每个真实对象/接⼝只有⼀个伪对象是有意义的,IDE函数将帮助您⽣成实现接⼝的类。这样创建虚拟对象并将其与测试⼀起存储并不重要。ThesecondapproachistouseNSubstitutethatisshippedwithUnityTestTools:第⼆种⽅法是使⽤UnityTestTools随附的NSubstitute:Rememberthatpassingnullasaparameterwhenmethodhasnonullparametercheckisalsoconsideredtobedummyobjectpattern.Nullisnotdescriptivethough.请记住,当⽅法没有空参数检查时,将空值作为参数传递也被视为伪对象模式。Null不是描述性的。Alldescribedmethodsarevalidandveryeasytouse.所有描述的⽅法都是有效的,并且⾮常易于使⽤。TestStub测试存根Butwhatifspaceship’smethodshouldreturnsomevalueandthisvalueistakenfromweaponobject?Inthiscase,weaponisnotaDummyobjectanymore.It’stimefora.但是,如果飞船的⽅法应该返回⼀些值并且该值取⾃武器对象,该怎么办?在这种情况下,武器不再是虚拟对象。该是的时候了。Teststubreturnsvaluesfortestingonespecificexecutionpath.E.g.BrokenWeaponStub,IncompatibleWeaponStubandothersthatletyoutestspecificscenarios.测试存根返回⽤于测试⼀个特定执⾏路径的值。例如BrokenWeaponStub,IncompatibleWeaponStub和其他可让您测试特定⽅案的项⽬。Testscenario:makesurethatshipshootsatleastoneshotiffunctionalweaponisequipped测试场景:如果装备了功能性武器,请确保飞船⾄少射击⼀枪Testwithteststubcreatedmanually.使⽤⼿动创建的测试存根进⾏测试。FunctionalWeaponStubimplementsIWeaponinterfacebutreturnhardcodedvalue.FunctionalWeaponStub实现IWeapon接⼝,但返回硬编码值。UnlikeDummyobjectthatdoesnothing,Stubcontainshardcodedvaluesandisintendedtoreturnthemasthisvaluesresultinspecificexecutionpath.与不执⾏任何操作的Dummy对象不同,Stub包含硬编码的值,并打算将其返回,因为此值会导致特定的执⾏路径。ItispossibletocreatethesamestubusingNSubstitute:可以使⽤NSubstitute创建相同的存根:TheobjectcreatedbySubstitute.ForimplementsIWeaponinterfaceandisreturnedasIWeapontype.Butitisactuallyaproxyobjectwhosebehaviourcouldbechanged.ReturnsisanextensionmethodofNSubstitutelibrarythatchangesthebehaviorofthisproxy.WhenShootmethodiscalled,thevaluespecifiedinReturns()methodisreturned.Itisalsopossibletoprovideasequenceofvaluesoradelegate.由Substitute.For创建的对象实现IWeapon接⼝,并以IWeapon类型返回。但这实际上是可以更改其⾏为的代理对象。Returns是NSubstitute库的扩展⽅法,该⽅法可以更改此代理的⾏为。调⽤Shoot⽅法时,将Returns()⽅法中指定的值。也可以提供⼀个值序列或⼀个委托。AsequenceofvalueswouldbereturnedIntheexamplebelow.在下⾯的⽰例中,将返回⼀个值序列。FirstcalltorandomNumberService.Range()withanyparameterswillresultin0.Nextcallwillreturn2andsoon.第⼀次使⽤任何参数对randomNumberService.Range()调⽤将导致0。下⼀次调⽤将返回2,依此类推。OtherusefulfunctionofNSubstituteistheabilitytomatchargumentsbytemplate.NSubstitute的另⼀个有⽤功能是能够通过模板匹配参数。IntheexamplebelowthedefaultbehaviorwouldbeoverriddenmakinganycalltoRange(10,100)return80.FormoredetailsreadtheNSubstitute.在下⾯的⽰例中,默认⾏为将被覆盖,使对Range(10,100)的任何调⽤均返回80。有关更多详细信息,请阅读NSubstitute。Theapproachwithrandomnumbergeneratorstubishighlyeffectiveintestingrandomevents,becauseitispossibletoemulaterequiredsequenceofrandomnumbers.带有随机数⽣成器存根的⽅法在测试随机事件⽅⾯⾮常有效,因为可以模拟所需的随机数序列。Makingstubsmanuallyiseasyandnamingthemcorrectlyleadstocreationofcleanandreadabletests.NSubstitutecandecreasetheamountofcodeandprovideadditionalflexibility.⼿动制作存根很容易,正确命名它们可以创建⼲净且可读的测试。NSubstitute可以减少代码量并提供额外的灵活性。TestSpy测试间谍Butwhatifitisneededtologthebehaviorofanobject?Howmanyhitshasanopponentreceived?Ifthetestdoublehasloggingfunctionality,thenitiscalleda.但是,如果需要记录对象的⾏为怎么办?对⼿收到了多少击球?如果测试双精度具有⽇志记录功能,则称为。Encountermeansmeetingsomeoneinspaceenroutetoastarsystem.Theplayercanchooseanactiontotakeinanencounter.Forexample,thepolicecouldbeignoredandapirateattacked.Testscenario:makesurethatopponentgetshitinanencounter意味着在太空中遇到某⼈到达星系。玩家可以选择⼀个动作来进⾏相遇。例如,警察可能会被忽略,海盗会遭到袭击。测试场景:确保对⼿在遭遇中被击中Encounterrequirestwospaceshipsandarandomnumbergeneratortocalculateprobableoutcomes.AlwaysMaxRandomNumberispassedwhichwillalwaysreturnmaximumnumberandthiswillleavenochancesfortheopponenttoevadethehit.Thetestisfollowingaveryspecificexecutionpath.count需要两个太空飞船和⼀个随机数⽣成器来计算可能的结果。AlwaysMaxRandomNumber被传递,它将始终返回最⼤数,这将使对⼿没有任何机会逃避命中。该测试遵循⾮常具体的执⾏路径。Theplayerspaceshipisrealinthisencounter,buttheopponentisaspy.Thespycombinesstubandloggingfunctionality.Itlogsthehitsthatwouldbecheckedlaterinthetest.玩家飞船在这次遭遇中是真实的,但对⼿是间谍。间谍程序结合了存根和⽇志记录功能。它记录了在测试中稍后将检查的匹配。ItispossibletoassembleatestspyusingNSubstitute’sandconstructs.可以使⽤NSubstitute的和结构来组装测试间谍。NSubstituteusesspecialargumentmatcherArg.Dotoexecuteadelegatewhenargumenttoitpassestothesubstitute.WhichmeansthathitCount+=x.Count()wouldbeexecutedoneachmethodcall.NSubstitute使⽤特殊的参数匹配器Arg.Do在将其参数传递给替代对象时执⾏委托。这意味着hitCount+=x.Count()将在每个⽅法调⽤上执⾏。MockObject模拟对象Thespyonlylogsthedata,leavingverificationtotheprogrammer.Whatwillhappenifaspyisgiventherighttoverifythedata?间谍仅记录数据,⽽将验证留给程序员。如果间谍被赋予验证数据的权利,将会发⽣什么?TestSpywithverificationcapabilityiscalled.(Note:Thisarticleshows.)具有验证功能的测试间谍称为。(注意:本⽂显⽰了。)Testscenario:makesurethateachweaponshootswhenspaceshipshootmethodiscalled测试场景:确保在调⽤太空飞船射击⽅法时射击每种武器Thistestcaseisspecial,notjustbecauseitusesMockobjectpattern,butbecauseitverifiesthebehaviourofthespaceshipinsteadofthestate.(Learnmoreabout)这个测试⽤例很特殊,不仅因为它使⽤了Mock对象模式,⽽且还因为它验证了飞船的⾏为⽽不是状态。(了解有关更多信息)Thetestdoesnotcareaboutreturningvaluesoftestdoubles,itmakessurethatcertainmethodswithcertainparameterswerecalled.Weuseanassumptionthatifaspaceshiphascalledcorrectmethodswithcorrectparametersonexternalsystems,thenitworkscorrectly.该测试并不关⼼测试双精度值的返回值,它可以确保调⽤具有某些参数的某些⽅法。我们假设,如果⼀艘太空船在外部系统上调⽤了具有正确参数的正确⽅法,那么它将正常⼯作。Makingmocksmanuallymightberathertimeconsuming.UsingNSubstituteframeworktogetmockobjectsforcheapisagoodchoice.⼿动进⾏模拟可能会⾮常耗时。使⽤NSubstitute框架廉价获取模拟对象是⼀个不错的选择。NSubstitutecanalsoverifyifmethodswerecalledinaspecificsequence.NSubstitute还可以验证是否按特定顺序调⽤了⽅法。Amockobjectnotonlylogsthecallsbutalsoverifiesthem.Itisatestspywithbuilt-inassertions.模拟对象不仅记录调⽤,还验证它们。它是带有内置断⾔的测试间谍。Fakeobject假物件Butwhatifatestdoubleneedssomelogic?但是,如果双重测试需要⼀些逻辑怎么办?Atestdoublethatcontainslogiciscalledaanditisadangerousbeast.Itistheonlytestdoublethatcontainslogicandemulatesarealsystemcomponent.AstheFakeitselfiscomplex,itisusedtosubstituterealsystemcomponentswhichcouldnotbesubstitutedbystubs.Themostcommonexampleisthesubstitutionofarealdatabasewithanin-memor
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年炭石墨负极材料项目发展计划
- 2025年羧甲淀粉钠项目建议书
- 实施均衡化城乡医疗卫生服务
- 加强市场监管确保供求信息真实性
- 公司内部事务处理方案
- 劳动合同签订与履行协议
- 电杆运输合同
- 2025年雕刻雕铣设备控制系统项目建议书
- 公司财务管理制度及流程
- 房屋租赁提前终止合同协议
- 药品经营企业(批发和零售)面临的风险点和应对措施
- 主要施工机械设备、劳动力、设备材料投入计划及其保证措施
- 无人机组装与调试 课件 项目1任务1 多旋翼无人机飞行平台组装调试
- 甲状腺乳腺外科ERAS实施流程(模板)
- 中国通 用技术集团招聘笔试题库
- 自动化部门的发展规划
- 2025届高考语文复习:小说人物+课件
- 2024年中国天然橡胶产业数据分析简报-农小蜂
- 《S公司客户开发与维护策略改进探究》开题报告10000字
- 1530学生安全教育记录表
- 村委会2025年工作总结及2025年工作计划
评论
0/150
提交评论