SlideShare a Scribd company logo
1 of 93
Download to read offline
Making Friends with
TDD
John Cleary
Lead Web Developer, DocNet
@TheRealBifter
So, What is TDD?
10 Write one test
20 Run tests (they fail)
30 Write code to make test pass
40 Run tests again (they pass)
50 Refactor (code AND tests)
60 Run test (they still pass)
70 Goto 10
Red
GreenRefactor
Red
GreenRefactor
Write
 a
 failing
 test
Red
GreenRefactor
Write
 a
 failing
 test
Fix
 the
 test
Red
GreenRefactor
Write
 a
 failing
 test
Fix
 the
 test
Make
 it
 pretty
Why do it?
1. Improved Code Design
Classes that are easy to test are by definition decoupled.
1. Improved Code Design
Classes that are easy to test are by definition decoupled.
Encourages refactoring
1. Improved Code Design
2. Verification
Fewer bugs
2. Verification
Fewer bugs
Easier to maintain
2. Verification
Awesome - let’s do it, right?
Awesome - let’s do it, right?
Wait!
Does it take longer?
Does it take longer?
Maybe, maybe not.
Is it harder?
Is it harder?
For sure!
But it wrecks my creativity,
dude?
But it wrecks my creativity,
dude?
Really? Do some prototypes man!
Could we get these benefits
without TDD?
Could we get these benefits
without TDD?
No, but yeah, but no...
Awesome, let's do it!
(for real this time)
Recently Used List Class
Recently Used List Class
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
Recently Used List Class
A recently-used-list is initially empty.
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
Recently Used List Class
A recently-used-list is initially empty.
The most recently added item is first, the least recently added
item is last.
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
Recently Used List Class
A recently-used-list is initially empty.
The most recently added item is first, the least recently added
item is last.
Items can be looked up by index, which counts from zero.
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
Recently Used List Class
A recently-used-list is initially empty.
The most recently added item is first, the least recently added
item is last.
Items can be looked up by index, which counts from zero.
Items in the list are unique, so duplicate insertions are moved
rather than added.
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
Recently Used List Class
A recently-used-list is initially empty.
The most recently added item is first, the least recently added
item is last.
Items can be looked up by index, which counts from zero.
Items in the list are unique, so duplicate insertions are moved
rather than added.
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
Recently Used List Class
A recently-used-list is initially empty.
The most recently added item is first, the least recently added
item is last.
Items can be looked up by index, which counts from zero.
Items in the list are unique, so duplicate insertions are moved
rather than added.
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
Recently Used List Class
A recently-used-list is initially empty.
The most recently added item is first, the least recently added
item is last.
Items can be looked up by index, which counts from zero.
Items in the list are unique, so duplicate insertions are moved
rather than added.
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
Recently Used List Class
A recently-used-list is initially empty.
The most recently added item is first, the least recently added
item is last.
Items can be looked up by index, which counts from zero.
Items in the list are unique, so duplicate insertions are moved
rather than added.
Develop a recently-used-list class to hold strings uniquely in Last-
In-First-Out order.
Refactor
 to
 this
That wasn’t so hard...
That wasn’t so hard...
...but what about testing the persistence layer?
You’ve got options
Mock the database layer
DBUnit
Codeception
You’ve got options
Mock the database layer
DBUnit
Codeception
You’ve got options
The
 purist
 way
Mock the database layer
DBUnit
Codeception
You’ve got options
The
 purist
 way
The
 easy
 way
Mock the database layer
DBUnit
Codeception
You’ve got options
The
 purist
 way
The

More Related Content

What's hot

Scala In The Wild
Scala In The WildScala In The Wild
Scala In The Wild
djspiewak
 
Lambda functions in java 8
Lambda functions in java 8Lambda functions in java 8
Lambda functions in java 8
James Brown
 
Maven TestNg frame work (1) (1)
Maven TestNg frame work (1) (1)Maven TestNg frame work (1) (1)
Maven TestNg frame work (1) (1)
Gopi Raghavendra
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
Joe Zulli
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented Php
Michael Girouard
 

What's hot (20)

