+ All Categories
Home > Technology > Writing testable code

Writing testable code

Date post: 12-May-2015
Category:
Upload: alvaro-videla
View: 4,610 times
Download: 0 times
Share this document with a friend
Description:
We've been told many times that we should write unit tests for our code. We have read the theory and we have applied automatic testing to our projects, sometimes successfully but often times not so. Why it seems to be so hard to test our code? However we look at it, automatic testing doesn't work like a "plug & play" peripheral. It just doesn't seem to fit with our project. A dependency is missing here; we have a hard to mock object there; and so on. What is _that_ thing we might be doing wrong but we fail to notice? In this talk we will argue that the problem lays in our code, in its structure, in the way we pass data around and even how we write for loops! This won't be your everyday "code quality" tech talk, since we are going to attack the problem of code quality from different points of view and paradigms like Functional Programming and the Unix philosophy of simplicity and reuse.
Popular Tags:
57
Writing Testable Code Alvaro Videla - Cloud Foundry Wednesday, April 10, 13
Transcript
Page 1: Writing testable code

Writing Testable Code

Alvaro Videla - Cloud Foundry

Wednesday, April 10, 13

Page 2: Writing testable code

About Me

• Cloud Foundry Developer Advocate

• Blog: http://videlalvaro.github.com/

• Twitter: @old_sound

Wednesday, April 10, 13

Page 3: Writing testable code

About Me

Co-author

RabbitMQ in Action

http://bit.ly/rabbitmq

Wednesday, April 10, 13

Page 4: Writing testable code

I’m not a:

Wednesday, April 10, 13

Page 5: Writing testable code

I’m not a:

• Application Testing Guru

Wednesday, April 10, 13

Page 6: Writing testable code

I’m not a:

• Application Testing Guru

• TDD Advocate

Wednesday, April 10, 13

Page 7: Writing testable code

Why is it so hard to write tests?

Wednesday, April 10, 13

Page 8: Writing testable code

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

Wednesday, April 10, 13

Page 9: Writing testable code

Unit Testing

[…] unit testing by definition only tests the functionality of the units

themselves.

http://en.wikipedia.org/wiki/Unit_testing

Wednesday, April 10, 13

Page 10: Writing testable code

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

Wednesday, April 10, 13

Page 11: Writing testable code

Dogmavs.

Reality

Wednesday, April 10, 13

Page 12: Writing testable code

A world of Trade Offs

Wednesday, April 10, 13

Page 13: Writing testable code

What should we test?

Wednesday, April 10, 13

Page 14: Writing testable code

How much should

we test?Wednesday, April 10, 13

Page 15: Writing testable code

“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

Wednesday, April 10, 13

Page 16: Writing testable code

The Hidden Secret

Of TDD

Wednesday, April 10, 13

Page 17: Writing testable code

The Secret of TDD

Wednesday, April 10, 13

Page 18: Writing testable code

The Secret of TDD

Wednesday, April 10, 13

Page 19: Writing testable code

Some books by Kent Beck

Wednesday, April 10, 13

Page 20: Writing testable code

To write good tests first we need

to learn how to program

Wednesday, April 10, 13

Page 21: Writing testable code

Wednesday, April 10, 13

Page 22: Writing testable code

We developers are like those users we like to complain so

much about

Wednesday, April 10, 13

Page 23: Writing testable code

Design evolves and matures with time

Wednesday, April 10, 13

Page 24: Writing testable code

Good Code sits in the small details

Wednesday, April 10, 13

Page 25: Writing testable code

TIPS

Wednesday, April 10, 13

Page 26: Writing testable code

Separate pure code from impure or stateful

Wednesday, April 10, 13

Page 27: Writing testable code

Pure Functions

Wednesday, April 10, 13

Page 28: Writing testable code

Pure Functions

• Referential Transparency

Wednesday, April 10, 13

Page 29: Writing testable code

Pure Functions

• Referential Transparency

• Don’t modify external state

Wednesday, April 10, 13

Page 30: Writing testable code

Pure Functions

• Referential Transparency

• Don’t modify external state

• Don’t produce side effects

Wednesday, April 10, 13

Page 31: Writing testable code

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

Wednesday, April 10, 13

Page 32: Writing testable code

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

Wednesday, April 10, 13

Page 33: Writing testable code

Score calculation can be moved into its own function

Wednesday, April 10, 13

Page 34: Writing testable code

Score calculation can be tested now

Wednesday, April 10, 13

Page 35: Writing testable code

First write Pure Code

Wednesday, April 10, 13

Page 36: Writing testable code

Add impure code step by step when

needed

Wednesday, April 10, 13

Page 37: Writing testable code

Write Composable

Code

Wednesday, April 10, 13

Page 38: Writing testable code

Function Composition

http://en.wikipedia.org/wiki/Function_(mathematics)

Wednesday, April 10, 13

Page 39: Writing testable code

Function Composition

http://en.wikipedia.org/wiki/Function_(mathematics)

Wednesday, April 10, 13

Page 40: Writing testable code

This looks familiar

Wednesday, April 10, 13

Page 41: Writing testable code

“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

Wednesday, April 10, 13

Page 42: Writing testable code

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.

Wednesday, April 10, 13

Page 43: Writing testable code

Why don’t we justcode in this style?

Wednesday, April 10, 13

Page 44: Writing testable code

This seems familiar again…

Wednesday, April 10, 13

Page 45: Writing testable code

Welcome to Functional

Programming

Wednesday, April 10, 13

Page 46: Writing testable code

“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

Wednesday, April 10, 13

Page 47: Writing testable code

What can we learn from Functional Programming?

Wednesday, April 10, 13

Page 48: Writing testable code

The proper use of Types

Wednesday, April 10, 13

Page 49: Writing testable code

What does ‘null’ mean?

Wednesday, April 10, 13

Page 50: Writing testable code

What does ‘true|false’ mean?

Wednesday, April 10, 13

Page 51: Writing testable code

Functions with just one responsibility

Wednesday, April 10, 13

Page 52: Writing testable code

Radical separation of pure code from impure code

Wednesday, April 10, 13

Page 53: Writing testable code

Let’s see an example

Wednesday, April 10, 13

Page 54: Writing testable code

Food for Thought

http://thinking-forth.sourceforge.netWednesday, April 10, 13

Page 55: Writing testable code

“Inside every well- written large programis a well-written small

program”

http://www.linfo.org/q_programming.html

Wednesday, April 10, 13

Page 56: Writing testable code

Questions?

Wednesday, April 10, 13

Page 57: Writing testable code

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

http://www.slideshare.net/old_sound

Wednesday, April 10, 13


Recommended