I have often asked myself "How should I write this test?"  It's hard, frustrating, and takes a lot of devotion.  The best answer I can come up with is usually "just dive in."

Getting a clear definition of what a unit test is supposed to do is a good start.  The pattern I notice while writing unit tests is that you must set up any required dependencies, perform a task, and then assert that some result of business logic.  If the expected result is the actual result, the test can be passed.

There are varying degrees of how a test can pass and it’s really up to you. I tend to feel that getting the test to compile and run a basic scenario is the tip of the iceberg. The more ground my tests cover, the more satisfied I am that I won’t run into unpredictable situations. So, for me, a good unit test is one that leaves almost no uncertainties.

As a common practice, I try to output as much useful information as I can about the tests that I write.  If I am testing the creation of an object with parameters, I will output what the parameters are (perhaps I'm trying to test many iterations of parameters).  After I create the object, I assert that it is in a good state, and if it is not, I fail the test and output any reasons I can find.  Enclosing the tests in a try/catch block is helpful to catch exceptions I wasn't prepared to encounter.  Writing out the exception message and stack trace during the test helps to see what might have gone wrong in the test without having to spend as much time debugging.  Even if a test passes, I make it a habit of outputting the results of the passed test.  The simple truth is that tests are fallible.

Testability is a word we often use to describe the practicality of properly testing.  The term should infer that code is easy to test. Just because something was tested, doesn’t mean that it was done easily or done well.  Code that is easy to test tends to require minimal setup, and accomplish a minimal number of goals.

I would be lying if I said that unit tests didn't make an ounce of difference in my code.  While the benefits are not as obvious up front, they become more apparent over time.  In my opinion, it is one of the best skills for a developer to learn.  Writing good unit tests takes practice, so keep trying (I know I will be).

Wednesday, June 17, 2009 2:11:00 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
Testability | Unit Testing

John Nelson

mugshot I am a passionate C# Developer working in ASP.NET on an e-commerce solution for ticketing software. I work across all of the application layers, including server side functionality, and client side programming with jQuery and MS Ajax. Although my full time job is in WebForms, I spend many of my off hours working with MVC. I am especially interested in productivity and good programming practices.

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
johncoder.com
Statistics
Total Posts: 41
This Year: 17
This Month: 0
This Week: 0
Comments: 4