writing testable code - GOTO...
Transcript of writing testable code - GOTO...
![Page 1: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/1.jpg)
Writing Testable Code
Alvaro Videla - Cloud Foundry
![Page 2: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/2.jpg)
About Me
• Cloud Foundry Developer Advocate
• Blog: http://videlalvaro.github.com/
• Twitter: @old_sound
![Page 3: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/3.jpg)
About Me
Co-author
RabbitMQ in Action
http://bit.ly/rabbitmq
![Page 4: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/4.jpg)
I’m not a:
![Page 5: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/5.jpg)
I’m not a:
• Application Testing Guru
![Page 6: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/6.jpg)
I’m not a:
• Application Testing Guru
• TDD Advocate
![Page 7: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/7.jpg)
Why is it so hard to write tests?
![Page 8: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/8.jpg)
Unit Testing
The goal of unit testing is to isolate each part of the program and show
that the individual parts are correct
http://en.wikipedia.org/wiki/Unit_testing
![Page 9: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/9.jpg)
Unit Testing
[…] unit testing by definition only tests the functionality of the units
themselves.
http://en.wikipedia.org/wiki/Unit_testing
![Page 10: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/10.jpg)
Unit Testing
[…] Therefore, it will not catch integration errors or broader system-level errors (such as functions performed across
multiple units, or non-functional test areas such as performance)
http://en.wikipedia.org/wiki/Unit_testing
![Page 11: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/11.jpg)
Dogmavs.
Reality
![Page 12: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/12.jpg)
A world of Trade Offs
![Page 13: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/13.jpg)
What should we test?
![Page 14: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/14.jpg)
How much should
we test?
![Page 15: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/15.jpg)
“I get paid for code that works, not for tests, so my philosophy is
to test as little as possible to reach a given level of confidence”
– Kent Beck
http://stackoverflow.com/questions/153234/how-deep-are-your-unit-tests/153565#153565
![Page 16: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/16.jpg)
The Hidden Secret
Of TDD
![Page 17: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/17.jpg)
The Secret of TDD
![Page 18: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/18.jpg)
The Secret of TDD
![Page 19: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/19.jpg)
Some books by Kent Beck
![Page 20: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/20.jpg)
To write good tests first we need
to learn how to program
![Page 21: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/21.jpg)
![Page 22: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/22.jpg)
We developers are like those users we like to complain so
much about
![Page 23: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/23.jpg)
Design evolves and matures with time
![Page 24: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/24.jpg)
Good Code sits in the small details
![Page 25: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/25.jpg)
TIPS
![Page 26: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/26.jpg)
Separate pure code from impure or stateful
![Page 27: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/27.jpg)
Pure Functions
![Page 28: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/28.jpg)
Pure Functions
• Referential Transparency
![Page 29: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/29.jpg)
Pure Functions
• Referential Transparency
• Don’t modify external state
![Page 30: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/30.jpg)
Pure Functions
• Referential Transparency
• Don’t modify external state
• Don’t produce side effects
![Page 31: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/31.jpg)
What’s wrong with this code?
if($player->getScore() > 0) { $player->setSwizzle(7);} else { $player->setSwizzle( $player->getSwizzle() + 1 );}
https://dl.dropboxusercontent.com/u/7810909/docs/what-does-fp-mean/what-does-fp-mean/chunk-html/ar01s05.html
![Page 32: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/32.jpg)
What’s wrong with this code?
$newScore = $player->getScore() > 0 ? 7
: $player->getSwizzle() + 1;
$player->setSwizzle($newScore);
https://dl.dropboxusercontent.com/u/7810909/docs/what-does-fp-mean/what-does-fp-mean/chunk-html/ar01s05.html
![Page 33: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/33.jpg)
Score calculation can be moved into its own function
![Page 34: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/34.jpg)
Score calculation can be tested now
![Page 35: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/35.jpg)
First write Pure Code
![Page 36: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/36.jpg)
Add impure code step by step when
needed
![Page 37: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/37.jpg)
Write Composable
Code
![Page 38: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/38.jpg)
Function Composition
http://en.wikipedia.org/wiki/Function_(mathematics)
![Page 39: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/39.jpg)
Function Composition
http://en.wikipedia.org/wiki/Function_(mathematics)
![Page 40: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/40.jpg)
This looks familiar
![Page 41: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/41.jpg)
“Many UNIX programs do quite trivial tasks in isolation, but,
combined with other programs, become general and useful
tools.”
http://math.albany.edu/math/pers/hammond/unixphil.html
![Page 42: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/42.jpg)
Number of open connections per IP
netstat -ntu | awk '{print $5}' | \cut -d: -f1 | sort | uniq -c | sort -n
http://www.commandlinefu.com/commands/view/1767/number-of-open-connections-per-ip.
![Page 43: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/43.jpg)
Why don’t we justcode in this style?
![Page 44: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/44.jpg)
This seems familiar again…
![Page 45: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/45.jpg)
Welcome to Functional
Programming
![Page 46: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/46.jpg)
“Writing unit tests is reinventingfunctional programming
in non-functional languages”
http://noss.github.io/2009/02/25/writing-unit-tests-is-reinventing-functional-programming-in-non-functional-languages.html
![Page 47: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/47.jpg)
What can we learn from Functional Programming?
![Page 48: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/48.jpg)
The proper use of Types
![Page 49: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/49.jpg)
What does ‘null’ mean?
![Page 50: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/50.jpg)
What does ‘true|false’ mean?
![Page 51: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/51.jpg)
Functions with just one responsibility
![Page 52: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/52.jpg)
Radical separation of pure code from impure code
![Page 53: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/53.jpg)
Let’s see an example
![Page 54: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/54.jpg)
Food for Thought
http://thinking-forth.sourceforge.net
![Page 55: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/55.jpg)
“Inside every well- written large programis a well-written small
program”
http://www.linfo.org/q_programming.html
![Page 56: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/56.jpg)
Questions?
![Page 57: writing testable code - GOTO Conferencegotocon.com/.../slides/AlvaroVidela_WritingTestableCode.pdf · “I get paid for code that works, not for tests, so my philosophy is to test](https://reader034.fdocuments.us/reader034/viewer/2022050200/5f5414b14c18374a7775e77c/html5/thumbnails/57.jpg)
Thanks!http://twitter.com/old_soundhttp://github.com/videlalvaro
http://www.slideshare.net/old_sound