Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some...

24
Copyright © 2007, Langr Software Solutions. All Rights Reserved. Test-Driven Development vs. Test-After Development Doing TDD Well Jeff Langr Langr Software Solutions http://langrsoft.com

Transcript of Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some...

Page 1: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Test-Driven Development vs. Test-After DevelopmentDoing TDD Well

Jeff LangrLangr Software Solutions

http://langrsoft.com

Page 2: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

2Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Test-Driven Development

A design technique

Page 3: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

3Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Test-after (TAD) vs. Test-first (TDD)• Allows some refactoring• Coverage levels up to ~75%• No direct design impact• Can reduce defects• Can be treated as separate task

• Enables continual refactoring• Coverage approaching 100%• Drives the design• Significantly reduced defects,

debugging cycles• Part of the coding process• Clarifies, documents

understanding of requirements• Continual progress, consistent

pacing• Continual feedback and learning• Sustainable

Unit Testing

Unit testing is:- expensive- never the whole picture

Page 4: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

4Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Doing TDD Well:Some Simple Suggestions

There is no “advanced” TDD

Page 5: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

5Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Doing TDD Well—Think About

• Spec by example• Testability and design• Incrementalism

• Keeping it simple

It's just code!

Page 6: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

6Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Doing TDD Well

Practice

Page 7: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

7Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Doing TDD Well

Pair

Page 8: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

8Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Doing TDD Well

Paraphrase

Page 9: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

9Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Test everything– Use integration tests if necessary, but minimize– Don't avoid tests for difficult challenges– Decompose tougher problems; isolate complexity

Page 10: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

10Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Fail firstTake smaller steps than you are now

Page 11: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

11Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Run all the tests10-minute rule

– Discard code– Requires fast tests

• Unit vs. integration isn't as important as fast vs. slow

Page 12: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

12Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Keep the build green

Page 13: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

13Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Refactor zealously– Little things matter– Tests too– Consider “2nd time” instead of “3rd

time” refactoring

writer.write(headerText); writer.newLine(); writer.write(detailText); writer.newLine();

writeLine(writer, headerText); writeLine(writer, detailText);

private void writeLine( BufferedWriter writer, String text) throws IOException { writer.write(text); writer.newLine(); }

Page 14: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

14Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Don't forget OO 101– Very small single-responsibility classes– Minimized coupling

SomeClass

+methodA+methodB+methodC+methodD-methoda-methodb

AClass

+methodA

BClass

+methodB

AnotherClass

+methoda

BnotherClass

+methodb

CDClass

Page 15: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

15Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Heed cohesion in tests– Decrease asserts per test

• But don't insist on “always one”– Build tests around behavior/cases, not methods– Build fixtures around common setup

Page 16: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

16Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Rename continually

@Test public void something()@Test public void create()@Test public void defaultCreate()@Test public void isEmptyOnDefaultCreation()

Page 17: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

17Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Don't overuse mocks– But don't refuse to use them– Mocks tightly couple tests to production code

• Can violate encapsulation• Can inhibit refactoring

Page 18: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

18Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Use good toolsMaster them

Page 19: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

19Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Don't code for the futureThink and act hard about the presentKeep a to-do list

– Scrap by check-in, task, day, iteration end

Page 20: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

20Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Never be blocked! (Uncle Bob's “prime directive”)Abstract away volatilityDecouple from othersDon't wait for definition

- Start the feedback loop- Start the process

Page 21: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

21Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Read– Books

• Test Driven: TDD and Acceptance TDD for Java Developers, Lasse Koskela

• xUnit Test Patterns, Gerard Meszaros• Implementation Patterns, Kent Beck

– Articles– Yahoo! groups

• testdrivendevelopment, extremeprogramming, JUnit, etc.

Page 22: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

22Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Work as part of a team– Look for standards– Welcome opportunities to review– Talk frequently

Page 23: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

23Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Remain humble– Keep an open mind– Be willing to back up and take a different approach– Be willing to change how you develop– Revisit failures, to learn– Get some rest

Page 24: Test-Driven Development vs. Test-After …Test-after (TAD) vs. Test-first (TDD) • Allows some refactoring • Coverage levels up to ~75% • No direct design impact • Can reduce

24Copyright © 2007, Langr Software Solutions. All Rights Reserved.

Doing TDD Well

TDD is a skill.

Practice, practice, practice.