XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie?...
Transcript of XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie?...
![Page 1: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/1.jpg)
XPDays 2016Michael König, Blue Yonder GmbH
Test-Driven Development:Wann? Wie? Wieso?
![Page 2: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/2.jpg)
Confessions of a Physicist
Condi-tionals
Errors
Loops
Special cases
Print output
Print temp
Print input
Fix input
Write function Run program
Works?Debug
program
Destroy evidence!
no
yes
![Page 3: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/3.jpg)
There ‘s tribblesEverywhere!
©Paramount Pictures
![Page 4: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/4.jpg)
§ Change!§ Bug§ Feature§ Performance
Would you kindly…
§ Mommy!§ Unclear behavior§ New & old bugs§ Manual testing§ Not my code!§ Wait, that was me?!
“Never change a running system”
“Fix nothing which ain’t broken”
![Page 5: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/5.jpg)
This far,
No further!
©Paramount Pictures
![Page 6: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/6.jpg)
Test-driven development
Assert output
Control input
Errors
Special cases
Mocks & Fakes
Write minimal failing test
Pass test with minimal (production) code change
Refactor tests and production code
Red
Green
Refactor
![Page 7: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/7.jpg)
§ Minimal!§ Prevents complexity
§ Execute all tests§ Prevents slow tests
§ Assert new test fails§ Prevents inactive tests§ Prevents bugs in tests§ Prevents complexity
Red: Write minimal failing test
Minimal means:• Missing import• Missing class• Missing function• One assertion a time• Simple to complex• Error cases first• Corner cases next• General behavior last
![Page 8: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/8.jpg)
§ Minimal!§ Prevents missing tests
§ Execute all tests§ Prevents slow tests
§ Assert all test succeed§ Prevents bugs in code§ Prevents bugs in tests
Green: Pass test with minimal change
Minimal means:• Add file stub• Add class stub• Add function stub• Unconditionally raise• Hard-coded results• Correctly sized results• Defer conditionals• Defer loops
![Page 9: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/9.jpg)
§ Remove superseded tests§ Better signal/noise ratio
§ Clean code principles§ Reduce complexity
§ Execute all tests§ Prevents slow tests§ Prevents refactoring bugs§ Prevents brittle tests
Refactor: Clean up test/production code
Principles• DRY• SRP• SLA• KISS• POLA• LoD
![Page 10: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/10.jpg)
Hands-On Session
©Paramount Pictures
![Page 11: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/11.jpg)
Hands-on session: Roman numerals
§ Task description & Python quickstart athttps://github.com/blue-yonder/tdd_exercise
§ Virtual environment recommended
> git clone https://github.com/blue-yonder/tdd_exercise> cd tdd_exercise> python setup.py test
![Page 12: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/12.jpg)
I ‘m SENSINGconfusing emotions
©Paramount Pictures
![Page 13: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/13.jpg)
§ Babysteps… really?§ Not necessarily§ Write failing test§ Write obvious implementation
§ TDD lets you work as fast as you can
Is TDD that painfully slow?
“The best race drivers know when to brake”
![Page 14: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/14.jpg)
§ Impact on code§ Modular design§ Cleaner code§ Less bugs
§ Impact on tests§ Full automation§ 100% coverage§ Executable specs
TDD boosts your code
![Page 15: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/15.jpg)
§ Steady sense of progress
§ Ease of mind
§ Courage
TDD boosts your work life
![Page 16: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/16.jpg)
What could possiblygo wrong?
©Paramount Pictures
![Page 17: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/17.jpg)
Lots of things, apparently
§ Die 10 goldenen Regeln für schlechte Tests§ Tilmann Glaser, Peter Fichtner
§ Wann soll ich mocken?§ David Völkel
![Page 18: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/18.jpg)
§ Can’t say no
§ Overly complex tests
§ Parrots
§ Riddles
§ Nitroglycerine
§ Mocking hell
§ Refactoring clamps
Tests to avoid
![Page 19: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/19.jpg)
§ Split§ Helpers for setup / assertions§ Hand-picked examples§ Express intent in names§ Eliminate all randomness§ Prefer fakes/stubs over mocks
§ Improve production code design
§ Last resort: Drop
Countermeasures
![Page 20: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/20.jpg)
To boldly do TDD
where no one has done before
©Paramount Pictures
![Page 21: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/21.jpg)
§ Prototype code§ Quickly moving target without perspective
§ Performance optimizations§ Non-functional, without prior expectation
§ Concurrent programming§ Hard to control
§ Declarative code§ How more complex than what
Be honest: Is TDD perfect for anything?
![Page 22: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/22.jpg)
Debriefing
©Paramount Pictures
![Page 23: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/23.jpg)
§ Great for§ Functional correctness§ Black/white situations§ Production code§ Single-threaded code§ Non-declarative code
§ Don’t be dogmatic about it
TDD: One tool in your belt
![Page 24: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/24.jpg)
Test-driven development
Write minimal failing test
Pass test with minimal (production) code change
Refactor tests and production code
Red
Green
Refactor
![Page 25: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/25.jpg)
§ Test-driven development by example§ by Kent Beck
§ Code Katas:§ Poker hand classification§ Hangman game§ Roman to Arabic numerals§ …
Further material
![Page 26: XPDays 2016 TDD · XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso? Confessions of a Physicist Condi-tionals Errors Loops Special cases Print](https://reader034.fdocuments.us/reader034/viewer/2022042322/5f0cf4dd7e708231d437f712/html5/thumbnails/26.jpg)
Without TDD,your Code hasno honor!
©Paramount Pictures