Successfully reported this slideshow.

Making friends with TDD

404 views

Published on

Published in: Technology, Business
  • Be the first to comment

Making friends with TDD

  1. 1. Making Friends withTDDJohn ClearyLead Web Developer, DocNet@TheRealBifter
  2. 2. So, What is TDD?
  3. 3. 10 Write one test20 Run tests (they fail)30 Write code to make test pass40 Run tests again (they pass)50 Refactor (code AND tests)60 Run test (they still pass)70 Goto 10
  4. 4. RedGreenRefactor
  5. 5. RedGreenRefactorWrite
  6. 6.  a
  7. 7.  failing
  8. 8.  test
  9. 9. RedGreenRefactorWrite
  10. 10.  a
  11. 11.  failing
  12. 12.  testFix
  13. 13.  the
  14. 14.  test
  15. 15. RedGreenRefactorWrite
  16. 16.  a
  17. 17.  failing
  18. 18.  testFix
  19. 19.  the
  20. 20.  testMake
  21. 21.  it
  22. 22.  pretty
  23. 23. Why do it?
  24. 24. 1. Improved Code Design
  25. 25. Classes that are easy to test are by definition decoupled.1. Improved Code Design
  26. 26. Classes that are easy to test are by definition decoupled.Encourages refactoring1. Improved Code Design
  27. 27. 2. Verification
  28. 28. Fewer bugs2. Verification
  29. 29. Fewer bugsEasier to maintain2. Verification
  30. 30. Awesome - let’s do it, right?
  31. 31. Awesome - let’s do it, right?Wait!
  32. 32. Does it take longer?
  33. 33. Does it take longer?Maybe, maybe not.
  34. 34. Is it harder?
  35. 35. Is it harder?For sure!
  36. 36. But it wrecks my creativity,dude?
  37. 37. But it wrecks my creativity,dude?Really? Do some prototypes man!
  38. 38. Could we get these benefitswithout TDD?
  39. 39. Could we get these benefitswithout TDD?No, but yeah, but no...
  40. 40. Awesome, lets do it!(for real this time)
  41. 41. Recently Used List Class
  42. 42. Recently Used List ClassDevelop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.
  43. 43. Recently Used List ClassA recently-used-list is initially empty.Develop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.
  44. 44. Recently Used List ClassA recently-used-list is initially empty.The most recently added item is first, the least recently addeditem is last.Develop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.
  45. 45. Recently Used List ClassA recently-used-list is initially empty.The most recently added item is first, the least recently addeditem 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. 46. Recently Used List ClassA recently-used-list is initially empty.The most recently added item is first, the least recently addeditem is last.Items can be looked up by index, which counts from zero.Items in the list are unique, so duplicate insertions are movedrather than added.Develop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.
  47. 47. Recently Used List ClassA recently-used-list is initially empty.The most recently added item is first, the least recently addeditem is last.Items can be looked up by index, which counts from zero.Items in the list are unique, so duplicate insertions are movedrather than added.Develop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.
  48. 48. Recently Used List ClassA recently-used-list is initially empty.The most recently added item is first, the least recently addeditem is last.Items can be looked up by index, which counts from zero.Items in the list are unique, so duplicate insertions are movedrather than added.Develop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.
  49. 49. Recently Used List ClassA recently-used-list is initially empty.The most recently added item is first, the least recently addeditem is last.Items can be looked up by index, which counts from zero.Items in the list are unique, so duplicate insertions are movedrather than added.Develop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.
  50. 50. Recently Used List ClassA recently-used-list is initially empty.The most recently added item is first, the least recently addeditem is last.Items can be looked up by index, which counts from zero.Items in the list are unique, so duplicate insertions are movedrather than added.Develop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.
  51. 51. Refactor
  52. 52.  to
  53. 53.  this
  54. 54. That wasn’t so hard...
  55. 55. That wasn’t so hard......but what about testing the persistence layer?
  56. 56. You’ve got options
  57. 57. Mock the database layerDBUnitCodeceptionYou’ve got options
  58. 58. Mock the database layerDBUnitCodeceptionYou’ve got optionsThe
  59. 59.  purist
  60. 60.  way
  61. 61. Mock the database layerDBUnitCodeceptionYou’ve got optionsThe
  62. 62.  purist
  63. 63.  wayThe
  64. 64.  easy
  65. 65.  way
  66. 66. Mock the database layerDBUnitCodeceptionYou’ve got optionsThe
  67. 67.  purist
  68. 68.  wayThe
  69. 69.  easy
  70. 70.  wayThe
  71. 71.  even
  72. 72.  easier
  73. 73.  way
  74. 74. Mock Examplefunction 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• ComplicatedBut good because• Super fast• Not tied to database structure
  75. 75. DBUnit Examplepublic 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 resultsBut good because• Tests are easy to write and read• It’s obvious whats being tested
  76. 76. Codeception Example?phpfunction testSavingUser(){$user = new User();$user-setName(Miles);$user-save();$this-codeGuy-seeInDatabase(users,array(name = Miles));}?Not great because• Slow• Tied to database structureBut good because• Less fragile than DBUnit (no xml to maintain)• Tests are easy to write and read• It’s obvious whats being tested
  77. 77. When testing the DB...
  78. 78. When testing the DB...Give each developer their own test database (preferably ontheir own VM)
  79. 79. When testing the DB...Give each developer their own test database (preferably ontheir own VM)DB tests are slow - move them outside the main suite or usememory tables
  80. 80. When testing the DB...Give each developer their own test database (preferably ontheir own VM)DB tests are slow - move them outside the main suite or usememory tablesUse a tool like “Migrations” to keep databases in sync
  81. 81. UI Testing
  82. 82. UI TestingPHPUnit with Selenium
  83. 83. UI TestingPHPUnit with SeleniumCodeception
  84. 84. So, where do we go from here?
  85. 85. 1. Have a gowww.cyber-dojo.com
  86. 86. 2. Build up slowly
  87. 87. 2. Build up slowlyStart with one afternoon per week
  88. 88. 2. Build up slowlyStart with one afternoon per weekTry pair programming
  89. 89. 3. Motivate your team
  90. 90. 3. Motivate your teamStart a Coding Club / Kata
  91. 91. 3. Motivate your teamStart a Coding Club / Kata[Get your boss to] buy some books
  92. 92. 3. Motivate your teamStart a Coding Club / Kata[Get your boss to] buy some booksSee
  93. 93.  me
  94. 94.  after
  95. 95.  this
  96. 96.  talk
  97. 97. 4. Get a CI serverJenkinsTravisCruiseControl
  98. 98. 5. Share
  99. 99. 5. ShareAttend public Katas
  100. 100. 5. ShareAttend public KatasForums IRC
  101. 101. Questions ?
  102. 102. Thank YouJohn ClearyLead Web Developer, DocNet@TheRealBifter

×