Real life unit testing tools and practices

Post on 11-May-2015

315 views 0 download

Tags:

Transcript of Real life unit testing tools and practices

Gil Zilberfeld

Real Life Unit TestingTools and Practices

11-Jan-2012

Agenda

• The problems with legacy code • How isolation helps • Hand rolled mocks • Mocking frameworks• Writing effective unit tests

People write millions of lines of legacy code

every day!

So what is legacy code?

• Code that doesn’t have tests• Michael Feathers

• Code you’ve written before last Tuesday• Gil Zilberfeld

Testing legacy code

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

The death spiral

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

Code is Tangled

• Dependencies• Injected through the constructor• Passed as parameters• Available through static methods• Created inside the tested code• And through other clever ways…

You want tests…

• But for that, you need to change your code

• But without tests, that’s crazy!

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

Mocking Frameworks

• Change behavior of code without changing it

• Verification of calls

Demo: Mocking Framework

.NET Mocking Frameworks

• Open-source• Commercial

Good unit tests are…

• Quick• Can run a full suite in seconds

• Readable• Can be used as code documentation

• Focused• Can pinpoint bugs quickly when they break

• Robust• Do not break when your code changes

Guidelines

• Separate quick from slow• Use descriptive names• Test slices of functionality• Use isolation wisely

Summary

• Write quick, readable tests• Use a mocking framework from the

beginning• Start today!

www.typemock.com

typemock.com/blog

@typemock

www.gilzilberfeld.com

gilz@typemock.com

@gil_zilberfeld

Questions?