27
The Star Wars Version Gil Zilberfeld Unit Testing

Unit testing - The Star Wars version

Embed Size (px)

DESCRIPTION

Introduction to unit testing and mocking as seen in a galaxy far away...

Citation preview

Page 1: Unit testing - The Star Wars version

The Star Wars Version

Gil Zilberfeld

Unit Testing

Page 2: Unit testing - The Star Wars version

Bugs

How do we deal with them?

Page 3: Unit testing - The Star Wars version

xUnit Frameworks

• Test Identification• Test Runner• Asserts

Page 4: Unit testing - The Star Wars version

.NET xUnit Frameworks

• NUnit• MSTest• xUnit• MbUnit

Page 5: Unit testing - The Star Wars version

Demo

Page 6: Unit testing - The Star Wars version

Why Unit Test?

• Low TCO• Small setup

overhead• It’s not a bug if

it’s caught before QA

• Go faster

• Quick feedback• Knowing your

code works• Confidence to

change your code• Quicker than

debugging

Page 7: Unit testing - The Star Wars version

The death spiral

• Tests are slow• Stop running tests• Stop writing tests• Back to square one

Page 8: Unit testing - The Star Wars version

People write millions of lines of legacy code

every day!

Page 9: Unit testing - The Star Wars version

Testing legacy code

• Tests are slow• Require big setup• Maybe impossible to run• Slow to debug

Page 10: Unit testing - The Star Wars version

Isolation and Mocking

• Let you test your code for different scenarios, by faking the dependencies

• As long as your code is ready for it

Page 11: Unit testing - The Star Wars version

Demo: Hand-Rolled Mocks

Page 12: Unit testing - The Star Wars version

.NET Mocking Frameworks

• Open-source• Commercial

Page 13: Unit testing - The Star Wars version

Demo: Mocking Framework

Page 14: Unit testing - The Star Wars version

Coverage…

… and other Jedi mind

tricks

Page 15: Unit testing - The Star Wars version

Test Driven Development

Page 16: Unit testing - The Star Wars version

Demo

Page 17: Unit testing - The Star Wars version

Benefits

• Same Tools • Better Coverage• Less debugging• Better Design

Think before your code!

Page 18: Unit testing - The Star Wars version

But…

• Requires discipline• Works better for greenfield

projects• Not for everyone

Page 19: Unit testing - The Star Wars version
Page 20: Unit testing - The Star Wars version

Agile Adoption

Page 21: Unit testing - The Star Wars version
Page 22: Unit testing - The Star Wars version

Working software

• Automated builds• Automatic build• Run unit tests• Run integration tests

• Unit tests and TDD

Over comprehensive documentation

Page 23: Unit testing - The Star Wars version

Guidelines

• Unit testing is a team

sport

• Where to start?

• Scaffolding

Page 24: Unit testing - The Star Wars version

Guidelines

• Naming

• Reviewing

• AAA

Page 25: Unit testing - The Star Wars version

Guidelines

• Refactoring

• Readability

• Coverage

• Organization

Page 26: Unit testing - The Star Wars version

www.typemock.com

typemock.com/blog

@typemock

www.gilzilberfeld.com

[email protected]

@gil_zilberfeld

Questions?

Page 27: Unit testing - The Star Wars version

Death star explosion probability calculator• Only Luke can shoot correctly• Explode if core temperature > 500• 1000 X-Wings• Probability of Luke being hit depends on the

other X-Wingsp(success) = 0.3(X-Wings)

• Probability of X-Wing surviving degrades in time

p(survival) = 1/t2

• Calculate Luke's hitting probability every 100 time units