TDD
or: How I Learned to Stop Worrying and Love Tests
Vera Reynolds
software dev @ pillar
@vecho_ralfa
- story
- beginning - write for hours, hope it works
- incorrect assumptions
- errors
- hours hunting the bug
- not only that
- fix one thing - break another
- how do people do it with large programs??
- you must get better at this over time
Test-Driven Development
Wiki: Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) automated test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards.
- simple process, hard to sum up
- what you want before how to do it
history
Kent Beck & XP
- 60s - mercury space program - punch card tdd
- 1999 - XP Explained
- 2002 - TDD By Example
- automated tests
- beck - sUnit - smalltalk
- beck - jUnit - java
history
2001: agile manifesto
- Uncle Bob, Martin Fowler, Alistair Cockburn, Kent Beck...
- heavyweight waterfall
- mid 90s - lightweight agile
- tdd -> xp -> agile
history
2001: agile manifesto
- value both sides
- how do you get to working software and embrace change?
waterfall
- throw over the wall
- you found a bug a month later?
- same day?
"Walking on water and developing software from a specification are easy if both are frozen." - Edward V Berard
- requirements change
- business doesn't know WHAT it wants until it's done
- devs don't know HOW to build it until it's done
agile
- evolve requirements over time - often
- deliver incrementally - often
- fail fast/often/small - cheaper
- embrace change
different levels of testing
- unit test
- integration test
- functional test
- acceptance test
- user test
exploratory testing, system testing, release testing...
- unit isolation / SRS
- integrate units
- functional (solution) - end to end
- acceptance (problem) - customer specs, given when then, black box
- (accept vs func) software is a solution to a problem
- Verification: Are we building the product right?
- Validation: Are we building the right product?
- definitions vary. test all levels
anantomy of a unit test
- setup
- prepare
- execute
- assert
- teardown
good unit test
- repeatable
- isolated
- automated
tdd
red green refactor
write a failing test
write enough code to make the test pass
refactor
repeat
- unit - what, tdd - when
- beck - 10 mins or less, automatic
- REFACTOR - incremental (evolutionary) design
- code = creative writing
- uncle bob - test more specific -> code more generic
- regression
- JAVA EXAMPLE
BDD
Behaviour-Driven Development
Dan North
- where to start?
- what to test?
- how much to test in one go?
- what to name a test?
- why does the test fail?
- UK author
- too much emphasis no TEST
- not a testing technique, a design technique
BDD
semantics
test: calling the 'refresh' function reloads the item list array from the database
behavior: clicking the 'refresh' button should display the most recent item in the list
- north - JBehave - replacement for jUnit
- decoupling implementation (what from how)
- easier refactoring
- cross-team communication - common language
- bdd = tdd done right?
- JS EXAMPLE
tools
- xUnit
- Jasmine
- mock/fake/stub/spy
so, always tdd, right?
...depends on who you ask
Robert Martin (Uncle Bob):
- physical boundary
- fiddling layer
- test support code
- abstract logic into testable layers
- depends on task, team, experience
- price - more lines
- benefit (when done right) - modular decoupled codebase
- coverage is a nice side-effect
is tdd dead?
David Heinemeier Hansson (DHH) - April 2014
- TDD is dead. Long live testing.
Discussion with Martin Fowler and Kent Beck
- Rails, Basecamp
- problems: heavy mocking, cumbersome design
- ruby is too dynamic
summary
- one of core practices in Agile/XP
- unit test = what, tdd = when
- red - green - refactor
- bdd = tdd