Scala In The Wild
Scala In The WildScala In The Wild
Scala In The Wild
 
Code Like Pythonista
Code Like PythonistaCode Like Pythonista
Code Like Pythonista
 
Lambda functions in java 8
Lambda functions in java 8Lambda functions in java 8
Lambda functions in java 8
 
Practically Functional
Practically FunctionalPractically Functional
Practically Functional
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
Maven TestNg frame work (1) (1)
Maven TestNg frame work (1) (1)Maven TestNg frame work (1) (1)
Maven TestNg frame work (1) (1)
 
Java 101 Intro to Java Programming - Exercises
Java 101   Intro to Java Programming - ExercisesJava 101   Intro to Java Programming - Exercises
Java 101 Intro to Java Programming - Exercises
 
Linked to ArrayList: the full story
Linked to ArrayList: the full storyLinked to ArrayList: the full story
Linked to ArrayList: the full story
 
06 Java Language And OOP Part VI
06 Java Language And OOP Part VI06 Java Language And OOP Part VI
06 Java Language And OOP Part VI
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
Scala For Java Programmers
Scala For Java ProgrammersScala For Java Programmers
Scala For Java Programmers
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 
Java SE 8 best practices
Java SE 8 best practicesJava SE 8 best practices
Java SE 8 best practices
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented Php
 
Java 102
Java 102Java 102
Java 102
 
Java 7 New Features
Java 7 New FeaturesJava 7 New Features
Java 7 New Features
 
The Ring programming language version 1.5.1 book - Part 13 of 180
The Ring programming language version 1.5.1 book - Part 13 of 180The Ring programming language version 1.5.1 book - Part 13 of 180
The Ring programming language version 1.5.1 book - Part 13 of 180
 
Fire in the type hole
Fire in the type holeFire in the type hole
Fire in the type hole
 

Viewers also liked

Pfo on sar options paper february 2012(2)
Pfo on sar options paper february 2012(2)Pfo on sar options paper february 2012(2)
Pfo on sar options paper february 2012(2)
Bob Vansickle
 

Viewers also liked (6)

What is the Internet (CHS Year 6)
What is the Internet (CHS Year 6)What is the Internet (CHS Year 6)
What is the Internet (CHS Year 6)
 
Summary of merging ow and odsp and option paper feb 6 2012
Summary of merging ow and odsp and option paper feb 6 2012Summary of merging ow and odsp and option paper feb 6 2012
Summary of merging ow and odsp and option paper feb 6 2012
 
Pfo on sar options paper february 2012(2)
Pfo on sar options paper february 2012(2)Pfo on sar options paper february 2012(2)
Pfo on sar options paper february 2012(2)
 
User stories
User storiesUser stories
User stories
 
Getting Things Done
Getting Things DoneGetting Things Done
Getting Things Done
 
Capistrano - automate all the things
Capistrano - automate all the thingsCapistrano - automate all the things
Capistrano - automate all the things
 

Similar to Making friends with TDD

Rspec Tips
Rspec TipsRspec Tips
Rspec Tips
lionpeal
 

Similar to Making friends with TDD (20)

TDD Training
TDD TrainingTDD Training
TDD Training
 
Scala in the Wild
Scala in the WildScala in the Wild
Scala in the Wild
 
Laravelcollectionsunraveled
LaravelcollectionsunraveledLaravelcollectionsunraveled
Laravelcollectionsunraveled
 
Mastering Python lesson 5a_lists_list_operations
Mastering Python lesson 5a_lists_list_operationsMastering Python lesson 5a_lists_list_operations
Mastering Python lesson 5a_lists_list_operations
 
ECET 370 Invent Yourself/newtonhelp.com
ECET 370 Invent Yourself/newtonhelp.comECET 370 Invent Yourself/newtonhelp.com
ECET 370 Invent Yourself/newtonhelp.com
 
Completely Test-Driven
Completely Test-DrivenCompletely Test-Driven
Completely Test-Driven
 
Beginning linq
Beginning linqBeginning linq
Beginning linq
 
Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium
 
