Test-Driven Development Introduction

19
Test-Driven Test-Driven Development Development Introduction Introduction 김김김 김김김 Senior researcher Technology Research Institute

description

Test-Driven Development에 대한 개략적인 소개를 통해서 업무에 활용한다.

Transcript of Test-Driven Development Introduction

Page 1: Test-Driven Development Introduction

Test-Driven DevelopmentTest-Driven Development IntroductionIntroduction

김종선김종선Senior researcher Technology Research Institute

Page 2: Test-Driven Development Introduction

OutlineOutline

What is TDD?What is TDD? TDD in PracticeTDD in Practice Endo-TestingEndo-Testing Test-Driven UI DevelopmentTest-Driven UI Development ConclusionConclusion

Page 3: Test-Driven Development Introduction

Test-Driven Development (TDD)Test-Driven Development (TDD)

Is a programming practiceIs a programming practice Unit tests are written before the Unit tests are written before the

domain codedomain code Namely:Namely:

Write a test that failsWrite a test that fails Write code to make the test passWrite code to make the test pass RefactorRefactor

Unit Tests and Refactoring are the Unit Tests and Refactoring are the tools of TDDtools of TDD

Page 4: Test-Driven Development Introduction

Unit TestsUnit Tests

Test specific aspects of a functionalityTest specific aspects of a functionality Execute rapidlyExecute rapidly Independent of each otherIndependent of each other Independent of the surrounding Independent of the surrounding

environmentenvironment Independent of execution orderIndependent of execution order AutomatedAutomated

Page 5: Test-Driven Development Introduction

RefactoringRefactoring

Change the internal structure of the Change the internal structure of the code without changing it’s external code without changing it’s external behavior.behavior.

Associated with arithmetic Associated with arithmetic factorization:factorization:

ab + ac = a(b+c)ab + ac = a(b+c)

Same result but the expression is Same result but the expression is simplified.simplified.

Page 6: Test-Driven Development Introduction

Test ListTest List

Task Based Task Based 4-8 hour duration4-8 hour duration 15-20 minute exercise at beginning 15-20 minute exercise at beginning

Brainstorm a list of unit testsBrainstorm a list of unit tests Do not get hung up on completeness, Do not get hung up on completeness,

you can always add more lateryou can always add more later Describes completion requirements Describes completion requirements

Page 7: Test-Driven Development Introduction

RedRed//GreenGreen//RefactorRefactor

Write 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

RefactorRefactor as needed as needed

Page 8: Test-Driven Development Introduction

Financial ServiceFinancial Service A simple application that implements the A simple application that implements the

following set of functionality:following set of functionality: Credit an AccountCredit an Account Debit an AccountDebit an Account

Page 9: Test-Driven Development Introduction

Characteristics of TDDCharacteristics of TDD

TDD promotes code testabilityTDD promotes code testability Testability is related to:Testability is related to:

Strong cohesionStrong cohesion Loose couplingLoose coupling No redundancyNo redundancy EncapsulationEncapsulation

These are good programming practicesThese are good programming practices Striving for good tests results in better Striving for good tests results in better

codecode

Page 10: Test-Driven Development Introduction

TDD TenetsTDD Tenets

Never 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 test

Eliminate Duplication (Refactor Eliminate Duplication (Refactor mercilessly)mercilessly)

Page 11: Test-Driven Development Introduction

ObservationObservation

It’s harder to write unit tests for It’s harder to write unit tests for components located at the “edge” of components located at the “edge” of the system:the system: Web ServicesWeb Services Data Access LayerData Access Layer User InterfaceUser Interface

Page 12: Test-Driven Development Introduction

Mock ObjectsMock Objects

Inherent challenges in testing Inherent challenges in testing dependant objectsdependant objects Objects dependant on ephemeral objects Objects dependant on ephemeral objects

produce unpredictable behaviorproduce unpredictable behavior User Interfaces, Databases and the like User Interfaces, Databases and the like

are inherently ephemeralare inherently ephemeral

Page 13: Test-Driven Development Introduction

ExampleExample

How can we write a test for GetRate() How can we write a test for GetRate() when the result of when the result of FinancialServicesWS.GetRate() is FinancialServicesWS.GetRate() is unpredictable?unpredictable?

Money

+GetRate(string from, string to):double

FinancialServices WS

+GetRate(string from, string to):double

Page 14: Test-Driven Development Introduction

Mock ObjectsMock Objects

Solution:Solution: Replace the unpredictable object with a Replace the unpredictable object with a

testing version that produces predictable testing version that produces predictable resultsresults

FinancialServicesWS

+GetRate(string from, string to):double

MockFinancialServicesWS

+GetRate(string from, string to):double

IFinancialServicesWS

+GetRate(string from, string to):double

Page 15: Test-Driven Development Introduction

ConclusionConclusion

Advantages of Mock Objects:Advantages of Mock Objects: Promote design to interfacesPromote design to interfaces Promote testability and isolation of testsPromote testability and isolation of tests Promote decouplingPromote decoupling

Challenges of Mock Objects:Challenges of Mock Objects: More classes to maintainMore classes to maintain Requires breaking encapsulation to Requires breaking encapsulation to

replace real object with mock object, replace real object with mock object, sometimes resulting in less “elegant” sometimes resulting in less “elegant” codecode

Page 16: Test-Driven Development Introduction

User InterfacesUser Interfaces

Difficult 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.

Inherent challenges in testing Windows Inherent challenges in testing Windows Forms applications:Forms applications: Are highly coupledAre highly coupled .Exe assemblies cannot be referenced .Exe assemblies cannot be referenced

from the IDEfrom the IDE

Page 17: Test-Driven Development Introduction

ConclusionConclusion

Advantages:Advantages: Easy to implementEasy to implement Development of UI can be completely test-Development of UI can be completely test-

drivendriven Promotes decouplingPromotes decoupling Enables creation of automated User Enables creation of automated User

Acceptance TestsAcceptance Tests

Challenges:Challenges: Setup requires somewhat complex wiringSetup requires somewhat complex wiring

Page 18: Test-Driven Development Introduction

ReferencesReferences

““Test-Driven Development By Test-Driven Development By Example”, Kent BeckExample”, Kent Beck

““Refactoring, Improving The Design Of Refactoring, Improving The Design Of Existing Code”, Martin FowlerExisting Code”, Martin Fowler

““Patterns Of Enterprise Application Patterns Of Enterprise Application Architecture”, Martin FowlerArchitecture”, Martin Fowler

Page 19: Test-Driven Development Introduction

Q & AQ & A