TDD Painkillers
description
Transcript of TDD Painkillers
http://www.flickr.com/photos/pinksherbet/4004791663/
C# TDD Painkillers@[email protected]/blogs/BenHall
WH
O AM
I?London based ASP.net MVPWeb Developer @ 7digital.com
Open Source Developer
Co-Author Testing ASP.net Web Applicationswww.testingaspnet.com
YouTest Design App Design
http://www.flickr.com/photos/chiotsrun/4115059294/sizes/l/
Test Design
http://www.flickr.com/photos/nyuhuhuu/4442144329/
TDD doesn’t mean NUnit
TDD is about learning
TDD is about validating the unknowns
Proving your own assumptions
Fundamentally the biggest TDD painkiller is not doing TDD
That’s why we have a QA department
Developer writing the testDeveloper writing the implementation
Do the simplest thing possible.
“First make it work, then make it right”
“TDD like you meant it”
Spike
http://www.flickr.com/photos/peasap/1625639532/sizes/l/
IRB (Ruby)
http://github.com/BenHall/CastleDynamicProxy101/blob/master/CastleDynamicProxy101/Program.cs
http://github.com/BenHall/memcached101/blob/master/MemcacheDemo/Program.cs
NUnit makes learning repeatable
Helps guide the learning during implementation
TEST NAMING AND STRUCTUREPain killer
Structure
EXAMPLE
EXAMPLE
Context \ Spec
Naming of test Implementation
ACTUAL TEST IMPLEMENTATIONPainkiller
Highlight relevant data
Duplicated Setup in testEXAMPLE
Duplicated Setup in testEXAMPLE
Tests are documentation
IT DEPENDS
Given, When, Then
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
DESIGN SMELLS INDICATE IT’S NOT DONE VIA TDD
Random, Dates, Machine Settings• DateTime.Now();• new Random().Next();
Use static Func<T>
http://www.flickr.com/photos/gagilas/2659695352/
EXTERNAL DEPENDENCIES
Fake the other side
Mock External Dependencies
Anti Corruption
http://www.flickr.com/photos/aresauburnphotos/2678453389/sizes/l/
Mock Usage
http://ayende.com/Blog/archive/2006/07/02/DoNOTMockSystemData.aspx
Fragile to refactoring
Running away from HttpContext
Interfaces establish behaviour boundaries.
Stub\Mock at boundaries
Let the tests drive the design
Application Design
http://www.flickr.com/photos/yakobusan/2436481628/
Dependencies
Poor man’s IoC
REALLY?!?
Design SmellSmell - Responsibilities
http://www.flickr.com/photos/ricmcarthur/56268640/
Tests help indicate smells
Tests help indicate smells
Abstract away?
Combined service?
• Single responsibility principle• Open/closed principle• Liskov substitution principle• Interface segregation principle• Dependency inversion principle
UI Testinghttp://blig.ig.com.br/brokenarrow/files/2009/04/too_many_toolbars.jpg
WPF Apps With The Model-View-ViewModel Design Pattern
MVVM Pattern
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
MVP Pattern
MVP Pattern
Acceptance Test Driven Development
http://codebetter.com/blogs/benhall/archive/2010/03/16/testing-a-wpf-ui-using-ruby-cucumber-and-wipflash-dll.aspx
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
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
Full stack testing for ATDD?
WHAT’S GOOD APPLICATION DESIGN?
Code Coverage
Code and Test Reviews
7digital XTCR – Cross Team Code Review
It’s all about the mindset
Hardest part == getting started
Second hardest part == keep doing it
Becomes natural
Pair with someone
http://www.flickr.com/photos/jasohill/65804069/sizes/l/
Kata
http://www.flickr.com/photos/53511700@N06/4944542750/sizes/l/
http://www.flickr.com/photos/leon_homan/2856628778/
End of the line
Summary• Let the tests drive your design• Listen for smells• Fear – beat the lizard brain• just do it!
http://www.flickr.com/photos/mike_c/4780493909/