Wednesday, August 26, 2009

XUnit Smells

I think unit tests are a bit like dental floss. We all know we should use it daily (the floss), but we still don't do it. The knowledge of its usefulness does not automatically mean we are going to actually do it. The same goes with unit tests. I believe every single developer in our department is aware that unit tests would improve that quality of our product, but we still don't do it.

I keep wondering: why can't we have some working (and useful) unit tests for our software, when everyone else in the industry seems to be doing it? I actually took active part in an effort to establish a unit test suite for our product a couple of years back, and it failed. It didn't fail because of a lack of commitment from the team, on the contrary, everyone was on-board trying to make this work. But it failed because the maintenance costs for the test suite were too high with respect to the benefits it provided. That is why I thought that the XUnit Test Patterns and Smells presentation would be useful for our team. What was wrong with our test code that made it unmaintainable?

The presentation was given by Gerard Meszaros. The first thing he said that really caught my attention was this (he called it a sobering thought): "there should be at least as much test code as there is production code, or maybe twice as much." For me this was a pretty shocking declaration. What he said next was not nearly as shocking, and it made a lot of sense given the first assertion: "test code should be much easier to maintain than production code". Another point the presenter made is that we should design for testability upfront.

2 comments:

  1. I realize I didn't mention any actual test smells in the original post, so here are some:

    * The test code is hard to understand
    * The test code is hard to write
    * Test execution is erratic
    * Conditional logic in test code
    * etc.

    ReplyDelete
  2. Hi,

    In line with your subject, I would recommend the following reading:

    * What makes code hard to test (and why we should consider testing up front): See http://misko.hevery.com/2008/07/30/top-10-things-which-make-your-code-hard-to-test/

    * What prevents us from writing tests; sometimes, we just do not know how to do that well:
    See http://misko.hevery.com/2009/07/07/why-are-we-embarrassed-to-admit-that-we-dont-know-how-to-write-tests/

    I would invite anybody interested in testing to follow the posts from Misko Hevery here: http://misko.hevery.com/

    Soon, we will put some metrics in place to at least measure to what extent we do unit test our software. It could be an interesting starting point to improve with respect to testing.

    ReplyDelete