Facts About Unit Testing

Subu Iyer
What is Unit Testing?

Unit Testing is the process of testing small, individual components of a software application. Testing a function or a class by itself falls under unit testing. Unit testing involves writing code to test code as there is no user interface to manually test these individual components. Typically, unit testing is a task performed by developers, but coders in the testing team could easily add variations to the unit tests to improve test coverage.

Why Unit Test?

Unit Testing helps to verify the correctness of individual components. It helps to isolate components and pin-point issues. Finding issues by unit testing is easier and quicker than identifying issues in the application, with all components working together. Also, automated unit tests help a great deal during regression. Imagine testing all features of the application after a minor change, modification or bug fix, it would be a nightmare!

How do you Unit Test?

Unit Tests should be written before programming and coded along with the new functionality. So a new or modified code block is accompanied by its unit test case. Write unit tests for code that is exposed, so public methods, documented parts of an API, etc should have unit tests. Write your unit tests to a standard and common interface like jUnit, nUnit, etc. even if it is only a handful of tests. Don't create test cases with their own main methods! Very soon your project code base will grow and the number of unit tests will also go up and running the unit tests will require significant time and effort. Pick a standard test harness to run your tests, don't write your own! Such proven and widely used test infrastructure also comes with neat features to report test results. Unit tests should not require major environment setup. If you are testing parts of an enterprise application, use mock objects in your tests. Don't write application specific unit tests. Write unit tests to test the correctness of modules. Testing how the modules behave together in the application can be done later in Integration Tests or Functional Tests.

Unit Testing has become more important and relevant in newer development methodologies like Test Driven Development, eXtreme Programming, etc. The benefit of this approach (as opposed to practices where you write functional code and then think about testing) is that you can take smaller, quicker steps to reach your goal. Unit testing your code helps to find issues at step one and the turn around time to fix the problems is also very short. Refactoring is easier as you have a suite of tests to confirm that you did not break anything. While Unit Testing still needs to be complimented with Integration and Functional Testing, it is a good start to a successful project.

Published by Subu Iyer

1  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.