A second look at Unit Testing with Roy Osherove at Microsoft Swit

Preview:

DESCRIPTION

 

Citation preview

A Second Look

at Unit Testing

@RoyOsherove

Senior Developer,

Bouvet

roy@osherove.com

• 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

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

Noti

ceable

Sys

tem

Behavi

or

Change

Noticeable by a user

of the public API, at the same level of

the entry point that made the change.

ACT and ASSERT on the same level of APIs.

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

Recu

rsiv

e F

ake

s &

C

hain

ed

Exp

ect

ati

ons

Recursive is not explicit: Person.Manager.Manager is

already a fake instance without asking for it

Chained Expectations are explicit Mock.Arrange(() => Person.Manager.Manager)

.Returns(fakeManager)

Team Agile - All rights

reserved

Unco

nst

rain

ed

Const

rain

ed

Goodbye

R

hin

oM

ock

s Hello FakeItEasy,

NSubstitute

Valu

es

of

a G

ood

Isola

tion F

ram

ew

ork

Simple Single point of entry Easy to remember Intellisense Guidance

Test Robustness Recursive Fakes non strict by default

Helpful Helpful Error Messages

No surprisesTeam Agile - All rights

reserved

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

• 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

Thank

you!

royo@bouvet.no roy@osherove.com

@RoyOsherove

Recommended