Saturday, February 17, 2007

Unit testing, lessons learned

I was doing a small application and thought of using the opportunity to see what additional effort it would require to make it unit-testable.
  • Two projects had to be added to the solution: A class library project where the code to be tested has been factored out, and a class library containing the unit-tests.
  • The configuration could no longer be read directly from the app.config, because it had to be changable programatically, so a ConfigurationManager had to be introduced.
  • An additional interface had to be introduced for the ConfigurationManager so that it could be stubbed out.
  • A second constructor had to be added which took a ConfigurationManager as a parameter.
  • The run method contained an infinite while loop and needed therefore to be split into two methods.
  • Finally, all the unit-tests, of course, had to be written :)
What was a simple code, easily tested by hand, turned out to be somewhat of a programming effort. The code became more complicated and, consequently, more brittle. Lets hope that the unit tests are good enough to compensate for that ;)

No comments: