(track sponsor) Common   TDDMistakes &  PitfallsAmir Barylko
AMIR BARYLKO                     COMMON TDD                   MISTAKES & PITFALLS                                         ...
WHO AM I?  • Architect  • Developer  • Mentor  • Great      cook  • The     one who’s entertaining you for the next hour!A...
INTRO                                         Why projects fail?                                          Reality Check   ...
WHY PROJECTS FAIL?  • Delivering            late or over budget  • Delivering            the wrong thing  • Unstable      ...
REALITY CHECK  • It is impossible to gather all the requirements at     the beginning of a project.  • Whatever   requirem...
NO MORE EXCUSES  • It   works on my computer!           • We need a satellite                                          con...
WHY TDD?  • Prove         that your code         • Regression   tests as     works                                byproduc...
RED GREEN REFACTOR  • Always       start with red  • Minimum          amount of code to get green  • Refactor       if nec...
THE LISTAmir Barylko - Common mistakes and pitfalls      MavenThought Inc.
TEST IDENTIFICATION  • Unit  • Integration  • Acceptance  • Which       one should I use?  • What       are the limits?Ami...
UNIT  public class When_movie_library_adds_a_movie  {      public void Should_include_all_the_movies_in_the_contents()  } ...
INTEGRATION  public class When_movie_library_sends_a_tweet  {      public void Should_send_a_tweet_with_new_movies()  }  •...
ACCEPTANCE  Scenario:      Get presenters as JSON    Given I      have some speakers at the conference    When I       get...
ONE SCENARIO PER TEST  • Easy     to approach  • Easy     to understand  • Easy     to maintainAmir Barylko - TDD Mistakes...
EVERYTHING TOGETHER  [TestFixture]  public class MovieLibraryTest  {      [Test]      public void When_Initialized_Should_...
SCENARIO IDENTIFICATION  1.Initialize                           3.Listing NV movies    1.1.Library should be empty        ...
ADDING MOVIES  public class When_movie_library_adds_a_movie  {         public void Should_include_the_movie()         publ...
LISTING NV MOVIES  public class When_movie_library_list_nv_with_no_critic  {         public void Should_throw_missing_crit...
WHO’S YOUR DD?  • Quality      Driver  • Use     the methodology  • Gain     confidence  • Don’t      show offAmir Barylko ...
THE TEST  public class When_creating_cells  {      public void Should_not_be_alive()      {          var cell = new Cell()...
MAKE IT PASS  • Minimum          amount of code      public class Cell                                         {  • Don’t ...
BEHAVIOUR VERIFICATION  • Test    setters and getters  • Verify     the dependencies called  • Test    private members  • ...
TESTING THE CALL  public class When_movie_library_lists_nv_movies  {      public void Should_return_all_the_nv_movies()   ...
SPECIFY BEHAVIOUR  • Avoid      testing implementation  • Setup      all the behaviour  • Find    the scenario that fails ...
LACK OF AUTOMATION  • Who       runs the test?  • Who’s       in charge of deployment?  • Who       enforces policies on c...
ITERATION 0  • Run     all the tests since day one  • Commit         should trigger build  • Build     should trigger test...
LEGACY CODE  • The     code is a mess  • Can’t     refactor easily  • No     documentation  • No     testing  • Who       ...
WHAT TO DO?  • Acceptance          tests to document current features  • Migrations        to capture current database  • ...
DEPENDENCIES  • Hardcoded           dependencies  • Unit     or integration test?  • IsolationAmir Barylko - TDD Mistakes ...
SOMETHING SMELLS  public class MovieLibrary {          private TwitterNotifier _notifier;          public MovieLibrary() {...
EXTRACT DEPENDENCIES  public class MovieLibrary {         private INotifier _notifier;         public MovieLibrary(INotifi...
RANDOM VALUES  • Are     u kidding?  • How       do I know are the right values?  • How       do I know is not going to fa...
TEST WITH PARAMETERS                                         [Row(1)]  • Avoid   duplication and              [Row(2)]    ...
QUESTIONS?Amir Barylko - Common mistakes and pitfalls   MavenThought Inc.
RESOURCES  • Contact        me: amir@barylko.com, @abarylko  • Download: http://www.orhtocoders.com/presentations  • Books...
RESOURCES II  • NUnit: http://www.nunit.org  • Gallio     & MbUnit: http://www.gallio.org  • MavenThought Testing: http://...
Upcoming SlideShare
Loading in...5
×

PRDC11-tdd-common-mistakes

355

Published on

Presentat

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
355
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

PRDC11-tdd-common-mistakes

  1. 1. (track sponsor) Common TDDMistakes & PitfallsAmir Barylko
  2. 2. AMIR BARYLKO COMMON TDD MISTAKES & PITFALLS PRAIRIE DEV CON REGINA 2011Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  3. 3. WHO AM I? • Architect • Developer • Mentor • Great cook • The one who’s entertaining you for the next hour!Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  4. 4. INTRO Why projects fail? Reality Check No more excuses Why TDD?Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  5. 5. WHY PROJECTS FAIL? • Delivering late or over budget • Delivering the wrong thing • Unstable in production • Costly to maintainAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  6. 6. REALITY CHECK • It is impossible to gather all the requirements at the beginning of a project. • Whatever requirements you do gather are guaranteed to change. • There will always be more to do than time and money will allow.Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  7. 7. NO MORE EXCUSES • It works on my computer! • We need a satellite connection in order to • It was like that when I got run it! here! • We can’t reproduce the • The previous developer error! didn’t know XXXX! • We can’t test that!Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  8. 8. WHY TDD? • Prove that your code • Regression tests as works byproduct • Avoid waste • Makechanges with (debugging) confidence • Increment code quality • Bring back the joy of coding! • Better designAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  9. 9. RED GREEN REFACTOR • Always start with red • Minimum amount of code to get green • Refactor if necessary • Gain confidence • When in doubt, write a testAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  10. 10. THE LISTAmir Barylko - Common mistakes and pitfalls MavenThought Inc.
  11. 11. TEST IDENTIFICATION • Unit • Integration • Acceptance • Which one should I use? • What are the limits?Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  12. 12. UNIT public class When_movie_library_adds_a_movie { public void Should_include_all_the_movies_in_the_contents() } • One class • One method • No dependenciesAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  13. 13. INTEGRATION public class When_movie_library_sends_a_tweet { public void Should_send_a_tweet_with_new_movies() } • Two or more classes • Validate interaction between classes • Still white box testingAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  14. 14. ACCEPTANCE Scenario: Get presenters as JSON Given I have some speakers at the conference When I get "speakers" as JSON Then I should get a response with all the speakers • Black box testing • Manipulate the application • Populate the database with expected data • Validate using the application valuesAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  15. 15. ONE SCENARIO PER TEST • Easy to approach • Easy to understand • Easy to maintainAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  16. 16. EVERYTHING TOGETHER [TestFixture] public class MovieLibraryTest { [Test] public void When_Initialized_Should_Be_Empty() [Test] public void When_Adding_Should_Appear_In_The_Contents() [Test] public void When_Adding_Should_Trigger_Event() [Test] public void When_ListingNV_Should_Ask_Critic() [Test] public void When_ListingNV_Should_Return_All_NV() [Test] public void When_ListingNV_Should_Throw_Exception_If_Missing_Critic() }Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  17. 17. SCENARIO IDENTIFICATION 1.Initialize 3.Listing NV movies 1.1.Library should be empty 3.1.Missing Critic 2.Addingd Movies 3.2.Listing only NV 2.1.Contents are changes 2.2.Event is triggeredAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  18. 18. ADDING MOVIES public class When_movie_library_adds_a_movie { public void Should_include_the_movie() public void Should_notify_an_element_was_added() }Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  19. 19. LISTING NV MOVIES public class When_movie_library_list_nv_with_no_critic { public void Should_throw_missing_critic_exception() } public class When_movie_library_lists_nv_movies { public void Should_return_all_the_nv_movies() }Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  20. 20. WHO’S YOUR DD? • Quality Driver • Use the methodology • Gain confidence • Don’t show offAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  21. 21. THE TEST public class When_creating_cells { public void Should_not_be_alive() { var cell = new Cell(); cell.Alive.Should().Be.False(); } }Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  22. 22. MAKE IT PASS • Minimum amount of code public class Cell { • Don’t think about what’s public bool Alive { next get { return xxxx? } } • One bit of code at a time } • Let the technology drive • Now, make it fail!Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  23. 23. BEHAVIOUR VERIFICATION • Test setters and getters • Verify the dependencies called • Test private members • How do I know when’s enough?Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  24. 24. TESTING THE CALL public class When_movie_library_lists_nv_movies { public void Should_return_all_the_nv_movies() public void Should_call_the_critic_3_times() }Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  25. 25. SPECIFY BEHAVIOUR • Avoid testing implementation • Setup all the behaviour • Find the scenario that fails • Test calls if can’t be verified otherwiseAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  26. 26. LACK OF AUTOMATION • Who runs the test? • Who’s in charge of deployment? • Who enforces policies on commit? • Where’s the documentation? • Do I need the IDE in order to build?Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  27. 27. ITERATION 0 • Run all the tests since day one • Commit should trigger build • Build should trigger test • Test may trigger deploy • Scripts should run locallyAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  28. 28. LEGACY CODE • The code is a mess • Can’t refactor easily • No documentation • No testing • Who dares to make a change?Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  29. 29. WHAT TO DO? • Acceptance tests to document current features • Migrations to capture current database • Migrations to capture changes and population • Unit test works better for new modificationsAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  30. 30. DEPENDENCIES • Hardcoded dependencies • Unit or integration test? • IsolationAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  31. 31. SOMETHING SMELLS public class MovieLibrary { private TwitterNotifier _notifier; public MovieLibrary() { this._notifier = new TwitterNotifier(); } }Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  32. 32. EXTRACT DEPENDENCIES public class MovieLibrary { private INotifier _notifier; public MovieLibrary(INotifier notifier) { this._notifier = notifier; } } • Introduces dependency in the constructor / setter • Easy to test and maintainAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  33. 33. RANDOM VALUES • Are u kidding? • How do I know are the right values? • How do I know is not going to fail the next one?Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  34. 34. TEST WITH PARAMETERS [Row(1)] • Avoid duplication and [Row(2)] repetition void Method(int arg) [Row(typeof(...))] • Generic Parameters void Method<T>(...) • Parameters Factories [Factory(...)] void Method(string arg) • Random strings void Method([Random]...) • Random numbers void Method([Random]..., [Factory]...)Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  35. 35. QUESTIONS?Amir Barylko - Common mistakes and pitfalls MavenThought Inc.
  36. 36. RESOURCES • Contact me: amir@barylko.com, @abarylko • Download: http://www.orhtocoders.com/presentations • Books: The rSpec book, xUnit Patterns.Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  37. 37. RESOURCES II • NUnit: http://www.nunit.org • Gallio & MbUnit: http://www.gallio.org • MavenThought Testing: http://maventcommons.codeplex.com • Rhino Mocks: http://www.ayende.com • StructureMap: http://structuremap.sourcefore.com • TeamCity: http://www.jetbrains.comAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×