An Introduction to TestDriven Development TDD Craig Murphy

  • Slides: 32
Download presentation
An Introduction to Test-Driven Development (TDD) § Craig Murphy craig@craigmurphy. com http: //www. Craig.

An Introduction to Test-Driven Development (TDD) § Craig Murphy craig@craigmurphy. com http: //www. Craig. Murphy. com © 2001 -2004 Craig Murphy

Disclaimer § Developer = Programmer § We’re talking about unit testing – i. e.

Disclaimer § Developer = Programmer § We’re talking about unit testing – i. e. testing the internals of a class § Black box testing for objects § Classes are testing in isolation § Loosely coupled, highly cohesive architectures An Introduction to Test-Driven Development (TDD) 2

Agenda § § § Motivation What is TDD? TDD Stages TDD in Delphi Demonstration

Agenda § § § Motivation What is TDD? TDD Stages TDD in Delphi Demonstration § Delphi § C#Builder using cs. Unit § Why TDD? § Summary § Notable Quotes An Introduction to Test-Driven Development (TDD) 3

Motivation § One of my goals for Q 3/Q 4 2003 was to become

Motivation § One of my goals for Q 3/Q 4 2003 was to become evangelical about e. Xtreme Programming (XP) and Test-Driven Development (TDD) § TDD sits nicely in the XP “way of doing things” § TDD can be used without practicing XP § Writing articles and giving presentations is one such way of achieving that goal § To reduce the amount of re-testing that is required § Especially with legacy applications § To avoid introducing new bugs after refactoring existing code An Introduction to Test-Driven Development (TDD) 4

What is TDD? § “Before you write code, think about what it will do.

What is TDD? § “Before you write code, think about what it will do. Write a test that will use the methods you haven’t even written yet. ” § Extreme Programming Applied: Playing To Win § Ken Auer, Roy Miller § “The Purple Book” § A test is not something you “do”, it is something you “write” and run once, twice, three times, etc. § It is a piece of code § Testing is therefore “automated” § Repeatedly executed, even after small changes An Introduction to Test-Driven Development (TDD) 5

What is TDD? § TDD is a technique whereby you write your test cases

What is TDD? § TDD is a technique whereby you write your test cases before you write any implementation code § Tests drive or dictate the code that is developed § An indication of “intent” § Tests provide a specification of “what” a piece of code actually does § Some might argue that “tests are part of the documentation” An Introduction to Test-Driven Development (TDD) 6

Agenda § § § Motivation What is TDD? TDD Stages TDD in Delphi Demonstration

Agenda § § § Motivation What is TDD? TDD Stages TDD in Delphi Demonstration § Delphi § C#Builder using cs. Unit § Why TDD? § Summary § Notable Quotes An Introduction to Test-Driven Development (TDD) 7

TDD Stages § In Extreme Programming Explored (The Green Book), Wake describes the test

TDD Stages § In Extreme Programming Explored (The Green Book), Wake describes the test / code cycle: 1. 2. Write a single test Compile it. It shouldn’t compile because you’ve not written the implementation code Implement just enough code to get the test to compile Run the test and see it fail Implement just enough code to get the test to pass Run the test and see it pass Refactor for clarity and “once and only once” Repeat 3. 4. 5. 6. 7. 8. An Introduction to Test-Driven Development (TDD) Bill 8

TDD Stages Write a test Refactor code (and test) Compile Run test, watch it

TDD Stages Write a test Refactor code (and test) Compile Run test, watch it pass Fix compile errors Write code Run test, watch it fail An Introduction to Test-Driven Development (TDD) 9

Agenda § § § Motivation What is TDD? TDD Stages TDD in Delphi Demonstration

Agenda § § § Motivation What is TDD? TDD Stages TDD in Delphi Demonstration § Delphi § C#Builder using cs. Unit § Why TDD? § Summary § Notable Quotes An Introduction to Test-Driven Development (TDD) 10

TDD in Delphi § DUnit § An x. Unit implementation for Delphi § x.

TDD in Delphi § DUnit § An x. Unit implementation for Delphi § x. Unit is a colloquial umbrella term § Platform-specific implementations § e. g. s. Unit, JUnit, XMLUnit § Provides classes with methods to help us write test cases § TTest. Case § “Check” functions… An Introduction to Test-Driven Development (TDD) 11

TDD in Delphi using DUnit TObject TTest. Case TYour. Class TTest. Your. Class Test