Dynamic Python
Dynamic PythonDynamic Python
Dynamic Python
 
Java basics
Java basicsJava basics
Java basics
 
ECET 370 Success Begins/Newtonhelp.com
ECET 370 Success Begins/Newtonhelp.comECET 370 Success Begins/Newtonhelp.com
ECET 370 Success Begins/Newtonhelp.com
 
ECET 370 HELPS Redefined Education--ecet370helps.com
ECET 370 HELPS Redefined Education--ecet370helps.comECET 370 HELPS Redefined Education--ecet370helps.com
ECET 370 HELPS Redefined Education--ecet370helps.com
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
ECET 370 HELPS Education Counseling--ecet370helps.com
ECET 370 HELPS  Education Counseling--ecet370helps.comECET 370 HELPS  Education Counseling--ecet370helps.com
ECET 370 HELPS Education Counseling--ecet370helps.com
 
Rspec Tips
Rspec TipsRspec Tips
Rspec Tips
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
Test automation with selenide
Test automation with selenideTest automation with selenide
Test automation with selenide
 
Ch5 beeing an application
Ch5   beeing an applicationCh5   beeing an application
Ch5 beeing an application
 
Javasession6
Javasession6Javasession6
Javasession6
 
The Road to Lambda - Mike Duigou
The Road to Lambda - Mike DuigouThe Road to Lambda - Mike Duigou
The Road to Lambda - Mike Duigou
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 

