160 likes | 387 Views
Introduction to Test Driven Development. What is TDD?. It is an iterative development technique Focused on design rather than validation Also referred as: Test first development. TDD effects. Improve and speed up software development Have flexible and extensible code
E N D
What is TDD? • It is an iterativedevelopmenttechnique • Focused on design ratherthanvalidation • Alsoreferred as: Testfirstdevelopment
TDD effects • Improve and speedup software development • Haveflexible and extensiblecode • Reducebugs • Improvedocumentation
TDD mantra • Write testbeforeimplementation • Refactor, refactor, refactor • Relies on unittesting and unittestingframeworks • ”Unittestsrunfast, iftheydonotrunfasttheyarenotunittests”
TDD: step 1 • Think in terms of baby steps • Focus on the behaviouryouwishyourcodewillhave THINK
TDD: step 2 • Write a test, watchitfail • Focus on the behaviouryourclass and methodwillhave • A.K.A. nameyour API properly THINK FAIL
TDD: step 3 • Write the code, makeitpass • Write the minimalamount of codethatmakes the testpass THINK FAIL PASS
TDD: step 4 • Simplify and improve the design • Do NOT modify the behaviour! THINK FAIL PASS REFACTOR
TDD cycle THINK FAIL PASS REFACTOR
Limitations • Steeplearningcurve • Itrequiresdiscipline to conform to the process • Hard to apply to GUI development • Hard to apply to legacycode
References • Kent Beck: Testdrivendevelopmentbyexample • Dave Astels: Testdrivendevelopment, a practivalguide
Exercise For this exercise you will implement as simplified version of a tennis game using Test-driven development: • A game is won by the first player to have won at least four points in total and at least two points more than the opponent. • The running score of each game is described in a manner peculiar to tennis: scores from zero to three points are described as "love", "fifteen", "thirty", and "forty" respectively. • If at least three points have been scored by each player, and the scores are equal, the score is "deuce". • If at least three points have been scored by each side and a player has one more point than his opponent, the score of the game is "advantage" for the player in the lead.