Why Should I Write Unit Tests?

In my previous article I explored the reasons why developers don’t to write unit tests, when they are developing code. In this article I want to discuss the reasons why you should write unit tests for the new code that you write or the bugs that you fix.

First Scenario – I’m Fixing A Bug

This is a great time to write a unit test for the code that you are modifying. First you want to investigate the scenario or scenarios which will cause the bug to manifest itself in the code. Once you have nailed down the ways in which you can reproduce the bug, you have a much greater change of fixing the bug. You may be lucky enough to have a tester to work with to define the scenarios where bug shows itself. This makes your life easier as a developer and also helps the tester who will validate your code when it is released to the testing environment.

With the testing scenarios in hand you can then create a test class with specific tests to exercise the code. You will now have some failing tests, this is a great thing, because now you can work on fixing the code. Once the tests you have written have gone green, you can have a good degree of confidence that you have fixed the code. If not you can go back an write another test to capture the missing scenario.

So What Are The Benefits?

More Confidence Changing The Code

You have written these wonderful unit tests and it has seemed like extra work. But what about the next time the code you have just fixed is touched. It might regress if nobody knows the bug was there in the past. They may unwittingly change the code and re-introduce the bug and nobody will know unless it is spotted by a tester again. We now have some tests to lean on when we change the code.

Less Chance Of Regression

So that innocuous one line change, which seems so innocent and won’t possibly hurt our code base, does in fact break our code. We can see this by simple running the unit tests and know that we have broken something. You can spend more time writing new code instead of wrestling with the zombie bug which won’t go away!

Second Scenario – New Development

The team you are working with is embarking on a new development project, what an exciting time you think to yourself. You want to start writing unit tests for the code that you are producing straight away.

So What Are The Benefits?

Better Understanding of The Problem

This will help you ensure that you understand the problem that you are trying to solve with the code that you are writing. You may spot edge cases in problem you solving and you can write tests to ensure that these edge cases are handled by your code. This also forces you to confront the fact that you don’t understand the problem you are trying to solve early on.

Produces Testable Code

Writing unit tests forces you to write code which is decoupled. You have to write code which is easily testable. You will feel this pain as you are writing the tests. For instance if you produce a class with too many dependencies, you will feel this pain when you are setting up your tests.

Better Design

Writing unit tests forces you to think about how you are designing the code. You also see it from the perspective of someone using the code that you have written. It becomes apparent if the design is intuitive or if you have to think hard before knowing how to use it. This also helps challenge your assumptions about using your own code. This is much better to find out earlier on, since you can fix the problem quicker and easier.

Refactor Code With Confidence

Having an application with a suite of unit tests is great. This will give confidence to you and those that come after you when the code is refactored or changed. You can rely on the suite of unit tests to tell you if you have broken something early. You can avoid costly bugs being introduced, since you will find them much earlier in the process.

Provides Documentation

Having unit tests for the application helps document the application. So someone coming in from a cold start to the application can look at the unit tests and gain a better understanding of how the code works and what it is mean’t to do.

Debugging Is Easier

If you are working on a large application, you can work more rapidly by writing unit tests and using them as a starting point for debugging any issues you have during development. Its certainly much faster than running a large application and ensuring you have the correct test data set up for the scenario you are trying to verify.

Reduced Costs

By unit testing you find bugs earlier on in the development process. The cost of fixing bugs later in the development process increases. This is more apparent if the bug has a large impact on the code base. Since you will have to retest the code that you have changed and anything affected by it.

Conclusion

Writing unit tests when you start developing new code or fixing existing code saves you time and money. You have greater confidence when changing your code base knowing that you can rely upon a consistent suite of unit tests. The software products which are produced as a result of unit testing are more stable and easier to change.