White paper on unit testing

Introduction

Project manager is accountable for quality of software delivered to customer. But in most cases project manager doesn’t have confidence on the quality of code written. The project manager is never confident on last minutes change or refactoring of code due to the risk of breaking working software. Another situation when a project manager sweats when a critical member of team leaves the project at crucial time of project. Apart from the on boarding time and bringing the person speed to project, the challenge is the how well the new person can fix any new issue or enhance existing feature without introducing any defect.

Most of the time project manager is not confident when a developer or tech lead or architect propose code refactoring. This is due to lack of tool which can measure the changes done won’t break the existing functionality.

The reliability, quality and speed to market is one of the major differentiating factor in any industry and it holds good for software industry as well. The world is changing in rapid pace and so the technology, you need to step up with the change to survive and give sustainable growth to organization.

What is Unit-Testing?

Units are the smallest piece of code written in a function or method in a module of any software. A single unit should be the most granular part of any module. Unit test is a test of the smallest and most granular method. This unit test verifies the positive, negative and exception scenario of the smallest unit. This is done by developer before integration testing of a module or whole software. Unit testing takes time but it reduces the effort of integration time by reducing the number of defects being logged and closing defects after re-testing. The less number of defects at the end of sprint or project gives confidence to project manager and team about the quality of software being shipped.

Unit testing is first stage of testing of any written code. It is always cost effective to fix the bug early in development lifecycle, if any bug found later stage it requires complete analysis to fix the bug. It takes more time if the bug is being fixed by someone other than who is developed.

As per Capers Jones, the relative cost of fixing defect increases with the progress of software development life cycle.

 

Any defect fixing in any stage follows the previous stage. E.g. if a defect is found in Regression test; the software goes through unit test, function test and regression test after bug is fixed. Similarly, if defect is fixed in System test; the software goes through unit test, functional test, regression test, performance test and system test.

The right way of applying unit test in software development is three step process.

  1. Test driven development or Test First
  2. Use test framework
  3. Automate unit tests

 

Test driven development or Test First

In test driven development (TDD) unit test is written first and then code. It is obvious that compilation of test will fail as there is no code to test. This sounds silly but here is the trick where one has to start thinking about number of parameters needs to be passed, type of parameters need to be passed and the return type of method.

Now to fix the compilation issue and to run the test we need to write method with the signature and the return value. To pass the test developer can hard code the return value for time being. Developer feels good when test passes with hard coded value, isn’t it? Next step is to replace the hard coded value with the real return value of the method. A developer can skip the step of hard coding return value of a method. This is individual developer choice whether to hard code a return value and make the test pass and as a next step modify the method with correct return value.

Writing unit test first makes developer to put on thinking hat before writing method. This thinking process of developers makes sure the smallest unit is written as a method. The other advantage of smallest unit of method is the responsibility of unit is clearly defined. When responsibility of a method is clearly defined it can be bind to any other of the system. The loose coupling is one of the attribute of good design.

Unit test framework

Unit test framework simplifies the unit testing. There are lot of unit testing framework available for different languages which should be leveraged with its true potential. Most of the unit test framework can be integrated to IDE and has user friendly interface. The unit test framework provides test suite which runs all the unit test in the suite by single click or command.

Another advantage of unit test framework is it lets you determine the code coverage of the solution. This gives quantitative data about how much code is covered using unit test.

Here is list of unit testing framework of different languages

https://en.wikipedia.org/wiki/List_of_unit_testing_frameworks

Automate unit test

Automating unit test with build will make sure the test written is not failing. If a unit test is failing there are high chances application is breaking. As a result the build can be stopped moving to next environment for functional, integration, system, UAT, etc. If the solution size is big automated test suite should run with build as nightly job so that developer can fix any issues in next morning.

Another approach is to run the unit test in regular interval and log the failed test case for review. By this approach developer don’t need to run the entire suite to find the failed test cases.

 

Conclusion

Unit test is part of development of software which saves costs down the line and improves code quality. It gives confidence of code refactoring and enhancement of features with less effort.

It has initial cost because it takes significant time to write unit test and learning curve. But when you see holistic picture of overall software development life cycle it saves cost and improve the quality of developed software significantly.

Very nice article Sumit. So true. TDD is the way to go!!

Like
Reply

To view or add a comment, sign in

More articles by Sumit Dutta

  • Xamarin after Microsoft's Acquisition

    16th May, 2011 was the day when Xamarin launched to develop cross platform apps. The time when mobility had just…

    3 Comments

Others also viewed

Explore content categories