prdc11-tdd-common-mistakes

37
(track sponsor) Amir Barylko Common TDD Mistakes & Pitfalls

Upload: amir-barylko

Post on 11-May-2015

324 views

Category:

Technology


0 download

DESCRIPTION

Presentat

TRANSCRIPT

Page 1: PRDC11-tdd-common-mistakes

(track sponsor)

Amir Barylko

Common TDD

Mistakes &Pitfalls

Page 2: PRDC11-tdd-common-mistakes

Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.

AMIR BARYLKOCOMMON TDD

MISTAKES & PITFALLSPRAIRIE DEV CON

REGINA 2011

Page 3: PRDC11-tdd-common-mistakes

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!

Page 4: PRDC11-tdd-common-mistakes

Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.

INTROWhy projects fail?

Reality CheckNo more excuses

Why TDD?

Page 5: PRDC11-tdd-common-mistakes

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

Page 6: PRDC11-tdd-common-mistakes

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.

Page 7: PRDC11-tdd-common-mistakes

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!

Page 8: PRDC11-tdd-common-mistakes

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!

Page 9: PRDC11-tdd-common-mistakes

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

Page 10: PRDC11-tdd-common-mistakes

Amir Barylko - Common mistakes and pitfalls MavenThought Inc.

THE LIST

Page 11: PRDC11-tdd-common-mistakes

Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.

TEST IDENTIFICATION

• Unit

• Integration

• Acceptance

• Which one should I use?

• What are the limits?

Page 12: PRDC11-tdd-common-mistakes

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

Page 13: PRDC11-tdd-common-mistakes

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

Page 14: PRDC11-tdd-common-mistakes

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

Page 15: PRDC11-tdd-common-mistakes

Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.

ONE SCENARIO PER TEST

• Easy to approach

• Easy to understand

• Easy to maintain

Page 16: PRDC11-tdd-common-mistakes

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()}

Page 17: PRDC11-tdd-common-mistakes

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

Page 18: PRDC11-tdd-common-mistakes

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()

}

Page 19: PRDC11-tdd-common-mistakes

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()

}

Page 20: PRDC11-tdd-common-mistakes

Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.

WHO’S YOUR DD?

• Quality Driver

• Use the methodology

• Gain confidence

• Don’t show off

Page 21: PRDC11-tdd-common-mistakes

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(); } }

Page 22: PRDC11-tdd-common-mistakes

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? } }}

Page 23: PRDC11-tdd-common-mistakes

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?

Page 24: PRDC11-tdd-common-mistakes

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()}

Page 25: PRDC11-tdd-common-mistakes

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

Page 26: PRDC11-tdd-common-mistakes

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?

Page 27: PRDC11-tdd-common-mistakes

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

Page 28: PRDC11-tdd-common-mistakes

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?

Page 29: PRDC11-tdd-common-mistakes

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

Page 30: PRDC11-tdd-common-mistakes

Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.

DEPENDENCIES

• Hardcoded dependencies

• Unit or integration test?

• Isolation

Page 31: PRDC11-tdd-common-mistakes

Amir Barylko - TDD Mistakes & Pitfalls MavenThought Inc.

SOMETHING SMELLS

public class MovieLibrary {

private TwitterNotifier _notifier;

public MovieLibrary() {

this._notifier = new TwitterNotifier();

}

}

Page 32: PRDC11-tdd-common-mistakes

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

Page 33: PRDC11-tdd-common-mistakes

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?

Page 34: PRDC11-tdd-common-mistakes

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>(...)

Page 35: PRDC11-tdd-common-mistakes

Amir Barylko - Common mistakes and pitfalls MavenThought Inc.

QUESTIONS?

Page 36: PRDC11-tdd-common-mistakes

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.

Page 37: PRDC11-tdd-common-mistakes

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