5. What is TDD?
• Technique for specifying system features with
tests
• Tests are written before production code
• Simple, repeated, short-cycled mechanism
• Automated mechanism for regression testing
of code changes
6. Why TDD?
• Improves code quality
• Minimises defects
• Increases code maintainability
• Reduces the cost of change
• Provides a quick feedback
• Acts as documentation for your code
• Reduces fear of breaking things
• It is fun and infectious
8. PDCA Explained
• PLAN
– Establish the objectives
– Identify steps required to meet the objective
• DO
– Execute the steps to meet the objective
• CHECK
– Study actual results and compare against expected results
– Look for completeness in meeting the objectives
• ACT
– If CHECK step shows objectives met vs. with the plan,
repeat with next objective else rework and CHECK again
11. What is a Test Framework?
• An abstract set of…
– concepts, processes, procedures, environments
• Where automated tests are…
– designed, implemented and executed
• Includes physical structures for…
– test creation
– test execution
• As well as…
– logical interactions amongst components
12. Why Test Frameworks?
• Provides the basis of test automation
• Simplifies test automation efforts
• Provides concepts and tools that support
automated testing
15. Setting Up
• Required Software
• Download JUnit
• Create Java201 Project
• Add JUnit to the Classpath
• Create HelloGreetingTest
• Run the HelloGreetingTest
• Create the HelloGreeting class
• Re-run the HelloGreetingTest
17. Download JUnit
• Download the following files from
www.junit.org to a folder on your hard drive
– junit.jar
– hamcrest-core.jar
18. Create Java201 Project
• Create new project named Java201
• Create a new source folder named test in the
java201 project
• Create a lib folder in the Java201 project
• In the test source folder, create the
java201.greetings package
19. Add JUnit to the Classpath
• Copy the junit.jar and hamcrest-core.jar files to
the lib folder of your project
• Refresh the project to import the new contents
of the lib folder
• In Eclipse, click on the Java201 project, select
File>Properties>Build Path and click on
Libraries tab
• Click Add JARs, browse to the project lib folder,
select both jar files and click OK twice
20. Create HelloGreetingTest.java
• In Eclipse, Right-click the test folder, select New>JUnit Test Case,
enter HelloGreetingTest in the Name field and click Finish
24. Create HelloGreeting class
• In Eclipse, right-click the src folder and select New>Class, enter
HelloGreeting as class name and click Finish
• Create a sayHello() method that returns the String “Hello, TDD!”
25. Re-run the HelloGreetingTest
• Declare and initialise HelloGreeting object and
assert it returns “Hello, TDD!” and rerun the test
28. Anatomy of a Unit Test
Declaration of
class under test
setUp() runs once
before each test method
A test method
Initialization of
class under test
tearDown() runs once
after each test method
Checking test results
30. Exercise: Game of Cards
• Using TDD, create the following building blocks of a
typical card game
– Card
• Has a suite (Club, Heart, Diamond, Spade) and rank (2-10, Jack,
Queen, King, Ace)
• Ace is highest card
– Hand
• Consists of one or more Cards
– Deck
• Contains 52 Cards, 13 cards per suite
– Dealer
• Deals cards from a deck
• Each player will be dealt a Hand of Cards
31. Card
rank
suite
getRank() : Rank
setRank() : void
getSuite() : Suite
setSuite() : void
Hand
cards
maxCards
getNoOfCards() : int
drawCard(): Card
addCard() : void
Deck
cards
getCards(int noOFCards) :
List<Card>
Dealer
deck
deal(noOfCards) : Hand
Game of Cards Domain Model
Tests Keep you out of the (time hungry) debugger!
Tests Reduce Bugs in New Features
Tests Reduce Bugs in Existing Features
Tests Reduce the Cost of Change
Tests Improve Design
Tests Allow Refactoring
Tests Constrain Features
Tests Defend Against Other Programmers
Testing Is Fun
Testing Forces You to Slow Down and Think
Testing Makes Development Faster
Tests Reduce Fear