prdc11-tdd-common-mistakes
DESCRIPTION
PresentatTRANSCRIPT
(track sponsor)
Amir Barylko
Common TDD
Mistakes &Pitfalls
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
AMIR BARYLKOCOMMON TDD
MISTAKES & PITFALLSPRAIRIE DEV CON
REGINA 2011
Amir 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.
INTROWhy projects fail?
Reality CheckNo 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 maintain
Amir 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!
• It was like that when I got here!
• The previous developer didn’t know XXXX!
• We need a satellite connection in order to run it!
• We can’t reproduce the error!
• We can’t test that!
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
WHY TDD?
•Prove that your code works
•Avoid waste (debugging)
• Increment code quality
•Better design
•Regression tests as byproduct
•Make changes with confidence
•Bring back the joy of coding!
Amir 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 test
Amir Barylko - Common mistakes and pitfalls MavenThought Inc.
THE LIST
Amir Barylko - TDD Mistakes & 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 dependencies
Amir 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 testing
Amir 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 values
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
ONE SCENARIO PER TEST
• Easy to approach
• Easy to understand
• Easy to maintain
Amir 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.Initialize1.1.Library should be empty
2.Addingd Movies
2.1.Contents are changes
2.2.Event is triggered
3.Listing NV movies
3.1.Missing Critic
3.2.Listing only NV
Amir 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 off
Amir 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
• Don’t think about what’s next
• One bit of code at a time
• Let the technology drive
• Now, make it fail!
public class Cell{ public bool Alive { get { return xxxx? } }}
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 otherwise
Amir 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 locally
Amir 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 modifications
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
DEPENDENCIES
• Hardcoded dependencies
• Unit or integration test?
• Isolation
Amir 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 maintain
Amir 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
• Avoid duplication and repetition
• Generic Parameters
• Parameters Factories
• Random strings
• Random numbers
[Row(1)][Row(2)]void Method(int arg)
[Factory(...)]void Method(string arg)
void Method([Random]...)
void Method([Random]..., [Factory]...)
[Row(typeof(...))]void Method<T>(...)
Amir Barylko - Common mistakes and pitfalls MavenThought Inc.
QUESTIONS?
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
RESOURCES
• Contact me: [email protected], @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.com