Test-Driven Development Introduction
Oleksii Fedorov
Software Craftsperson
@ Pivotal Labs - Berlin
@waterlink000
3 Rules of TDD
You are not allowed to write a line of production code unless test fails.
You are not allowed to write more of a test, that is sufficient to fail.
You are not allowed to write more production code, that is sufficient to make the test pass.
Red -> Green -> Refactoring
-
Red by writing a failing test
-
Green by writing enough of a production code to pass it
- Spend most of your time in Refactoring phase
- Repeat
You shape your system in refactoring phase, while keeping all the tests green.Benefits of TDD
- Maintainability, Flexibility, Extensibility
- Unparalleled Test Coverage
- Clean API
- Refactoring - 1st-class citizen
- Executable low-level documentation
- Long Term Speed Improvements
simple, elegant, modular code. bugs are found earlier (cheaper to fix). encourages end-user-point-of-view.Drawbacks of TDD
- Tests require maintenance
- Hard in legacy code
- Not trivial to do well
- Slower start
- Team needs to buy it
Tests keep code healthy -> they need care. The challenge to cover spaghetti code with tests. Going for the gold problem.My Personal Experience with TDD
3 years in and not going back!
Green & Refactoring = Energy Boost
energy, motivation, happinessNo Debugging
Maybe 2 hours per year total debugging time.Zero Fear
Code as a Modeling Clay.
I am not afraid to change the code and refactor anything in any way. It allows me to work with code as if it was a modeling clay (Plastilin).Enables Me & My Team
./ship instead of git push.
QA Process
$ ./test && ./ship
Finished in 7.02343 seconds
17611 test cases, 0 failures
Your ship has sailed!
Summary | Q&A
Failing test first.
Only enough of a test to fail.
Only enough of a code to pass.
RED -> GREEN -> REFACTORING -> ...
Benefits vs Potential Drawbacks
Fun & Confidence
img {
height: 320px;
float: left;
}
Test-Driven Development Introduction