Posts
12
Comments
5
Trackbacks
0
Writing Great Unit Tests

Steve Sanderson has a great post on writing great unit tests: best and worst practices.  The only things I'd add are:

  1. Use a dynamic mock framework like NMock, Rhino Mocks, etc wherever possible to mock your interfaces. 
    • Don't create static mock implementations of your interfaces only to throw NotImplementedException or return specific values from its functions.  Instead, use dynamic mocks.
    • Do create static mock implementations of your interfaces where you need to set the same types of expectations over and over again on your mocks.  For example, imagine you use an ILoggingService interface to log events, and you want to test several components to ensure they log the correct events in the correct order.  Instead of setting up Mock<ILoggingService> in each test, create a reusable MockLoggingService that takes as input the list of events expected to be logged in the expected order, and use it across your tests.
  2. Don't set a lower quality bar for your tests than for your other code.
    • Extend your coding standards to your tests.  Turn Treat Warnings As Errors on, enforce FxCop rules, enforce StyleCop rules, etc.
    • Document your tests.  Turn Generate XML Comments on.  Require good, meaningful, descriptive comments describing what the code being tested should do, what the test does, and how it relates to other tests.
  3. Train your team on how to write good unit tests.
  4. Review your team's unit tests.

Colin

posted on Tuesday, August 25, 2009 9:06 AM Print
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 3 and 1 and type the answer here: