2. AMIR BARYLKO
COMMON TDD
MISTAKES & PITFALLS
PRAIRIE DEV CON
REGINA 2011
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
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. INTRO
Why projects fail?
Reality Check
No more excuses
Why TDD?
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
5. 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.
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. 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. 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 design
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
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 test
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
11. TEST IDENTIFICATION
• Unit
• Integration
• Acceptance
• Which one should I use?
• What are the limits?
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
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 dependencies
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
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 testing
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
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 values
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
15. ONE SCENARIO PER TEST
• Easy to approach
• Easy to understand
• Easy to maintain
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
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. 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 triggered
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
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. 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. WHO’S YOUR DD?
• Quality Driver
• Use the methodology
• Gain confidence
• Don’t show off
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
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. 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. 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. 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. 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.
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. 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.
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. 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.
30. DEPENDENCIES
• Hardcoded dependencies
• Unit or integration test?
• Isolation
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
31. SOMETHING SMELLS
public class MovieLibrary {
private TwitterNotifier _notifier;
public MovieLibrary() {
this._notifier = new TwitterNotifier();
}
}
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
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 maintain
Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.
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. 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.