xpdays 2016 tdd

26
XPDays 2016 Michael König, Blue Yonder GmbH Test-Driven Development: Wann? Wie? Wieso?

Upload: nguyenkhuong

Post on 14-Feb-2017

220 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: XPDays 2016 TDD

XPDays 2016Michael König, Blue Yonder GmbH

Test-Driven Development:Wann? Wie? Wieso?

Page 2: XPDays 2016 TDD

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

There ‘s tribblesEverywhere!

©Paramount Pictures

Page 4: XPDays 2016 TDD

§ 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

This far,

No further!

©Paramount Pictures

Page 6: XPDays 2016 TDD

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

§ 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

§ 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

§ 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

Hands-On Session

©Paramount Pictures

Page 11: XPDays 2016 TDD

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

I ‘m SENSINGconfusing emotions

©Paramount Pictures

Page 13: XPDays 2016 TDD

§ 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

§ 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

§ Steady sense of progress

§ Ease of mind

§ Courage

TDD boosts your work life

Page 16: XPDays 2016 TDD

What could possiblygo wrong?

©Paramount Pictures

Page 17: XPDays 2016 TDD

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

§ Can’t say no

§ Overly complex tests

§ Parrots

§ Riddles

§ Nitroglycerine

§ Mocking hell

§ Refactoring clamps

Tests to avoid

Page 19: XPDays 2016 TDD

§ 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

To boldly do TDD

where no one has done before

©Paramount Pictures

Page 21: XPDays 2016 TDD

§ 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

Debriefing

©Paramount Pictures

Page 23: XPDays 2016 TDD

§ 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

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

§ 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

Without TDD,your Code hasno honor!

©Paramount Pictures