TDD Painkillers

84
http://www.flickr.com/photos/pinksherbet/4004791663/ # TDD Painkillers @Ben_Hall [email protected] Blog.BenHall.me.uk CodeBetter.com/blogs/BenHall

description

Presentation given at DDD Dublin on 9th October 2010. Covers common issues with TDD and ways to over come them.

Transcript of TDD Painkillers

Page 1: TDD Painkillers

http://www.flickr.com/photos/pinksherbet/4004791663/

C# TDD Painkillers@[email protected]/blogs/BenHall

Page 2: TDD Painkillers

WH

O AM

I?London based ASP.net MVPWeb Developer @ 7digital.com

Open Source Developer

Co-Author Testing ASP.net Web Applicationswww.testingaspnet.com

Page 3: TDD Painkillers

YouTest Design App Design

Page 4: TDD Painkillers

http://www.flickr.com/photos/chiotsrun/4115059294/sizes/l/

Page 5: TDD Painkillers

Test Design

http://www.flickr.com/photos/nyuhuhuu/4442144329/

Page 6: TDD Painkillers

TDD doesn’t mean NUnit

Page 7: TDD Painkillers

TDD is about learning

Page 8: TDD Painkillers

TDD is about validating the unknowns

Proving your own assumptions

Page 9: TDD Painkillers

Fundamentally the biggest TDD painkiller is not doing TDD

Page 10: TDD Painkillers

That’s why we have a QA department

Page 11: TDD Painkillers

Developer writing the testDeveloper writing the implementation

Page 12: TDD Painkillers

Do the simplest thing possible.

“First make it work, then make it right”

Page 13: TDD Painkillers

“TDD like you meant it”

Page 14: TDD Painkillers

Spike

http://www.flickr.com/photos/peasap/1625639532/sizes/l/

Page 15: TDD Painkillers

IRB (Ruby)

Page 17: TDD Painkillers

NUnit makes learning repeatable

Helps guide the learning during implementation

Page 18: TDD Painkillers

TEST NAMING AND STRUCTUREPain killer

Page 19: TDD Painkillers

Structure

Page 20: TDD Painkillers
Page 21: TDD Painkillers
Page 22: TDD Painkillers

EXAMPLE

Page 23: TDD Painkillers

EXAMPLE

Page 24: TDD Painkillers

Context \ Spec

Page 25: TDD Painkillers

Naming of test Implementation

Page 26: TDD Painkillers

ACTUAL TEST IMPLEMENTATIONPainkiller

Page 27: TDD Painkillers

Highlight relevant data

Page 28: TDD Painkillers

Duplicated Setup in testEXAMPLE

Page 29: TDD Painkillers

Duplicated Setup in testEXAMPLE

Page 30: TDD Painkillers
Page 31: TDD Painkillers

Tests are documentation

Page 32: TDD Painkillers

IT DEPENDS

Page 33: TDD Painkillers

Given, When, Then

Page 34: TDD Painkillers

Scenario: Payment method has expired Given user John And John has expired credit card When I get the default payment method Then an invalid credit card will be returned

Cuke4nuke \ SpecFlow

Page 35: TDD Painkillers
Page 36: TDD Painkillers

DESIGN SMELLS INDICATE IT’S NOT DONE VIA TDD

Page 37: TDD Painkillers

Random, Dates, Machine Settings• DateTime.Now();• new Random().Next();

Page 38: TDD Painkillers

Use static Func<T>

Page 39: TDD Painkillers

http://www.flickr.com/photos/gagilas/2659695352/

EXTERNAL DEPENDENCIES

Page 40: TDD Painkillers

Fake the other side

Page 41: TDD Painkillers

Mock External Dependencies

Page 42: TDD Painkillers

Anti Corruption

http://www.flickr.com/photos/aresauburnphotos/2678453389/sizes/l/

Page 43: TDD Painkillers

Mock Usage

Page 44: TDD Painkillers

http://ayende.com/Blog/archive/2006/07/02/DoNOTMockSystemData.aspx

