PRDC11-tdd-common-mistakes
Upcoming SlideShare
Loading in...5
×
 

PRDC11-tdd-common-mistakes

on

  • 524 views

Presentat

Presentat

Statistics

Views

Total Views
524
Views on SlideShare
524
Embed Views
0

Actions

Likes
0
Downloads
5
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

PRDC11-tdd-common-mistakes PRDC11-tdd-common-mistakes Presentation Transcript

  • (track sponsor) Common TDDMistakes & PitfallsAmir Barylko
  • AMIR BARYLKO COMMON TDD MISTAKES & PITFALLS PRAIRIE DEV CON REGINA 2011Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  • 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.
  • INTRO Why projects fail? Reality Check No more excuses Why TDD?Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  • WHY PROJECTS FAIL? • Delivering late or over budget • Delivering the wrong thing • Unstable in production • Costly to maintainAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  • 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.
  • 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.
  • 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.
  • 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.
  • THE LISTAmir Barylko - Common mistakes and pitfalls MavenThought Inc.
  • TEST IDENTIFICATION • Unit • Integration • Acceptance • Which one should I use? • What are the limits?Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  • 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.
  • 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.
  • 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.
  • ONE SCENARIO PER TEST • Easy to approach • Easy to understand • Easy to maintainAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  • 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.
  • 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.
  • 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.
  • 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.
  • WHO’S YOUR DD? • Quality Driver • Use the methodology • Gain confidence • Don’t show offAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • DEPENDENCIES • Hardcoded dependencies • Unit or integration test? • IsolationAmir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  • SOMETHING SMELLS public class MovieLibrary { private TwitterNotifier _notifier; public MovieLibrary() { this._notifier = new TwitterNotifier(); } }Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
  • 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.
  • 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.
  • 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.
  • QUESTIONS?Amir Barylko - Common mistakes and pitfalls MavenThought Inc.
  • 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.
  • 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.