Testing is hard!• Testing is too much work• I can’t get my developers to write tests• I can’t get my teammates to write tests• My codebase was not written with testability• Mocking frameworks take too much work tofake out deep call chains• My dependencies are static, sealed, or non-public and can’t be faked
Jargon/Definitions• Unit Test– Code (usually a method) that runs another piece of code (usually a single method/function inisolation) and checks the correctness of an assumption afterward. If the assumptions arewrong, the unit test is failed.• Fake– Encapsulates noth Stubs and Mocks• Stub– A controllable replacement for an existing dependency in the system.– Use a stub to test your code without dealing with the dependency directly– Can not fail a test– Usually just a dummy object that does nothing, returns property values as instructed– Not the item being tested again• Mock– A fake object in the system that decides whether the unit test has passed or failed.– Does so by verifying whether the object under test interacted as expected with the Fake
Testing• User Model Testing - link• Acceptance Testing - link• Usability Testing• Performance and load testing• Security Testing• Integration Testing• Unit Testing – uses fakes to isolate
Unit Testing weaknesses• Can not catch integration errors, system-levelerrors, or non-functional (performance,security, etc)• Additional maintenance• Additional coding• Unit tests may share the same blind spots asthe code because they are written by thesame developer
Unit Testing strengths• A code driven documentation– Less likely to get out of sync than comments– Less likely to get out of sync than documentation• Regression reduction• Instant feedback• Safer refactoring and feature addition• Unit tests may help the developer find blindspots
What is a good unit test?• Automated/repeatable• Easy to implement• Once written, remains for future use/versions• Anyone should be able to run it• Runs quickly (up to 20,000 in a minute)– No dependencies
What’s a mock/isolation framework?• In order to properly unit test your externaldependencies must be replaced. A unit testshould ONLY fail because of the code you aretesting, otherwise it’s an integration test.• A mock framework is a tool to replace externaldependencies with fakes.
What does TypeMock bring?• Fake static/sealed/non-public elements– DateTime.Now faking!• No design for testability required• Recursive fakes (nested call chains) a.b.c.d
TypeMock vs Others• Some mock frameworks can only mockinterfaces• nUnit.Mocks – no events or generics mocking• Rhino Mocks – no strings for method names,single developer (if he stops, game over?)– Documentation does not appear to support non-interface mocking• nMock 2.0 – currently in RC status, appears torequire interfaces to fake
Debate• Advocates of Testability say you don’t get theclean SOLID design of writing for testability• Should the isolation framework be dictatingdesign decisions?• Design for testability leaves out the majorityof work… Brownfield applications
TDD and indirectly unit testingresearch• "We found that test-first students on average wrote more tests and,in turn, students who wrote more tests tended to be moreproductive. We also observed that the minimum quality increasedlinearly with the number of programmer tests, independent of thedevelopment strategy employed."• "The external validity of the results could be limited since thesubjects were students. Runeson  compared freshmen,graduate, and professional developers and concluded that similarimprovement trends persisted among the three groups. Replicatedexperiments by Porterand Votta  and Höst et al.  suggest that students mayprovide an adequate model of the professional population."• Taken fromhttp://weblogs.asp.net/rosherove/archive/2008/01/25/research-finds-tdd-boosts-developer-productivity.aspx
More reading…• Martin Fowler – Mocks aren’t stubs• Uncle Bob – TDD Derangement Syndrome
Good Practices• Only a single Assert per test• Code a test for both the Happy Path and theUnhappy Path• Start out testing by hitting your longest, mostcyclomatically complex, most critical, or justhardest to understand methods.• Try to reduce the external dependency contactpoints in a class• Write a test that FAILS first! Write the test beforethe code, or comment out the part that shouldmake it succeed.
My Passions• Tooling anything that makes coding easier ormore productive• MVC• Extensibility