Page 45: TDD Painkillers
Page 46: TDD Painkillers

Fragile to refactoring

Page 47: TDD Painkillers

Running away from HttpContext

Page 48: TDD Painkillers

Interfaces establish behaviour boundaries.

Stub\Mock at boundaries

Page 49: TDD Painkillers

Let the tests drive the design

Page 50: TDD Painkillers

Application Design

http://www.flickr.com/photos/yakobusan/2436481628/

Page 51: TDD Painkillers

Dependencies

Page 52: TDD Painkillers

Poor man’s IoC

Page 53: TDD Painkillers

REALLY?!?

Page 54: TDD Painkillers
Page 55: TDD Painkillers
Page 56: TDD Painkillers

Design SmellSmell - Responsibilities

http://www.flickr.com/photos/ricmcarthur/56268640/

Page 57: TDD Painkillers

Tests help indicate smells

Page 58: TDD Painkillers

Tests help indicate smells

Abstract away?

Combined service?

Page 59: TDD Painkillers
Page 60: TDD Painkillers

• Single responsibility principle• Open/closed principle• Liskov substitution principle• Interface segregation principle• Dependency inversion principle

Page 61: TDD Painkillers

UI Testinghttp://blig.ig.com.br/brokenarrow/files/2009/04/too_many_toolbars.jpg

Page 62: TDD Painkillers

WPF Apps With The Model-View-ViewModel Design Pattern

MVVM Pattern

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

Page 63: TDD Painkillers

MVP Pattern

Page 64: TDD Painkillers

MVP Pattern

Page 65: TDD Painkillers

Acceptance Test Driven Development

Page 66: TDD Painkillers

http://codebetter.com/blogs/benhall/archive/2010/03/16/testing-a-wpf-ui-using-ruby-cucumber-and-wipflash-dll.aspx

Page 67: TDD Painkillers

Scenario: Saving a product by name only Given the application has started And I enter the name "Product 1" When I save the product Then "Product 1" should be available to purchase

Page 68: TDD Painkillers

Given /^the application has started$/ do host = ApplicationLauncher.new @app = host.launch Dir.pwd + '/src/ExampleUIs/bin/Debug/ExampleUIs.exe' @main_window = @app.find_window 'petShopWindow' end Given /^I enter the name "([^\"]*)"$/ do |product_name| textbox = @main_window.find_TextBox "petNameInput" textbox.text = product_name end When /^I save the product$/ do button = @main_window.find_Button "petSaveButton" button.click sleep(1) end Then /^"([^\"]*)" should be available to purchase$/ do |product_name| combo = @main_window.find_ComboBox "basketInput" combo.Items.should include("Pet[#{product_name}]") end After do |scenario| @app.process.kill end

Page 69: TDD Painkillers

Full stack testing for ATDD?

Page 70: TDD Painkillers

WHAT’S GOOD APPLICATION DESIGN?

Page 71: TDD Painkillers

Code Coverage

Page 72: TDD Painkillers

Code and Test Reviews

Page 73: TDD Painkillers

7digital XTCR – Cross Team Code Review

Page 74: TDD Painkillers
Page 75: TDD Painkillers

It’s all about the mindset

Page 76: TDD Painkillers

Hardest part == getting started

Page 77: TDD Painkillers

Second hardest part == keep doing it

Page 78: TDD Painkillers

Becomes natural

Page 79: TDD Painkillers

Pair with someone

http://www.flickr.com/photos/jasohill/65804069/sizes/l/

Page 80: TDD Painkillers

Kata

http://www.flickr.com/photos/53511700@N06/4944542750/sizes/l/

Page 81: TDD Painkillers

Fear

http://www.flickr.com/photos/bartt/34937855/

Page 82: TDD Painkillers

http://www.flickr.com/photos/leon_homan/2856628778/

End of the line

Page 83: TDD Painkillers

Summary• Let the tests drive your design• Listen for smells• Fear – beat the lizard brain• just do it!

Page 84: TDD Painkillers

@[email protected]

http://www.flickr.com/photos/mike_c/4780493909/