+ All Categories
Home > Documents > writing testable code - GOTO...

writing testable code - GOTO...

Date post: 18-Jul-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
57
Writing Testable Code Alvaro Videla - Cloud Foundry
Transcript
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

“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

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

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

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

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

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
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

“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

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

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

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

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

“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

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

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

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

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

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

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

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

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

“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

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

Thanks!http://twitter.com/old_soundhttp://github.com/videlalvaro

http://www.slideshare.net/old_sound


Recommended