Real life unit testing tools and practices

17
Gil Zilberfeld Real Life Unit Testing Tools and Practices 11-Jan-2012

Transcript of Real life unit testing tools and practices

Page 1: Real life unit testing   tools and practices

Gil Zilberfeld

Real Life Unit TestingTools and Practices

11-Jan-2012

Page 2: Real life unit testing   tools and practices

Agenda

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

Page 3: Real life unit testing   tools and practices

People write millions of lines of legacy code

every day!

Page 4: Real life unit testing   tools and practices

So what is legacy code?

• Code that doesn’t have tests• Michael Feathers

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

Page 5: Real life unit testing   tools and practices

Testing legacy code

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

Page 6: Real life unit testing   tools and practices

The death spiral

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

Page 7: Real life unit testing   tools and practices

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…

Page 8: Real life unit testing   tools and practices

You want tests…

• But for that, you need to change your code

• But without tests, that’s crazy!

Page 9: Real life unit testing   tools and practices

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 10: Real life unit testing   tools and practices

Demo: Hand-Rolled Mocks

Page 11: Real life unit testing   tools and practices

Mocking Frameworks

• Change behavior of code without changing it

• Verification of calls

Page 12: Real life unit testing   tools and practices

Demo: Mocking Framework

Page 13: Real life unit testing   tools and practices

.NET Mocking Frameworks

• Open-source• Commercial

Page 14: Real life unit testing   tools and practices

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

Page 15: Real life unit testing   tools and practices

Guidelines

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

Page 16: Real life unit testing   tools and practices

Summary

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

beginning• Start today!

Page 17: Real life unit testing   tools and practices

www.typemock.com

typemock.com/blog

@typemock

www.gilzilberfeld.com

[email protected]

@gil_zilberfeld

Questions?