1. AMIR BARYLKO
TDD
INTRO
ONLINE BUSINESS SYSTEMS
APRIL 2011
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
2. WHO AM I?
• Quality Expert
• Architect
• Developer
• Mentor
• Great cook
• The one who’s entertaining you for the next hour!
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
3. RESOURCES
• Email: amir@barylko.com
• Twitter: @abarylko
• Blog: http://www.orthocoders.com
• Materials: http://www.orthocoders.com/presentations
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
4. TDD TRAINING
• When: May 26 & 27
• More info: http://www.maventhought.com
• Goal: Learn TDD with real hands on examples
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
5. INTRO
Why projects fail?
Reality Check
No more excuses
Why TDD?
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
6. WHY PROJECTS FAIL?
• Delivering late or over budget
• Delivering the wrong thing
• Unstable in production
• Costly to maintain
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
7. 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 - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
8. 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 - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
9. 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 - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
10. APPLYING TDD
Iteration 0 .. N
Quality as a Driver
Red - Green - Refactor
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
11. ITERATION 0
• Flush out architecture.
• Setup Testing harness for TDD and BDD.
• Setup continuous integration.
• Setup scripts to build, deploy, etc.
• Setup visual communication tools.
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
12. ITERATION 1.. N
• Start by Story Planning
• Pair programming (switching often) Every day!
• Daily Scrum
• End with Retrospective
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
13. QUALITY AS A DRIVER
Red
BDD
Red
Refactor Refactor TDD Green Green
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
14. RED
•Write a test that fails
•Relax, is ok if it compiles
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
15. GREEN
• Try to make the test pass
• Do a simple solution
• Use default values (not throwing exceptions)
• Don’t worry if the code “smells”
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
16. REFACTOR
• Avoid repeating code
• Avoid hardcoding dependencies
• Avoid “write only” code
• Refactor with confidence!
• Run all tests if possible
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
17. DEMO
Unit Testing Frameworks
Given - When - Then
Listing Contents
Reviews
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
18. TESTING FRAMEWORKS
• MsTest, xUnit, nUnit, MbUnit, MSpec
• Similar “hooks”
• Before and after test
• Before and after all test
• Arrange, Act, Assert
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
19. GIVEN WHEN THEN
• Test one “scenario” at a time
• Clear identification of functionality
• Easy to write, understand and maintain
• Repeatable, easy to learn
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
20. LISTING CONTENTS
• Controller “Movies” method “Index”
• Two scenarios
• Given I have no movies....
• Given I have the following movies....
• Where do I get the movies from?
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
21. GIVEN I HAVE NO MOVIES
[It]
public void Should_not_return_any_movies()
{
var result = (ViewResult)this.ActualResult;
var actual = (IEnumerable<IMovie>) result.ViewData.Model;
actual.Should().Be.Empty();
}
[It]
public void Should_render_the_index_view()
{
this.ActualResult.AssertViewRendered();
}
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
22. GIVEN I HAVE 10 MOVIES
[It]
public void Should_return_all_the_movies()
{
var result = (ViewResult)this.ActualResult;
var actual = (IEnumerable<IMovie>) result.ViewData.Model;
actual.Should().Have.SameValuesAs(this._movies);
}
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
23. ARE WE DONE?
• Fail 1: IMovieLibrary is not registered in the container
• Fail 2: SimpleMovieLibrary needs the db name
• How can we make sure it works?
• Is TDD enough?
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
24. FIX THE CONTAINER
Component
.For<IMovieLibrary>()
.Instance(new SimpleMovieLibrary(dbFile))...
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
25. SAFETY NET
Scenario: Browse available movies
Given I have the following movies:
| title |
| Blazing Saddles |
| Space Balls |
When I go to "Movies"
Then I should see in the listing:
| title |
| Blazing Saddles |
| Space Balls |
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
26. SUMMARY
Benefits
Challenges
Adoption
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
27. BENEFITS
• Let the methodology drive
• It will save your bacon!
• High quality the whole way!
• Very few bugs!
• Do your duty as developer!
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
28. CHALLENGES
• Very different from conventional testing
• Many developers find it complex to learn at first
• Hard to start without a Mentor
• Management buy in
• Difficult to keep under deadline pressure
• Beware of code coverage!
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
29. ADOPTION
• Find Mentor/Couch/Trainer
• Small iterations
• Have metrics ready (velocity, etc)
• Do whatever works for you
• Find out which tools will benefit you
• Automate, automate, automate!
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011
33. TDD TRAINING
• When: May 26 & 27
• More info: http://www.maventhought.com
• Goal: Learn TDD with real hands on examples
Amir Barylko - OBS Apr ‘11 MavenThought Inc.
Wednesday, April 27, 2011