introduction into tdd mantra

Upload: yunit

Post on 07-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Introduction Into TDD Mantra

    1/32

    TestTest--Driven DevelopmentDriven DevelopmentAn introduction into the TDD MantraAn introduction into the TDD Mantra

    Dennis van der SteltDennis van der Stelt

    LogicaCMGLogicaCMG

    Dennis van der SteltDennis van der Stelt

    LogicaCMGLogicaCMG

  • 8/6/2019 Introduction Into TDD Mantra

    2/32

    AgendaAgenda

    1.1. A Quick IntroductionA Quick Introduction2.2. TestTest--Driven Development MantraDriven Development Mantra

    3.3. RefactoringRefactoring

    4.4. Benefits of TestBenefits of Test--Driven DevelopmentDriven Development5.5. DemoDemo

    6.6. Key rules of TestKey rules of Test--Driven DevelopmentDriven Development

    7.7. Mocking/StubbingMocking/Stubbing

    8.8. Legacy CodeLegacy Code

  • 8/6/2019 Introduction Into TDD Mantra

    3/32

    A Quick IntroductionA Quick Introduction

    Unit testingUnit testing

    Automated testingAutomated testing

    Facilitates changeFacilitates change

    Simplifies integrationSimplifies integration

    System level documentationSystem level documentation

    Seperates interface from implementationSeperates interface from implementation

  • 8/6/2019 Introduction Into TDD Mantra

    4/32

    TDD MantraTDD Mantra

    Write the testWrite the test

    Write the codeWrite the code

    RefactorRefactor

    RepeatRepeat

    REDRED

    REDRED GREENGREEN

    GREENGREEN REFACTORREFACTOR

    REFACTORREFACTOR

    IM TESTIM TEST--DRIVENDRIVEN

    IM TESTIM TEST--DRIVENDRIVEN

  • 8/6/2019 Introduction Into TDD Mantra

    5/32

    TDD MantraTDD Mantra

    Where to begin?Where to begin?

    Work from specification (or notes, orWork from specification (or notes, ornapkin)napkin)

    Create a list of testsCreate a list of testsPick the simplest one.Pick the simplest one.

    Make test, make code.Make test, make code.

    RefactorRefactor

    Find a bug? Create a test that wouldveFind a bug? Create a test that wouldve

    caught it!caught it!

  • 8/6/2019 Introduction Into TDD Mantra

    6/32

  • 8/6/2019 Introduction Into TDD Mantra

    7/32

    TestTest--Driven DevelopmentDriven Development

    ...its about simple design that...its about simple design that

    Runs all testsRuns all tests

    Reveals all the intentionReveals all the intentionHas no duplicationHas no duplication

    Has fewest number of classes andHas fewest number of classes and

    methodsmethodsKent Beck, Extreme Programming Explained, 1999

  • 8/6/2019 Introduction Into TDD Mantra

    8/32

    TestTest--Driven DevelopmentDriven Development

    The act of writing a unit test is more an act of design then of verificationThe act of writing a unit test is more an act of design then of verification

    -- Robert C. MartinRobert C. Martin

    TestTest--driven development seeks specification, not validation, letting youdriven development seeks specification, not validation, letting you

    think through your design before you write your functional codethink through your design before you write your functional code-- Scott AmblerScott Ambler

    TestTest--Driven Development is a powerful way to produce well designed codeDriven Development is a powerful way to produce well designed codewith fewer defectswith fewer defects

    -- Martin FowlerMartin Fowler

    Fewer defects, less debugging, more confidence, better design, and higherFewer defects, less debugging, more confidence, better design, and higherproductivity in my programming practiceproductivity in my programming practice

    -- Kent BeckKent Beck

  • 8/6/2019 Introduction Into TDD Mantra

    9/32

    RefactoringRefactoring

    Restructuring of softwareRestructuring of software

    by applying a series of internalby applying a series of internalchanges that do not affect itschanges that do not affect its

    observable behaviorobservable behavior

    Martin Fowler, Refactoring, 2000

  • 8/6/2019 Introduction Into TDD Mantra

    10/32

  • 8/6/2019 Introduction Into TDD Mantra

    11/32

  • 8/6/2019 Introduction Into TDD Mantra

    12/32

    Benefits of TDD (3/3)Benefits of TDD (3/3)

    Cost of ChangeCost of Change

    WaterfallTest-Driven Development embraces change!

  • 8/6/2019 Introduction Into TDD Mantra

    13/32

    Cons of TDDCons of TDD

    Needs practice, but youll learn to love it!Needs practice, but youll learn to love it!

    Needs disciplineNeeds discipline

    Need to know OODPNeed to know OODP

    Scares away managersScares away managers

  • 8/6/2019 Introduction Into TDD Mantra

    14/32

    Bowling gameBowling gameFirst a quick design sessionFirst a quick design session

  • 8/6/2019 Introduction Into TDD Mantra

    15/32

    A quick design sessionA quick design session

    Lets go ahead and design our bowling game.Lets go ahead and design our bowling game.

    We clearly need the Gameclass.

    A game has ten frames.A frame has 1 or 2 roles.

    The tenth frame has two or three roles.It is different from all the other frames.

    The score function must iteratethrough all the frames, andcalculate all their scores.

    next framenext frame The score for spare or strikedepends on the frames succesor

    1010 1..21..2

    11

  • 8/6/2019 Introduction Into TDD Mantra

    16/32

    Bowling gameBowling gameFinally some code!!!Finally some code!!!

  • 8/6/2019 Introduction Into TDD Mantra

    17/32

    Key rules to TDDKey rules to TDD

    A test is no unit test ifA test is no unit test if

    it talks to a databaseit talks to a database

    it communicates across the networkit communicates across the network

    it touches the file systemit touches the file system

    it cant run at the same time as otherit cant run at the same time as otherteststests

    you have to perform actions to run theyou have to perform actions to run thetests (ex. configuration change)tests (ex. configuration change)

    Michael Feathers, 2003

  • 8/6/2019 Introduction Into TDD Mantra

    18/32

    Mocking / StubbingMocking / Stubbing

    A mock or stub stands in forA mock or stub stands in for

    an object outside your unitan object outside your unit

    an external object (database)an external object (database)

    a complex object(s)a complex object(s)

    a not yet developed objecta not yet developed object

    Mocking & Stubbing are different things!Mocking & Stubbing are different things!

  • 8/6/2019 Introduction Into TDD Mantra

    19/32

    Mocking / StubbingMocking / Stubbing

    public class BusinessClass{

    DataClass _dataLayer;

    public BusinessClass(){

    _dataLayer = new DataClass();}

    public void PerformSomeSortOfAction(DataSet dataSet){

    _dataLayer.SaveSomething(dataSet);}

    }

    Dependency injectionDependency injection

  • 8/6/2019 Introduction Into TDD Mantra

    20/32

    Mocking / StubbingMocking / Stubbing

    public interface IDataAccessGateway

    { void SaveSomething(DataSet dataSet);}

    public class BusinessClass{

    private IDataAccessGateway _gateway;

    public BusinessClass(IDataAccessGateway gateway){

    _gateway = gateway;}

    public void PerformSomeSortOfAction(DataSet dataSet){

    _gateway.SaveSomething(dataSet);}

    }

    Dependency injectionDependency injection

  • 8/6/2019 Introduction Into TDD Mantra

    21/32

    Mocking / StubbingMocking / Stubbing

    public interface IDataAccessGateway{

    void SaveSomething(DataSet dataSet);}

    public class BusinessClass{

    private IDataAccessGateway _gateway;

    public BusinessClass(IDataAccessGateway gateway){

    _gateway = gateway;}

    public BusinessClass() : this(new GateWay()) {}

    public void PerformSomeSortOfAction(DataSet dataSet){

    _gateway.SaveSomething(dataSet);}

    }

    Dependency injectionDependency injection

  • 8/6/2019 Introduction Into TDD Mantra

    22/32

    Mocking / StubbingMocking / Stubbing

    How about .NET 2.0 Generics?How about .NET 2.0 Generics?

    LoggerLogger

    Stock QuotesStock Quotes

    Web AppWeb App

    AuthenticatorAuthenticator

    Error HandlerError Handler DatabaseDatabase

    Example originally created by Jim Weirich in Ruby on his blog.Conversion to .NET originally by Brad Wilson and Peter Provost.

  • 8/6/2019 Introduction Into TDD Mantra

    23/32

    Mocking / StubbingMocking / Stubbing

    .NET 2.0 Generics : The old way.NET 2.0 Generics : The old waypublic class WebApp{

    public WebApp(){quotes = new StockQuotes();

    authenticator = new Authenticator();database = new DataBase();logger = new Logger();errorHander = new ErrorHandler();

    }

    // Some more code here

    }

  • 8/6/2019 Introduction Into TDD Mantra

    24/32

    Mocking / StubbingMocking / Stubbing

    .NET 2.0 Generics : First the interface.NET 2.0 Generics : First the interfacepublic interface ILocater{TObject Get();

    }

  • 8/6/2019 Introduction Into TDD Mantra

    25/32

    Mocking / StubbingMocking / Stubbing

    .NET 2.0 Generics : Example setting it up.NET 2.0 Generics : Example setting it uppublic interface ILocater{TObject Get();

    }

    public class MyLocator : ILocator

    {protected Dictionary dict =new Dictionary();

    public MyLocator(){dict.Add(typeof(ILogger), new Logger());dict.Add(typeof(IErrorHandler), new ErrorHandler());dict.Add(typeof(IQuotes), new StockQuotes());dict.Add(typeof(IDataBase), new Database());

    dict.Add(typeof(IAuthenticator), new Authenticator());}}

  • 8/6/2019 Introduction Into TDD Mantra

    26/32

    Mocking / StubbingMocking / Stubbing

    .NET 2.0 Generics : Get & use the objects.NET 2.0 Generics : Get & use the objectspublic interface ILocater{TObject Get();

    }

    public class MyLocator : ILocator

    {protected Dictionary dict =new Dictionary();

    public MyLocator(){dict.Add(typeof(ILogger), new Logger());dict.Add(typeof(IErrorHandler), new ErrorHandler());dict.Add(typeof(IQuotes), new StockQuotes());dict.Add(typeof(IDataBase), new Database());

    dict.Add(typeof(IAuthenticator), new Authenticator());}}

    public class StockQuotes{public StockQuotes(ILocator locator){errorHandler = locator.Get();logger = locator.Get();

    }}

  • 8/6/2019 Introduction Into TDD Mantra

    27/32

    Mocking / StubbingMocking / Stubbing

    How about legacy code?How about legacy code?public interface IDataService { }

    // Nowpublic class ProxyToNastyLegacyDataService : IDataService { }

    // Later

    public class CleanNewCodeDataService : IDataService { }public class StranglerApplication{

    public StranglerApplication(IDataService dataService) { }}

    The term StranglerApplication was introduced by Martin Fowler in 2004.Read more at martinfowler.com

  • 8/6/2019 Introduction Into TDD Mantra

    28/32

    Legacy codeLegacy code

    Why is legacy code bad?Why is legacy code bad?The previous developer isnt there to explain it.The previous developer isnt there to explain it.

    It takes time to learn the code.It takes time to learn the code.

    Its not easy to change the code.Its not easy to change the code.

    You might break some part of the applicationYou might break some part of the applicationyou didnt even touch.you didnt even touch.

    Wikipedia :Wikipedia : Legacy code is source code thatLegacy code is source code thatrelates to a norelates to a no--longer supported or manufacturedlonger supported or manufacturedoperating system or other computer system.operating system or other computer system.

    What is legacy code?What is legacy code?

  • 8/6/2019 Introduction Into TDD Mantra

    29/32

  • 8/6/2019 Introduction Into TDD Mantra

    30/32

    WRI E UNI TESTS!

    Conclusion:

    and do it the right way using Test-Driven Development.

  • 8/6/2019 Introduction Into TDD Mantra

    31/32

  • 8/6/2019 Introduction Into TDD Mantra

    32/32

    Web SitesWeb Siteshttp://www.agilealliance.org/http://www.agilealliance.org/http://xp.c2.com/ExtremeProgrammingRoadmap.htmlhttp://xp.c2.com/ExtremeProgrammingRoadmap.html

    http://www.xprogramming.com/http://www.xprogramming.com/http://www.testdriven.com/http://www.testdriven.com/

    BlogsBlogs

    http://butunclebob.com/http://butunclebob.com/http://martinfowler.com/bliki/http://martinfowler.com/bliki/http://codebetter.com/http://codebetter.com/http://steve.emxsoftware.com/http://steve.emxsoftware.com/