47
A Second Look at Unit Testing @RoyOsherove Senior Developer, Bouvet [email protected]

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

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: A second look at Unit Testing with Roy Osherove at Microsoft Swit

A Second Look

at Unit Testing

@RoyOsherove

Senior Developer,

Bouvet

[email protected]

Page 2: A second look at Unit Testing with Roy Osherove at Microsoft Swit
Page 3: A second look at Unit Testing with Roy Osherove at Microsoft Swit

• 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

Page 4: A second look at Unit Testing with Roy Osherove at Microsoft Swit
Page 5: A second look at Unit Testing with Roy Osherove at Microsoft Swit
Page 6: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Unit

Of

Work

Production Code

Unit Of Work

Return Value/Exceptio

n

Noticeable State Change

3rd Party call

Public API

Test

Page 7: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Production Code

Unit Of Work

Return Value/Exceptio

n

Public API

Assert Test

Page 8: A second look at Unit Testing with Roy Osherove at Microsoft Swit
Page 9: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Production Code

Unit Of Work

Noticeable State Change

Test

Public API

Ass

ert

Page 10: A second look at Unit Testing with Roy Osherove at Microsoft Swit
Page 11: A second look at Unit Testing with Roy Osherove at Microsoft Swit

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.

Page 12: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Production Code

Unit Of Work

3rd Party call

Test

Public API As

sert

3rd Party

Unit Of Work

Page 13: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Production Code

Unit Of Work

3rd Party call

Test

Public API As

sert

3rd Party

Unit Of Work

Mock

Page 14: A second look at Unit Testing with Roy Osherove at Microsoft Swit
Page 15: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Nam

ing b

y U

nit

O

f W

ork

UnitOfWork_Input_Expect

edOutput

UnitOfWork_LogicalAction

_ExpectedChangeInBeha

vior

UnitOfWork_ActionOrInpu

t_ExepectedCallToThirdPa

rty

Page 16: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Nam

ing b

y U

nit

O

f W

ork

Addition_PositiveNumbers

_ReturnsSum() Addition_WhenCalled_Res

etsTheNextSum() Addition_NegativeNumber

s_CallsLogger()

Page 17: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Setu

p M

eth

ods

Page 18: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Isola

tion

Fram

ew

ork

s

Constrained vs. Unconstrained

Page 19: A second look at Unit Testing with Roy Osherove at Microsoft Swit
Page 20: A second look at Unit Testing with Roy Osherove at Microsoft Swit

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)

Page 21: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Team Agile - All rights

reserved

Page 22: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Unco

nst

rain

ed

Page 23: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Const

rain

ed

Page 24: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Goodbye

R

hin

oM

ock

s Hello FakeItEasy,

NSubstitute

Page 25: A second look at Unit Testing with Roy Osherove at Microsoft Swit

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

Page 26: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Rspec style is ok

Assert multiple results on

same unit of work

Page 27: A second look at Unit Testing with Roy Osherove at Microsoft Swit
Page 28: A second look at Unit Testing with Roy Osherove at Microsoft Swit
Page 29: A second look at Unit Testing with Roy Osherove at Microsoft Swit
Page 30: A second look at Unit Testing with Roy Osherove at Microsoft Swit

LEGACY CODEFaking Deep

System Internals is OKExample - SilverUnit

Page 31: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Cross Cutting Fakes

Page 32: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Old code

Page 33: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Old code FakeNewcode

CROSS CUTTING FAKES

Page 34: A second look at Unit Testing with Roy Osherove at Microsoft Swit
Page 35: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Impacting

Change

Page 36: A second look at Unit Testing with Roy Osherove at Microsoft Swit

6 in

fluence

fo

rces

Personal Ability Motivation Social Ability Motivation Environmental Ability Motivation

Page 37: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Anoth

er

book?

Page 38: A second look at Unit Testing with Roy Osherove at Microsoft Swit

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

Page 39: A second look at Unit Testing with Roy Osherove at Microsoft Swit
Page 40: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Build and

Delivery

Page 41: A second look at Unit Testing with Roy Osherove at Microsoft Swit
Page 42: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Build Patterns

Page 43: A second look at Unit Testing with Roy Osherove at Microsoft Swit

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

Page 44: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Ext

ract

Scr

ipt

Extract Script

Page 45: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Build Chain Triggers

Triggered by checkin

Triggered by end of previous

Manual

Manual

Build Chaining

Page 46: A second look at Unit Testing with Roy Osherove at Microsoft Swit

• 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

Page 47: A second look at Unit Testing with Roy Osherove at Microsoft Swit

Thank

you!

[email protected] [email protected]

@RoyOsherove