Making friends with TDD

  • 1. Making Friends with TDD John Cleary Lead Web Developer, DocNet @TheRealBifter
  • 2. So, What is TDD?
  • 3. 10 Write one test 20 Run tests (they fail) 30 Write code to make test pass 40 Run tests again (they pass) 50 Refactor (code AND tests) 60 Run test (they still pass) 70 Goto 10
  • 6.  a
  • 10.  a
  • 13.  the
  • 16.  a
  • 19.  the
  • 21.  it
  • 25. Classes that are easy to test are by definition decoupled. 1. Improved Code Design
  • 26. Classes that are easy to test are by definition decoupled. Encourages refactoring 1. Improved Code Design
  • 29. Fewer bugs Easier to maintain 2. Verification
  • 30. Awesome - let’s do it, right?
  • 31. Awesome - let’s do it, right? Wait!
  • 32. Does it take longer?
  • 33. Does it take longer? Maybe, maybe not.
  • 36. But it wrecks my creativity, dude?
  • 37. But it wrecks my creativity, dude? Really? Do some prototypes man!
  • 38. Could we get these benefits without TDD?
  • 39. Could we get these benefits without TDD? No, but yeah, but no...
  • 40. Awesome, let's do it! (for real this time)
  • 42. Recently Used List Class Develop a recently-used-list class to hold strings uniquely in Last- In-First-Out order.
  • 43. Recently Used List Class A recently-used-list is initially empty. Develop a recently-used-list class to hold strings uniquely in Last- In-First-Out order.
  • 44. Recently Used List Class A recently-used-list is initially empty. The most recently added item is first, the least recently added item is last. Develop a recently-used-list class to hold strings uniquely in Last- In-First-Out order.
  • 45. Recently Used List Class A recently-used-list is initially empty. The most recently added item is first, the least recently added item is last. Items can be looked up by index, which counts from zero. Develop a recently-used-list class to hold strings uniquely in Last- In-First-Out order.
  • 46. Recently Used List Class A recently-used-list is initially empty. The most recently added item is first, the least recently added item is last. Items can be looked up by index, which counts from zero. Items in the list are unique, so duplicate insertions are moved rather than added. Develop a recently-used-list class to hold strings uniquely in Last- In-First-Out order.
  • 47. Recently Used List Class A recently-used-list is initially empty. The most recently added item is first, the least recently added item is last. Items can be looked up by index, which counts from zero. Items in the list are unique, so duplicate insertions are moved rather than added. Develop a recently-used-list class to hold strings uniquely in Last- In-First-Out order.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58. Recently Used List Class A recently-used-list is initially empty. The most recently added item is first, the least recently added item is last. Items can be looked up by index, which counts from zero. Items in the list are unique, so duplicate insertions are moved rather than added. Develop a recently-used-list class to hold strings uniquely in Last- In-First-Out order.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67. Recently Used List Class A recently-used-list is initially empty. The most recently added item is first, the least recently added item is last. Items can be looked up by index, which counts from zero. Items in the list are unique, so duplicate insertions are moved rather than added. Develop a recently-used-list class to hold strings uniquely in Last- In-First-Out order.
  • 68.
  • 69.
  • 70. Recently Used List Class A recently-used-list is initially empty. The most recently added item is first, the least recently added item is last. Items can be looked up by index, which counts from zero. Items in the list are unique, so duplicate insertions are moved rather than added. Develop a recently-used-list class to hold strings uniquely in Last- In-First-Out order.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 77.  to
  • 79. That wasn’t so hard...
  • 80. That wasn’t so hard... ...but what about testing the persistence layer?
  • 82. Mock the database layer DBUnit Codeception You’ve got options
  • 83. Mock the database layer DBUnit Codeception You’ve got options The
  • 85.  way
  • 86. Mock the database layer DBUnit Codeception You’ve got options The
  • 90.  way
  • 91. Mock the database layer DBUnit Codeception You’ve got options The
  • 98.  way
  • 99. Mock Example function testInsertDatabase() { $insertArray = array( 'name' = 'Miles', ); // create a mock database $mock = $this-getMockBuilder('Zend_Db_Adapter_Pdo_Mysql', array('insert')) -disableOriginalConstructor() -getMock(); // mock out the insert method $mock -expects($this-once()) -method('insert') -with($insertArray); // pass the database to the container $container = new DIContainer(): $container-set('database', $mock); // run the test $user = new User(); $user-setName('Miles'); $user-save(); } Not great because • Requires Dependency Injection • Can be fragile • Complicated But good because • Super fast • Not tied to database structure
  • 100. DBUnit Example public function testCanCreateNewUser() { $user = new User(); $user-setName('Miles'); $user-save(); $queryTable = $this-getConnection()-createTableQuery( 'users', 'SELECT * from users' ); $expectedTable = $this-createFlatXmlDataSet(users-expected.xml) -getTable('users'); $this-assertTableEquals($expectedTable, $queryTable); } Not great because • Slow • Can be fragile • Tied to database structure • Requires xml setup of expected results But good because • Tests are easy to write and read • It’s obvious whats being tested
  • 101. Codeception Example ?php function testSavingUser() { $user = new User(); $user-setName('Miles'); $user-save(); $this-codeGuy-seeInDatabase('users',array('name' = 'Miles')); } ? Not great because • Slow • Tied to database structure But good because • Less fragile than DBUnit (no xml to maintain) • Tests are easy to write and read • It’s obvious whats being tested
  • 103. When testing the DB... Give each developer their own test database (preferably on their own VM)
  • 104. When testing the DB... Give each developer their own test database (preferably on their own VM) DB tests are slow - move them outside the main suite or use memory tables
  • 105. When testing the DB... Give each developer their own test database (preferably on their own VM) DB tests are slow - move them outside the main suite or use memory tables Use a tool like “Migrations” to keep databases in sync
  • 108. UI Testing PHPUnit with Selenium Codeception
  • 109. So, where do we go from here?
  • 110. 1. Have a go www.cyber-dojo.com
  • 111. 2. Build up slowly
  • 112. 2. Build up slowly Start with one afternoon per week
  • 113. 2. Build up slowly Start with one afternoon per week Try pair programming
  • 115. 3. Motivate your team Start a Coding Club / Kata
  • 116. 3. Motivate your team Start a Coding Club / Kata [Get your boss to] buy some books
  • 117. 3. Motivate your team Start a Coding Club / Kata [Get your boss to] buy some books See
  • 118.  me
  • 120.  this
  • 121.  talk
  • 122. 4. Get a CI server Jenkins Travis CruiseControl
  • 125. 5. Share Attend public Katas Forums IRC
  • 127. Thank You John Cleary Lead Web Developer, DocNet @TheRealBifter