Everybody Needs … Unit Tests

Picture courtesy of Lotus72@flickr

… please remember people, no matter who you are, and whatever you do to live, thrive and survive, there are still some things that make us all the same: you, me, him, them – everybody, everybody! – this is a fragment of Blues Brothers’ “Everybody Need Somebody” song. Nothing to do with software development but when you change the next line of this song from “Everybody needs somebody” to “Everybody needs Unit Tests” it becomes closer to software development.

I intentionally used lyrics of this song to keep everyone’s attention (just a little bit) to this post. If you are familiar with TDD and unit test your code you can disregard it – I’d like to address this post to all software engineers/developers/programmers/etc. that DO NOT unit test their code.

In this post I will try (again) to convince all software developers that still don’t unit test their code that it’s right thing to do and it’s damn simple.

Assume that Agile software development is wrong, really wrong

You don’t have to be the fan of Agile movement – you can even think it’s stupid, immature and doesn’t help software development at all. Maybe you’re right, maybe not – it’s rather a question of taste. You don’t have to use or like Scrum, you may think that XP is crazy and an overkill – you have right to think so. I can understand everything but PLEASE do unit test your code! Forget about Agile, forget about processes and methodologies – just unit test your code.

I just took over another project that is quite complex (of course it is!) but has NO SINGLE unit test (or any test at all). More so, there is no documentation of any kind. Yes I can read the code and see what it does – that’s simple but how the hell I could know it does what it should do? How could I know it’s not a mistake made by the developer who created this creature?

This code lacks Unit Test, but fortunately day after day I add more and more and I’m more confident this piece of software works.

Unit Testing once again

If you want to know something more about unit testing check out our previous articles. I will just recapitulate the most important, in my opinion, qualities of unit testing:

  • unit tests document internal and external architecture of the software system
  • unit tests help you and other developers to immediately see whether code “improvements” broke already existing code
  • unit tests help making your software bug-free – all issues should be addressed by writing a failing test first
  • unit tests together with code coverage tools improve quality of your software
  • unit tests can be used as a presentation platform – you can present hard to visualise issues using unit testing green bar – green means that it works
  • unit tests significantly improve team confidency in progress – if all the tests pass it means that all features implemented so far work and nobody broke the system
  • unit tests improve efficiency – you don’t have to perform laborious manual tests again and again after code changes, just start unit tests and watch the red/green bar
  • already there but so important that worth reminding – unit tests work as a DOCUMENTATION (auxiliary or main one)
  • I’m open for your propositions

I hope I convinced you to start writing unit tests – if you don’t like or believe in Agile movement it’s OK. Unit Testing is just an obligatory practice of every decent software developer. If you do unit test it doesn’t mean you’re good one but if you don’t unit test you’re a poor developer (I mean it).

I wonder when unit testing will be taught obligatory at every decent university. It does so many good and is so simple – people, please – write unit tests!

If you have any ideas how to convince every developer that unit testing is right thing to do I’m open for suggestions.

6 thoughts on “Everybody Needs … Unit Tests”

    1. All of the benefits you mention are 100% accurate. I cannot emphasize enough just how important unit tests are. You cannot be agile without an agile codebase. Unit tests along with well written and designed code keep your codebase agile and give your business options.

      I disagree that Unit Testing is easy, it’s not really. No pain no gain is what I say.

      We strive for 100% code coverage and would never consider doing it any other way. Although 100% is debatable as to whether that’s over-kill.

      My 2 cents,

      1. Thanks all for your valuable comments – I appreciate it.

        @jackMilunsky – I would use double standards here – say your employer or prospective employer during interview that unit testing is sometimes a difficult art but we have to encourage other guys by selling them basics (which are simple) – they will learn more advanced stuff later on i.e. step by step


  1. Another great use for unit tests is for isolated performance profiling or (in C++) memory leak detection using tools like purify, valgrind, or what-have-you. When working with server code it is may be difficult, or at least time-consuming, to bring the server to the state where performance profiling or leak detection should start. With unit tests it is much quicker, which allows you to test changes out much more quickly than would be the case if you had to relink the server, restart it, and bring it back to the state where testing should begin.

  2. Unit test gives you a better design. It gives you a decoupled system or it atleast points out where the code is coupled and in order to unit test you have to break it down into smaller modules. Unit Testing implies better design.

Leave a Reply

Your email address will not be published. Required fields are marked *