27
Agile Professionals Network Christchurch Test Driven Development Demonstrated Thanks to our sponsors… Alan Christensen @christensena Tom Gutteridge @trgutteridge

TDD - Christchurch APN May 2012

Embed Size (px)

DESCRIPTION

Test Driven Development Intro and demonstration.Delivered at Christchurch Agile Professionals Network on 31 May 2012

Citation preview

Page 1: TDD - Christchurch APN May 2012

Agile Professionals Network Christchurch

Test Driven Development Demonstrated

Thanks to our sponsors…

Alan Christensen @christensenaTom Gutteridge @trgutteridge

Page 2: TDD - Christchurch APN May 2012

22

Page 3: TDD - Christchurch APN May 2012
Page 4: TDD - Christchurch APN May 2012
Page 5: TDD - Christchurch APN May 2012
Page 6: TDD - Christchurch APN May 2012
Page 7: TDD - Christchurch APN May 2012
Page 8: TDD - Christchurch APN May 2012
Page 9: TDD - Christchurch APN May 2012

Who am I?

Page 10: TDD - Christchurch APN May 2012

Format

•Introduction Presentation

•Live coding exercise

•Review slides

•TDD in the real world

•Questions and discussion

Page 11: TDD - Christchurch APN May 2012

Test Driven Development

• It is a development technique

• Incremental (iterative) workflow

• Always write a failing test first (red)

• Only write the minimum code to pass the failing test (green)

• Improve code while all tests are passing (refactor)

http://www.agileapps.co.uk/methodology/continuous.html

Page 12: TDD - Christchurch APN May 2012

Not about testing #1

•Yes you do end up with a suite of tests

•But TDD is a development exercise

•my mindset is coding, not testing

•I will not be exhausting edge cases, etc (at least initially)

Page 13: TDD - Christchurch APN May 2012

Not about testing #2

•By practicing TDD

•I approach my code from the outside, focusing on specifying behaviour

•I can focus on one thing at a time

•My resulting code is inherently designed for testability

•I get fast feedback

Page 14: TDD - Christchurch APN May 2012

Live coding exercise

•Coding Kata

•Wikipedia definition: “A code kata is an exercise in programming which helps hone your skills through practice and repetition”

Page 15: TDD - Christchurch APN May 2012

TDD improves design

•Encourages separation of concerns/single responsibility principle (SRP)

•Incremental, iterative: can lead to simpler designs

•Reduces fear of refactoring, encouraging improvements to design

•Alerts me to design smells

Page 16: TDD - Christchurch APN May 2012

Tests as documentation

•Unit test code provides numerous working examples of API usage

•Good unit test structure and naming can act as a specification

•Tests as documentation always up to date (or the tests are failing)

Page 17: TDD - Christchurch APN May 2012

Objections to TDD

•It takes too much time

•Tests can make it harder to refactor, not easier

•It’s just too hard with our legacy code or un-mockable framework(s)

•Our test suite takes too long to run so we don’t run it very often

Page 18: TDD - Christchurch APN May 2012

Why is TDD Hard?•Mindset seems unnatural at first

•Many frameworks/library components make it difficult to verify behaviour

•Large monolithic blocks of legacy code are hard to isolate

•Good design is not always obvious at first

•Forgetting to test first, not afterwards!

Page 19: TDD - Christchurch APN May 2012

Make TDD easier•Practice Red-Green-Refactor discipline

on simpler examples (e.g. katas) away from a real code base

•Learn from each other (e.g. weekly brown bag/dojo sessions), pair programming

•Learn good design, especially OO design

•Listen to your tests! If it’s getting hard going, consider refactoring your code

•Take it seriously!

Page 20: TDD - Christchurch APN May 2012

Tips: Untestable Frameworks

•Use adapter pattern to "broker" untestable dependencies

•Adapters should be tailored to how they are used by the app. Adapters do not need to expose entire API, only what is needed

•Decouple logic from frameworks (e.g. use MVC pattern)

Page 21: TDD - Christchurch APN May 2012

Productivity with TDD

•It takes practice to become productive

•Test code should be taken seriously, not treated as throwaway

•Keep duplication to a minimum but balance this with readability

•Worst case: your design completely changes resulting in rewrite. You have the old tests as a reference (specs)

Page 22: TDD - Christchurch APN May 2012

What not to TDD?

Page 23: TDD - Christchurch APN May 2012

What is a unit?

•"smallest thing that makes sense" George Dinwiddie

•Sometimes a method, sometimes a collaboration of classes

Page 24: TDD - Christchurch APN May 2012

Unit vs Integration

•Unit tests

•should be fast (fast feedback loop)

•should be focused

•when they fail, they usually point you to where problem is

•rigorous within the component under test

•one assert per test

Page 25: TDD - Christchurch APN May 2012

Integration vs Unit

•Integration tests

•usually slow

•good for verifying unit tested components connect together properly

•good for acceptance tests

•often easier to start with on a legacy code base

Page 26: TDD - Christchurch APN May 2012

Other Topics

•System Under Test isolation (mocks and stubs)

•Real World Unit Testing (or how to deal with existing test unfriendly code)

•Context specification (or how to write expressive, DRY test suites)

•Tips and Tricks, avoiding anti-patterns

Page 27: TDD - Christchurch APN May 2012

Links

•String Calculator kata exercise http://osherove.com/tdd-kata-1/

•Bowling game kata step by step (Powerpoint)http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata