Why Bother With Unit Tests?

Tags: software engineering, software-engineering, software, development, developers, unit testing, tet driven development, tdd, practices.development, practices, techniques, mspec, ncrunch

If you are not yet "test infected", then perhaps I can nudge you towards trying unit testing and in particular test driven development (TDD) for yourself. I began to adopt the practice about 8 years ago and I find it difficult now to contemplate a project without unit tests.

I thihk what many developers fail to grasp is that unit testing is not really about testing. Superficially, it is and you can treat it that way, writing tests after the code and then only selectively testing. However, there is a deeper level that becomes apparent once you commit to doing test driven development. It is really more of a software design technique that helps you to write more robust well-factored code. Often, the 'aha moment' comes late in the development cycle when the project is in maintenance mode. A unit test suite helpt to guide you towards writing loosely coupled code and gives you confidence to make changes and swap parts fo the code in and out as you need to. Others can tell you about this technique, but to really appreciate and understand the benefits, you have to try it for yourself. You have to fully commit to the idea and force yourself to keep good discipline.

In this short video, I make no attempt to teach the techniques. What I'm trying to show here is how a suite of unit tests can give you the confidence to make changes to your code and know that you haven't broken anything in the process. I demonstrate making a behavioural change to an existing feature. I show:

  1. how I first pin the correct behaviour by writing a test for the working code and shoeing that it passes;
  2. then I write tests for the new behaviour and show that they fail;
  3. then I make those new tests pass by implementing the new behaviour and we can see that the oringal tests all still pass.

At this point I have a lot of confidence that I have:

  • Implemented the new behaviour. Correctly.
  • Not broken any of the other parts of the code.

I hope this helps to encourage you to move towards using test driven development. It is not something that will happen in a day. You have to care enough about your code quality to commit to it and force yourself to do it. I personally struggled with the techniques through several projects before I finally got to the point where I felt uncomfortable NOT having unit tests in my projects. This can be a difficult journey for a single developer working in isolation, although there are many YouTube videos and tutorials on the techniques. Some of the most useful content I found while learnign to do this was on for-pay training materials, particularly Pluralsight.

No Comments

Add a Comment

Let Us Help You

Find us on Facebook