Why Junior Developers Should Learn Unit Testing?

harshit kandhway
Xebia Engineering Blog
4 min readNov 15, 2019

I am into software development for the past few years. I always thought code is about solving the problem in the fastest possible way and with the minimal lines of code. With recent learning on unit testing, my approach towards programming has changed or in other words, it has improved. Now, I think more about the problem by first writing a unit test and then writing the production code for it.

In this blog, I will share the reasons why I prefer writing unit tests and why you should do the same.

Reason 1: Help maintain focus

The best thing about writing unit tests is that they help us identify the smallest possible functionality one can start working on thinking much about alternate ways. This means I don’t have to obsess over what is the best solution for the problem. I focus on writing the simplest possible code and let my code evolve.

Reason 2: Make debugging easy

The unit test makes sure that the individual functionality of a program is working properly. While working in a bigger project if you are confident that the elementary functions are working correctly then it is relatively easy to integrate all such functionality.

Moreover, if the functions are unit tested then integration testing will only be required for integrating all functions to fulfill customer’s needs and in case any error occurs it gets easier to debug. The failed test will show the reason for failure as well as the line number which caused the error.

Reason 3: Makes it cheaper to identify and fix bugs early in the development lifecycle

Unit tests are written against individual functionality and if such test cases fail, it helps the developer to find the error in the least possible time at the specific function for which the unit test was written.

The unit test helps to identify the bugs at the development stage and hence it gets easier to rectify those errors. There was a case study on the development process done by two teams having similar skills and a similar number of developers, the results are shown below in the picture.

As we can observe writing test cases takes more time as compared to development without unit testing but still, the release time is less in the test-driven approach and one important thing to look for is the number of bugs in the production system which is approximately 6 times lesser when unit tests are included in the development process.

Reason 4: Acts as executable documentation for the API

Unit test names are more descriptive as a result anyone reading the test methods in the test file will get the basic idea of what the code does and all the different use cases that are covered through the program.

This helps in improving the quality of code by increasing the readability and understandability of the codes.

Reason 5: Cost-Efficient for customers so a win-win situation for both developers and customers

We all are aware of the bugs that occur when we are working with the customer’s system. In a study, it has been proved that these bugs and their solutions incur different costs.

Cost of defects across the development lifecycle

To rectify a bug identified at the developer’s system costs about 25$ and when this bug is identified at customers’ production system the cost incurred to resolve it may even range to 16000$. Hence a customer will always want to get their work done at minimum cost as a result writing unit test helps to identify any such bugs at developers system reducing the total price the customer has to pay and it is even easier to resolve the bug at the developer system itself rather rectifying it on the production system.

--

--