63
A Second Look at Unit Testing @RoyOsherove [email protected]

A Second Look at Unit Testing @[email protected]

Embed Size (px)

Citation preview

A Second Look

at Unit Testing@RoyOsherove

[email protected]

• Unit Of Work• Setup Methods

• Constrained vs. Unconstrained Isolation

Frameworks• Principles of Good Isolation Frameworks

• Faking Deep Legacy is OK• Six Influence Forces

• Beautiful Builds

Bottleneck Ninja Test Review Lion Build Bear

Unit

Of

Work

Production Code

Unit Of Work

Return Value/Exceptio

n

Noticeable State Change

3rd Party call

Public API

Test

Production Code

Unit Of Work

Return Value/Exceptio

n

Public API

Assert Test

Production Code

Unit Of Work

Noticeable State Change

Test

Public API

Ass

ert

Production Code

Unit Of Work

3rd Party call

Test

Public API As

sert

3rd Party

Unit Of Work

Production Code

Unit Of Work

3rd Party call

Test

Public API As

sert

3rd Party

Unit Of Work

Mock

Nam

ing b

y U

nit

O

f W

ork

UnitOfWork_Input_Expect

edOutput

UnitOfWork_LogicalAction

_ExpectedChangeInBeha

vior

UnitOfWork_ActionOrInpu

t_ExepectedCallToThirdPa

rty

Nam

ing b

y U

nit

O

f W

ork

Addition_PositiveNumbers

_ReturnsSum() Addition_WhenCalled_Res

etsTheNextSum() Addition_NegativeNumber

s_CallsLogger()

Setu

p M

eth

ods

Isola

tion

Fram

ew

ork

s

Constrained vs. Unconstrained

Goodbye

R

hin

oM

ock

s Hello FakeItEasy,

NSubstitute

Isola

tion

fram

ew

ork

s Fu

ture

s

Usability Readability Good Errors Simplicity Robust future proof tests Less over specification

Non strict Recursive Fakes

Rspec style is ok

Assert multiple results on

same unit of work

LEGACY CODEFaking Deep

System Internals is OKExample - SilverUnit

Cross Cutting Fakes

Old code

Old code FakeNewcode

CROSS CUTTING FAKES

Impacting

Change

6 in

fluence

fo

rces

Personal Ability Motivation Social Ability Motivation Environmental Ability Motivation

Anoth

er

book?

Tools

Pex – yes Moles (ms fakes) – NO

JustMock - Maybe Nuget – YES Chocolatey – YES

TeamCity – YES FinalBuilder – YES Ruby and Rake – Maybe

Amazon AWS agent hosting –

YES Git for very small commits -

YES

Build and

Delivery

Build Patterns

Build

Scr

ipt

Inje

ctio

n Allow version aware builds Build scripts are part

of source control Build configurations on

the CI server are dumb

triggers

Ext

ract

Scr

ipt

Extract Script

Build Chain Triggers

Triggered by checkin

Triggered by end of previous

Manual

Manual

Build Chaining

Slow Builds

Do X

Do X + Y

Do X + Y + Z

Do X + y + Z + N

Cumulative, Fast Builds

Do X

Do Y

Do Z

Do N

How Fast?

Extract Script

Shipping Skeleton

Fill in the blanks

Cumulative Builds

Build Waves

Location Agnostic

BeautifulBuilds.com

(wtf how many books?)

Cours

es

I’m

D

oin

g

Beautiful builds TDD with JS TDD with Ruby Art of Unit Testing in .NET from scratch [email protected]

Norway & Sweden Osherove.com for info

A s

hort

song?

This is a test line

It looks like you’re

doing just fine

It’s time for a

song of mine

It’s late in the

evening

It’s

late

in t

he

eve

nin

g

I’m wondering what words to say It’s so hard to breath

here The customer’s face is

gray

And then he

asks me

Is the product

alright?

And I say

“yes, we deploy it all

tonight”

I check in the

changes

I check in the

changes

The code doesn’t even

compile

I wish I was home now

Instead I fake a big smile

And when they

ask me

Is the code alright?

I say

“yes, it seems to

compile tonight’

I feel horrible

Because I see

The sadness in

their eyes

When they find out

That the product

Doesn’t even have

log files

This thing is

doomed

It’s time to go

home now

And I’ve got an aching

head

So I lock my computer

And pray that tomorrow

I’m dead

When my wife

asks me

“Do you feel

alright?”

I say “yes. Let’s watch game of thrones tonight”

I fe

el m

isera

ble Because I’ll guess

The madness in their

talk

When they find out that tomorrow I don’t plan to come to

work And write my CV

Thank

you!

Beautiful builds TDD with JS TDD with Ruby Art of Unit Testing in .NET from scratch [email protected]

@RoyOsherove Norway & Sweden