Thursday, 9 May 2013

Importance of Unit Tests

Anyway I'm writing a post on creating my first set of unit tests by myself for a small console app that a colleague created, and I thought to lead up to it I'd write a brief post explaining the importance of unit tests, why they're important and how they can make our lives as QA easier.

Wikipedia defines Unit Testing as "unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine if they are fit for use". So essentially, in everyday terms, it's the smallest possible piece of testable code.

Some people would argue that unit tests are a developers task, but I feel being the first form of QA on the code, that it's only right that as a QA you should play some role in coming up with then. Now you don't have to be able to write the unit tests, but at the very least you should sit down and come up with the scenarios, and maybe even pair program on writing the tests, if that's what floats your boat. 

The overall driving factor behind unit testing is that it's cheaper to fail fast and early, the cost of fixing a bug increases exponentially the further down the development cycle it's found, with Unit Tests they give you instant feedback when a test fails.

If a unit test fails it's often easy to debug and quick to see why it failed, even better if the unit tests are run at check in and fail the check in if any unit tests don't pass, this way it encourages developers to code better, and to make sure that unit tests are passing and testers only ever see good quality builds being deployed, and time isn't wasted sitting around waiting for a working build.

Unit Tests are quick and easy to run and maintain, unlike UI tests, they aren't brittle, they don't rely on interacting with a User Interface. Unit tests ensure that the code does what it is expected to do, whereas acceptance tests ensure that the application does what the business/users expect it to.

It's also important to get involed as a QA in coming up with scenarios in unit tests, often if a test is covered by a unit test then it might mean there isn't a need for an acceptance test to be written and run(obviously this is very much specific to the codebase etc).


  1. What does Programmers dont want to write Unit Tests ?

    1. Good point. I think you should try and educate them, and the business. Often it's not developers who don't want to write unit tests, but time constraints stopping them from writing them. If you can convince the business and the developers of the value in spending that little bit longer writing unit tests, the benefits will be tenfold.