What is Unit Testing

Imagine this scenario:

A developer with a few months of experience under the belt, using just a garden variety calculation to match up with customer test data seemed to be pleased that the results were matching up.  

There were few other smaller calculations that were built atop the existing calculations. However, there were some underlying issues in the original that were caused by the inaccuracies in the new calculations. 

Each time a problem in the original calculation surfaced, the developer had to make changes to the existing code. Then the developer still has to run the program with test input parameters to see if the issue is fixed, or if more code changes have to be made. 

This is a highly painstaking process because the developer has to see if other calculations have to be changed and if there are any changes affected by the code. And eats up a lot of time.

What’s wrong here?

Not doing unit testing. If the developer had worked on it through the process of unit testing, he could have saved a lot of time, and run a crisp testing program that shows faster results. 

When unit testing came into existence

Software developers have been doing some form of testing for as long as they can remember. With each innovation, new processes have been developed and automated testing appeared. Automated testing softwares has been around since the 1980s, but were not as refined as now. With automated testing, the developers can write codes to test the program, and they can run the tests any number of times they wanted, and not with much difficulty. 

There are different ways to test if your software works, and sometimes the differences between them might get blurry. However, the two main types of testing are Unit Testing and Integration Testing. In Integration testing, the purpose is to check whether the entire product works well as a whole. And in Unit Testing, the entire product is broken down into several pieces and tested in isolation. The testing itself doesn’t take much time because only small portions of the code would be tested at a time.

The importance of unit testing

Unit testing is a method of testing whether a particular software program works, and whether the individual components of a software program have the usability and functionality that they were intended for. Testing correctly would help detect bugs. Tests are prepared in the form of functions and the value and behaviour of the program in accordance with these functions would be determined. This is done in various scenarios, with the results kept in mind for each scenario. When an invalid situation happens, the function would notify that something wrong has happened and log it. 

Unit testing is almost similar to Test Driven Development or TDD, where the developers first write the unit tests and then the codes. These are specific test cases, and the software has to pass the tests. It is also easier to write comprehensive testing when individual units are tested, and all the units are put together. 

Unit testing is an important aspect of the agile software development process, and when set as a standard process, defects can be identified and rectified. It is extremely important to find and rectify defects in the early stages of product development, and with unit testing, that would be made possible. Hence, developers should focus on writing good test cases, with sufficient time and environment. 

There are two ways of doing unit testing:

  1. Manual testing
  2. Automated testing

Manual testing

Manual testing happens when the test cases are executed without any automation tools. Each of the test stages are managed and executed manually, and hence a tedious process so it is both painstaking and time consuming. 

Automated testing

There will be an automation tool that can record and test the program in stages without any human intervention. It is important to run only necessary tests, and avoid doing low-value tests. 

Benefits of unit testing

Detects bugs early in the software

Unit tests help detect bugs early in the software development cycle, resolve them and save money in the long run. Eliminating bugs in the early stages is vital because discovering them later can cause tremendous expenses later on. Debugging at higher levels can be really expensive, because when testing at higher levels, the changes made to the code will have to be checked right from the beginning. And unit testing works only with the latest code changes. 

The software development process becomes agile

Unit testing is an integral part of agile software development, and allows the developers to add new features and functions to the existing software and make changes to the old code. Unit testing goes well within the principles of the Agile Manifesto, especially the one that states that ““The best architectures, requirements and designs emerge from self-organising teams”, Hence this technique becomes a great tool to test because the changes you make during development can indeed contribute to the best design and architecture. 

Implementing agile methodologies and unit testing, reduces the work of the developers during the testing phase, and they can focus on the test environment and create better extensive integration and system tests.

Reduces cyclomatic complexity

Cyclomatic complexity is a measure of code complexity, and code complexity can be measured through code coverage. With unit tests, you will be able to understand the paths you have to take through a code block. If the codes are complex, then it is not easy to get to the high degrees of unit test coverage. To know whether the code works correctly, will depend on how complex the code is. 

The software program is tested before the real version is out

Who doesn’t like to test something before they actually do it? Whether it is a car or a piece of cosmetic, it is only natural testing them would give better information on how they would work.  With unit testing, it is possible to exercise the code, and check whether it works perfectly well within the conformity of its parameters. 

There are people who don’t support unit testing, and they tell you that your software delivery would get indefinitely postponed because unit tests are difficult to write, and it is not easy to cross all the scenarios to reach the final delivery stage. But if you do not do unit testing, and get into the market with an untested product, failure is not far behind. 

Documentation

Who doesn’t like documentation? Unit testing and its results are almost like documentation because developers can see how a software is supposed to work, what can go wrong, and how it can be fixed. If a new developer joins the company at any time, looking at this documentation would give them a better chance at understanding how a particular software was built. 

Understanding the effort and time required to do a particular change 

The only thing consistent with a software program is the changes that come to it with time. With unit tests, the developers would get an idea of how much effort is needed to make the changes work. Unit tests give values on how this can be done, and if the values aren’t good, you will know the tests have failed, and the way to make the changes has to be something else. 

Conclusion

With the above reasons, it is obvious unit testing is an integral part of software development. 

When every function is tested separately, then it will help detect bugs and fix them. However, it is important to be very rigorous in discipline and consistency if unit testing is to be done successfully. There are so many tools around for software testing, but preparing good tests require skill and practice. 

Interesting Links:

Unit Testing – What is Its Importance in Software Testing?

More information about Unit Testing

Pictures: Canva

The author: Sascha Thattil works at Software-Developer-India.com which is a part of the YUHIRO Group. YUHIRO is a German-Indian enterprise which provides programmers to IT companies, agencies and IT departments.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.