Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2001-2004 Craig Murphy Craig Murphy An Introduction to Test-Driven Development (TDD)

Similar presentations


Presentation on theme: "© 2001-2004 Craig Murphy Craig Murphy An Introduction to Test-Driven Development (TDD)"— Presentation transcript:

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

2 2 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

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

4 An Introduction to Test-Driven Development (TDD) 4 Motivation  One of my goals for Q3/Q4 2003 was to become evangelical about eXtreme 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

5 An Introduction to Test-Driven Development (TDD) 5 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

6 An Introduction to Test-Driven Development (TDD) 6 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”

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

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

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

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

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

12 An Introduction to Test-Driven Development (TDD) 12 TDD in Delphi using DUnit TObject TYourClass TTestCase TTestYourClass SomeMethod1 SomeMethod2 Test SomeMethod1 Test SomeMethod2 Test SomeMethod1 and SomeMethod2

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

14 An Introduction to Test-Driven Development (TDD) 14 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

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

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

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

18 An Introduction to Test-Driven Development (TDD) 18 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

19 An Introduction to Test-Driven Development (TDD) 19 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

20 An Introduction to Test-Driven Development (TDD) 20 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… A B

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

22 An Introduction to Test-Driven Development (TDD) 22 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

23 An Introduction to Test-Driven Development (TDD) 23 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

24 An Introduction to Test-Driven Development (TDD) 24 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

25 An Introduction to Test-Driven Development (TDD) 25 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/AgileScotland

26 An Introduction to Test-Driven Development (TDD) 26 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

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

28 An Introduction to Test-Driven Development (TDD) 28 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

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

30 An Introduction to Test-Driven Development (TDD) 30 Resources (Articles)  Test-Driven Development using csUnit 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 (Dec01)  An Introduction to Endo-Testing Using Mock Objects  Sacha Frick  The Delphi Magazine, Issue 96 (Aug03)

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

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


Download ppt "© 2001-2004 Craig Murphy Craig Murphy An Introduction to Test-Driven Development (TDD)"

Similar presentations


Ads by Google