Test Test- Driven Driven Development Development Jos José Almeida, Microsoft Almeida, Microsoft ( [email protected][email protected]) Outline Outline What What is is TDD? TDD? TDD TDD in in Practice Practice Endo Endo- Testing Testing Test Test- Driven Driven UI UI Development Development Conclusion Conclusion
13
Embed
Test-Driven Developmenthome.iscte-iul.pt/~mms/events/agile_seminar/apresentacoes/jose_al… · 2 Test-Driven Development (TDD) zIs a programming practice zUnit tests are written before
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
IsIs a a programmingprogramming practicepracticeUnitUnit teststests are are writtenwritten beforebefore thethedomaindomain codecodeNamelyNamely::
WriteWrite a a testtest thatthat failsfailsWriteWrite codecode to to makemake thethe testtest passpassRefactorRefactor
UnitUnit TestsTests andand RefactoringRefactoring are are thethetoolstools ofof TDDTDD
UnitUnit TestsTestsTestTest specificspecific aspectsaspects ofof a a functionalityfunctionalityExecute Execute rapidlyrapidlyIndependentIndependent ofof eacheach otherotherIndependentIndependent ofof thethe surroundingsurroundingenvironmentenvironmentIndependentIndependent ofof executionexecution orderorderAutomatedAutomated
3
RefactoringRefactoringChange the internal structure of the Change the internal structure of the code without changing itcode without changing it’’s external s external behavior.behavior.Associated with arithmetic Associated with arithmetic factorization:factorization:
abab + ac = + ac = a(b+ca(b+c))
Same result but the expression is Same result but the expression is simplified.simplified.
Test ListTest ListTask Based Task Based
44--8 hour duration8 hour duration1515--20 minute exercise at beginning 20 minute exercise at beginning
Brainstorm a list of unit testsBrainstorm a list of unit testsDo not get hung up on completeness, Do not get hung up on completeness, you can always add more lateryou can always add more laterDescribes completion requirements Describes completion requirements
4
RedRed//GreenGreen//RefactorRefactorWrite a test for new capability
Start
Compile
Fix compile errors
Run the testRun the testAnd see it failAnd see it fail
Write the code
Run the testRun the testAnd see it passAnd see it pass
StrongStrong cohesioncohesionLooseLoose couplingcouplingNo No redundancyredundancyEncapsulationEncapsulation
TheseThese are are goodgood programmingprogramming practicespracticesStrivingStriving for for goodgood teststests resultsresults inin betterbettercodecode
TDD TenetsTDD TenetsNever write a single line of code unless Never write a single line of code unless you have a failing unit testyou have a failing unit testEliminate Duplication (Eliminate Duplication (RefactorRefactormercilessly)mercilessly)
6
ObservationObservationItIt’’s s harderharder to to writewrite unitunit teststests for for componentscomponents locatedlocated atat thethe ““edgeedge”” ofofthethe systemsystem::
WebWeb ServicesServicesData Access Data Access LayerLayerUserUser InterfaceInterface
HowHow cancan wewe writewrite a a testtest for for GetRate(GetRate() ) whenwhen thethe resultresult ofofFinancialServicesWS.GetRate(FinancialServicesWS.GetRate() ) isisunpredictableunpredictable??
Steps to create a Dynamic Mock Steps to create a Dynamic Mock Object with Object with DotNetMockDotNetMock
1.1. FinancialServicesWSFinancialServicesWS is a proxy generated is a proxy generated from the WSDL of the web service. from the WSDL of the web service. RefactorRefactorthe proxy to extract an interface: the proxy to extract an interface: IFinancialServicesWSIFinancialServicesWS
2.2. Create a controller based on the extracted Create a controller based on the extracted interfaceinterface
3.3. HaveHave thethe controllercontroller createcreate a a dynamicdynamic mockmockobjectobject
PromotePromote design to interfacesdesign to interfacesPromotePromote testabilitytestability andand isolationisolation ofof teststestsPromotePromote decouplingdecoupling
ChallengesChallenges ofof MockMock ObjectsObjects::More classes to More classes to maintainmaintainRequiresRequires breakingbreaking encapsulationencapsulation to to replacereplace real real objectobject withwith mockmock objectobject, , sometimessometimes resultingresulting inin lessless ““elegantelegant””codecode
UserUser InterfacesInterfacesDifficult to write automated tests to, Difficult to write automated tests to, because they are designed to be because they are designed to be exercised by a user and often hide exercised by a user and often hide their programmatic interface.their programmatic interface.InherentInherent challengeschallenges inin testingtesting Windows Windows FormsForms applicationsapplications::
Are Are highlyhighly coupledcoupled..ExeExe assembliesassemblies cannotcannot bebe referencedreferencedfromfrom thethe IDEIDE
10
NUnitFormsNUnitFormsNUnitNUnit ExtensionExtension for for testingtesting Windows Windows FormsForms applicationsapplications
StepsSteps to to testtest a a WinFormsWinForms AppApp
1.1. PlacePlace thethe formsforms inin a a separateseparate classclasslibrarylibrary
4.4. Design Design thethe FormForm5.5. Use Use NUnitFormsNUnitForms to to writewrite thethe teststests6.6. WriteWrite thethe codecode to to passpass thethe teststests
11
UnitUnit TestingTesting Windows Windows FormsForms
ConclusionConclusionAdvantagesAdvantages::
EasyEasy to to implementimplementDevelopmentDevelopment ofof UI UI cancan bebe completelycompletely testtest--drivendrivenPromotesPromotes decouplingdecouplingEnablesEnables creationcreation ofof automatedautomated UserUserAcceptanceAcceptance TestsTests