TDD in Delphi using DUnit TObject TTest. Case TYour. Class TTest. Your. Class Test Some. Method 1 Some. Method 2 Test Some. Method 2 Some. Method 1 Test Some. Method 1 and Some. Method 2 An Introduction to Test-Driven Development (TDD) 12

TDD in Delphi using DUnit Check (condition: boolean; msg: string = ''); Check. Equals

TDD in Delphi using DUnit Check (condition: boolean; msg: string = ''); Check. Equals (expected, actual: integer; msg: string = ''); procedure TTest. Case. List. Test. Add; var Add. Object: TObject; FEmpty : TList; Begin FEmpty : = TList. Create; Add. Object : = TObject. Create; FEmpty. Add(Add. Object); // The following calls check to see if everything went OK. // When check fails, it will end up in the Test. Result as a failure. Check(FEmpty. Count = 1); Check(FEmpty. Items[0] = Add. Object); end; An Introduction to Test-Driven Development (TDD) 13

Test Complexity § Do the simplest thing § § Dave Astels: “Strive for simplicity”

Test Complexity § Do the simplest thing § § Dave Astels: “Strive for simplicity” Write a test that fails (red) Make the test pass (green) Refactor implementation code (change the internal design) § Use the compiler – let it tell you about errors and omissions § One assertion (Check/Assert) per test § Subject of furious debate on Yahoo’s TDD group An Introduction to Test-Driven Development (TDD) 14

demo Test-driven development using Dunit in Delphi 6 using cs. Unit in C#Builder An

demo Test-driven development using Dunit in Delphi 6 using cs. Unit in C#Builder An Introduction to Test-Driven Development (TDD) 15

Smells § Duplication § Once And Once Only (OAOO) § Setup / Tear. Down

Smells § Duplication § Once And Once Only (OAOO) § Setup / Tear. Down § Some duplicated code reveals itself as common ‘initialisation’ code § And / Or as ‘cleanup’ code An Introduction to Test-Driven Development (TDD) 16

Agenda § § § Motivation What is TDD? TDD Stages TDD in Delphi Demonstration

Agenda § § § Motivation What is TDD? TDD Stages TDD in Delphi Demonstration § Delphi § C#Builder using cs. Unit § Why TDD? § Summary § Notable Quotes An Introduction to Test-Driven Development (TDD) 17

Why TDD? § Programmers dislike testing § They will test reasonably thoroughly the first

Why TDD? § Programmers dislike testing § They will test reasonably thoroughly the first time § The second time however, testing is usually less thorough § The third time, well. . § Testing is considered a “boring” task § Testing might be the job of another department / person § TDD encourages programmers to maintain an exhaustive set of repeatable tests § Tests live alongside the Class/Code Under Test (CUT) § With tool support, tests can be run selectively § The tests can be run after every single change An Introduction to Test-Driven Development (TDD) 18

Why TDD? § Bob Martin: § “The act of writing a unit test is

Why TDD? § Bob Martin: § “The act of writing a unit test is more an act of design than of verification” § Confidence boost § By practicing TDD, developers will strive to improve their code – without the fear that is normally associated with code changes § Isn’t the green bar a feel good factor? § Remove / Reduce reliance on the debugger § No more “debug-later” attitudes An Introduction to Test-Driven Development (TDD) 19

Who should write the tests? § The programmers should write the tests § The

Who should write the tests? § The programmers should write the tests § The programmers can’t wait for somebody else to write tests § TDD promotes “small steps”, and lots of them § Small steps: the shortest distance between two points § Your destination is closer… B A An Introduction to Test-Driven Development (TDD) 20

Agenda § § § Motivation What is TDD? TDD Stages TDD in Delphi Demonstration

Agenda § § § Motivation What is TDD? TDD Stages TDD in Delphi Demonstration § Delphi § C#Builder using cs. Unit § Why TDD? § Summary § Notable Quotes An Introduction to Test-Driven Development (TDD) 21

Summary § TDD does not replace traditional testing § It defines a proven way

Summary § TDD does not replace traditional testing § It defines a proven way that ensures effective unit testing § Tests are working examples of how to invoke a piece of code § Essentially provides a working specification for the code § No code should go into production unless it has associated tests § Catch bugs before they are shipped to your customer § No code without tests § Tests determine, or dictate, the code An Introduction to Test-Driven Development (TDD) 22

Summary § TDD isn’t new § To quote Kent Beck: § “…you type the

