Test Driven Development Kenneth M Anderson University of Colorado Boulder CSCI 5828 Lecture 22 04 01 2010 University of Colorado 2010 1 Credit where Credit is Due I 2 Some of the material for this lecture is taken from TestDriven Development by Kent Beck as such some of this material is copyright Addison Wesley 2003 In addition some material for this lecture is taken from Agile Software Development Principles Patterns and Practices by Robert C Martin as such some materials is copyright Pearson Education Inc 2003 Credit where Credit is Due II Finally one of the examples is inspired by the Roman Numerals example that is featured in Dive into Python 3 http diveintopython3 org by Mark Pilgrim The slides devoted to that example are thus distributed using the following license http creativecommons org licenses by sa 3 0 3 Side Note 4 Pointer to a Podcast on the topic of Test Driven Development http faceoffshow com 2009 03 31 episode 10 test drivendevelopment Goals Review material from Chapter 8 of Pilone Miles Test Driven Development Terminology Concepts Techniques Tools 5 Test Driven Development 6 An agile practice that asserts that testing is a fundamental part of software development Rather than thinking of testing as something that occurs after implementation we want to think of it as something that occurs BEFORE and DURING implementation Indeed done properly testing can DRIVE implementation The result increased confidence when performing other tasks such as fixing bugs refactoring or reimplementing parts of your software system Testimonial On Monday September 8 2003 at 03 44 PM a former student wrote Dr Anderson I hope you don t mind hearing from former students Remember me from Object Oriented Analysis and Design last spring I m now happily graduated and working in the so called Real World yikes I just wanted to give you another testimony on the real life use of test driven development My co workers are stunned that I am actually using something at work that I learned at school well not really but they like to tease For a new software parsing tool I m developing I decided to use TDD to develop it and it is making my life so easy right now to test new changes Anyways I just thought of you and your class when I decided to use this and I wanted to let you know I hope that you are doing well Best of luck on this new semester 7 Test First 8 The definition of test driven development All production code is written to make failing test cases pass Terminology Production code is code that is deployed to end users and used in their production environments that is there day to day work Implications When developing software we write a test case first watch it fail then write the simplest code to make it pass repeat Example I Consider writing a program to score the game of bowling public class TestGame extends TestCase public void testOneThrow Game g new Game g addThrow 5 assertEquals 5 g getScore When you compile this program the test fails because the Game class does not yet exist But You have defined two methods on the class that you want to use You are designing this class from a client s perspective 9 Example II 10 You would now write the Game class public class Game public void addThrow int pins public int getScore return 0 The code now compiles but the test will still fail getScore returns 0 not 5 In Test Driven Design Beck recommends taking small simple steps So we get the test case to compile before we get it to pass Example III 11 Once we confirm that the test still fails we would then write the simplest code to make the test case pass that would be public class Game public void addThrow int pins public int getScore return 5 The test case now passes Example IV But this code is not very useful Lets add a new test case public class TestGame extends TestCase public void testOneThrow Game g new Game g addThrow 5 assertEquals 5 g getScore public void testTwoThrows Game g new Game g addThrow 5 g addThrow 4 assertEquals 9 g getScore The first test passes but the second case fails since 9 5 This code is written using JUnit it uses reflection to invoke tests automatically 12 Example V 13 We have duplication of information between the first test and the Game class In particular the number 5 appears in both places This duplication occurred because we were writing the simplest code to make the test pass Now in the presence of the second test case this duplication does more harm than good So we must now refactor the code to remove this duplication Example VI public class Game private int score 0 public void addThrow int pins score pins public int getScore return score Both tests now pass Progress 14 Example VII 15 But now we to make additional progress we add another test case to the TestGame class public void testSimpleSpare Game g new Game g addThrow 3 g addThrow 7 g addThrow 3 assertEquals 13 g scoreForFrame 1 assertEquals 16 g getScore We re back to the code not compiling due to scoreForFrame We ll need to add a method body for this method and give it the simplest implementation that will make all three of our tests cases pass TDD Life Cycle The life cycle of test driven development is Quickly add a test Run all tests and see the new one fail Make a simple change Run all tests and see them all pass Refactor to remove duplication This cycle is followed until you have met your goal 16 TDD Life Cycle continued Kent Beck likes to perform TDD using a testing framework such as JUnit Within such frameworks failing tests are indicated with a red bar passing tests are shown with a green bar As such the TDD life cycle is sometimes described as red bar green bar refactor 17 JUnit Red Bar When a test fails You see a red bar Failures Errors are listed Clicking on a failure displays more detailed information about what went wrong 18 Example Background Multi Currency Money 19 Lets design a system that will allow us to perform financial transactions with money that may be in different currencies e g if we know that the exchange rate from Swiss Francs to U S Dollars is 2 to 1 then we can calculate expressions like 5 USD 10 CHF 10 USD or 5 USD 10 CHF 20 CHF Starting From Scratch Lets start developing such an example How do we start TDD recommends writing a list of things we want to test This list can take any format just keep it simple Example 5 10 CHF 10 if rate is 2 1 5 2 10 20 First Test The first test case looks a bit complex lets start with the second 5 USD 2 10 USD First we write a test case public
View Full Document
Unlocking...