Test-Driven Development : Test-Driven Development Jeff Langr
Langr Software Solutions
http://langrsoft.com
http://agileInAFlash.com
Agile : 2 Agile Continually delivering quality software
that responds to changing business needs
Terminology : 3 Terminology Driving the development of a system via tests
Can refer to what the customer team does
Story test-driven development (story TDD)?
Acceptance test-driven development (ATDD)?
More typically refers to what the programming team does
We're focusing on the latter
Uncle Bob's Three Rules of TDD : 4 Uncle Bob's Three Rules of TDD Write no production code except to make a failing unit test pass.
Write no more of a unit test than is sufficient to fail.
Compilation failures count as failures.
Write no more production code than to pass the failing unit test. Source: http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd
Test-Driven Development : 5 Test-Driven Development An incremental design technique for programmers
A First Test : 6 A First Test public class PortfolioTest {
@Test
public void containsNoHoldingsOnCreation() {
Portfolio portfolio = new Portfolio();
assertEquals(0, portfolio.numberOfHoldings());
}
}
Another Test... : 7 Another Test... @Test
public void storesSharesForPurchase() {
portfolio.purchase("MSFT", 1);
assertEquals(1, portfolio.numberOfHoldings());
}
...Fleshed Out... : 8 ...Fleshed Out... @Test
public void storesSharesForPurchase() {
portfolio.purchase("MSFT", 5);
assertEquals(1, portfolio.numberOfHoldings());
assertEquals(5, portfolio.sharesOf("MSFT"));
}
...And Cleaned Up : 9 ...And Cleaned Up @Test
public void storesSharesForPurchase() {
portfolio.purchase("MSFT", 5);
assertEquals(1, portfolio.numberOfHoldings());
assertEquals(5, portfolio.sharesOf("MSFT"));
}
More Tests to Write : 10 More Tests to Write sumsSharesForMultiplePurchasesSameSymbol
separatesSharesBySymbol
disallowsPurchaseWithEmptySymbol
disallowsPurchaseWithInvalidSymbol
...
Think About : 11 Think About Specification by example
Testability and design
Incrementalism
Confidence to change It's just code!
Test-after (TAD) vs. Test-first (TDD)? : 12 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 Benefits of Unit testing is:
- expensive
- never the whole picture
Existing Systems : 13 Existing Systems Not designed for testing
Lack of ability to refactor
“Working Effectively With Legacy Code”
Michael Feathers
Excuses : 14 Excuses “Takes twice as long”
“Have to maintain test and code”
“Not all that valuable—tough work is in the 'wiring'”
“It's unprofessional to unit test to the exclusion of all other activities”
Also see http://c2.com/cgi/wiki?ObjectionsToWorkingTestFirst
Costs : 15 Costs Initial learning curve
Continual learning: “how do I test X?”
Some things can't be tested easily/effectively
Can promote overconfidence
Maintaining tests is work
Doing TDD Well : 16 Doing TDD Well Practice
Pair
and/or
Paraphrase
Introducing TDD : 17 Introducing TDD Ensure your build system supports it
Classroom training only gets you so far
Coaching essential
Developers have to convince selves of benefits
Management has to buy long-term benefits
Can't discard it in crunch time
Don't govern by metrics alone
Use metrics to point to problem areas
Slide 18 : 18 “We switched our development style practically overnight.
That's not to say that we had it all figured out right away, but that we could tell there was no going back.”
—Jerry Jackson