Summary § TDD isn’t new § To quote Kent Beck: § “…you type the expected output tape from a real input tape, then code until the actual results matched the expected result…” § TDD means less time spent in the debugger § TDD negates fear § Fear makes developers communicate less § Fear makes developers avoid repeatedly testing code § Afraid of negative feedback An Introduction to Test-Driven Development (TDD) 23

Summary § TDD promotes the creation of a set of “programmer tests” § Automated

Summary § TDD promotes the creation of a set of “programmer tests” § Automated tests that are written by the programmer § Exhaustive § Can be run over and over again § TDD allows us to refactor, or change the implementation of a class, without the fear of breaking it § TDD and refactoring go hand-in-hand § With care, [some] User Acceptance Tests can codified and run as part of the TDD process An Introduction to Test-Driven Development (TDD) 24

Notable Quotes § Ron Jeffries, TDD and XP aficionado on the subject of TDD:

Notable Quotes § Ron Jeffries, TDD and XP aficionado on the subject of TDD: § “clean code that works” § Martin Fowler: “The code is the design” § Alan Francis: “Legacy code is code without tests” § To learn more about Agile Methods, Extreme Programming & Test-Driven Development visit: http: //groups. yahoo. com/group/Agile. Scotland An Introduction to Test-Driven Development (TDD) 25

Resources (Books) test-driven development: A Practical Guide Dave Astels Prentice-Hall/Pearson Education, 2003 ISBN 0

Resources (Books) test-driven development: A Practical Guide Dave Astels Prentice-Hall/Pearson Education, 2003 ISBN 0 -13 -101649 -0 Reviewed BUG developers’ magazine, Nov/Dec 2003 ___________________ Test-Driven Development: By Example Kent Beck Addison-Wesley, 2003 ISBN 0 -321 -14653 -0 An Introduction to Test-Driven Development (TDD) 26

Resources (Books) Refactoring: Improving the Design of Existing Code Martin Fowler Addison-Wesley, 1999 ISBN

Resources (Books) Refactoring: Improving the Design of Existing Code Martin Fowler Addison-Wesley, 1999 ISBN 0 -201 -48567 -2 An Introduction to Test-Driven Development (TDD) 27

Resources (web-sites) § NUnit: http: //www. nunit. org § CSUnit: http: //www. csunit. org

Resources (web-sites) § NUnit: http: //www. nunit. org § CSUnit: http: //www. csunit. org § http: //groups. yahoo. com/group/csunit § Mock Objects: http: //www. mockobjects. com § testdrivendevelopment group (Yahoo): § http: //groups. yahoo. com/group/testdrivendevelopment § Ron Jeffries, Dave Astels, Kent Beck, etc. are regular contributors An Introduction to Test-Driven Development (TDD) 28

Resources (web-sites) § The DUnit group at Source. Forge § http: //dunit. sourceforge. net

Resources (web-sites) § The DUnit group at Source. Forge § http: //dunit. sourceforge. net § Lutz Roeder’s. NET Reflector: § http: //www. aisto. com/roeder/dotnet § x. Unit implementations: § http: //www. xprogramming. com/software. htm § http: //www. junit. org An Introduction to Test-Driven Development (TDD) 29

Resources (Articles) § Test-Driven Development using cs. Unit in C#Builder § Craig Murphy §

Resources (Articles) § Test-Driven Development using cs. Unit in C#Builder § Craig Murphy § UK-BUG Magazine Issue Jan/Feb 2004 § “To err is human: automated testing of Delphi code with DUnit” § Kris Golko § UK-BUG Magazine Issue Nov/Dec 2002 § Testing: Quality Time with DUnit § Rob Bracken § The Delphi Magazine, Issue 76 (Dec 01) § An Introduction to Endo-Testing Using Mock Objects § Sacha Frick § The Delphi Magazine, Issue 96 (Aug 03) An Introduction to Test-Driven Development (TDD) 30

Contact and Update Information Craig Murphy craig@Craig. Murphy. com Updated slides, notes and source

Contact and Update Information Craig Murphy craig@Craig. Murphy. com Updated slides, notes and source code: http: //www. Craig. Murphy. com An Introduction to Test-Driven Development (TDD) 31

Questions? RED GREEN REFACTOR An Introduction to Test-Driven Development (TDD) 32

Questions? RED GREEN REFACTOR An Introduction to Test-Driven Development (TDD) 32