Unit testing - The Star Wars version

Preview:

DESCRIPTION

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

Citation preview

The Star Wars Version

Gil Zilberfeld

Unit Testing

Bugs

How do we deal with them?

xUnit Frameworks

• Test Identification• Test Runner• Asserts

.NET xUnit Frameworks

• NUnit• MSTest• xUnit• MbUnit

Demo

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

The death spiral

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

People write millions of lines of legacy code

every day!

Testing legacy code

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

Isolation and Mocking

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

• As long as your code is ready for it

Demo: Hand-Rolled Mocks

.NET Mocking Frameworks

• Open-source• Commercial

Demo: Mocking Framework

Coverage…

… and other Jedi mind

tricks

Test Driven Development

Demo

Benefits

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

Think before your code!

But…

• Requires discipline• Works better for greenfield

projects• Not for everyone

Agile Adoption

Working software

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

• Unit tests and TDD

Over comprehensive documentation

Guidelines

• Unit testing is a team

sport

• Where to start?

• Scaffolding

Guidelines

• Naming

• Reviewing

• AAA

Guidelines

• Refactoring

• Readability

• Coverage

• Organization

www.typemock.com

typemock.com/blog

@typemock

www.gilzilberfeld.com

gilz@typemock.com

@gil_zilberfeld

Questions?

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

Recommended