16
@gil_zilberf eld Gil Zilberfeld, Product Manager Unit Testing – The Prequel Jan 29 th , 2013

Unit testing the prequel

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Unit testing   the prequel

@gil_zilberfeld

Gil Zilberfeld, Product Manager

Unit Testing – The Prequel

Jan 29th, 2013

Page 2: Unit testing   the prequel

@gil_zilberfeld

Prepare

• Know your tools

• Evaluate the risk

• Choose your partner

Page 3: Unit testing   the prequel

@gil_zilberfeld

Step 1: What to test

• What to test:• What you’re

working on• Bug or feature?• Logic

• What not to test:• Multithreading• Stored procedures• UI

Page 4: Unit testing   the prequel

@gil_zilberfeld

Step 2: Get ready

• Pick organization strategy• Create test projects• Remove fluff• VS I’m looking at you

Page 5: Unit testing   the prequel

@gil_zilberfeld

Step 3: Pick a scenario

• Scenario:• Context• Action• Success criteria

• Pick the simplest one

• Say it!• Name it!• Assert it!

Page 6: Unit testing   the prequel

@gil_zilberfeld

@gil_zilberfeld

How code looks

Page 7: Unit testing   the prequel

@gil_zilberfeld

@gil_zilberfeld

Simple pathtest

Page 8: Unit testing   the prequel

@gil_zilberfeld

@gil_zilberfeld

“Happy path”test

Page 9: Unit testing   the prequel

@gil_zilberfeld

Step 3: Pick a scenario

• Scenario:• Context• Logic• Success criteria

• Pick the simplest one

• Say it!• Name it!• Assert it!

Page 10: Unit testing   the prequel

@gil_zilberfeld

Step 4: Identify dependencies

• Read the code• Constructor

parameters• Fields• Statics• Global state

• Mocking strategy• None (Integration)• Hand-rolled• Interface based• Anything goes

Page 11: Unit testing   the prequel

@gil_zilberfeld

@gil_zilberfeld

Let’s play:Where are the dependencies?

Page 12: Unit testing   the prequel

@gil_zilberfeld

Step 5: Simplify design

• Automatic refactoring• Close down regions you’re not going to cover• Rename fields• Extract methods

• Manual refactoring works too• …but more risky

Page 13: Unit testing   the prequel

@gil_zilberfeld

Step 6: Here we go!

• ArrangeRun

Fail• Arrange

RunFail…..

• Success!

Page 14: Unit testing   the prequel

@gil_zilberfeld

Step 7: Next!

• Pick the next scenario

• Say it• Name it• Assert it

• Mutate the test setup

• ArrangeRun

Fail…

• Win!

Page 15: Unit testing   the prequel

@gil_zilberfeld

Congratulations!

• This is just the beginning• Lifetime improvement• Picking scenarios• Naming• Mocking• Refactoring• … and much more!

Page 16: Unit testing   the prequel

@gil_zilberfeld

www.typemock.com

typemock.com/blog

@typemock

www.gilzilberfeld.com

[email protected]

@gil_zilberfeld

Questions?