Friday, September 09, 2005

Unit Testing

Unit Testing

Definition - Unit Test can be defined as running one component of a system for testing purposes.

In computer programming, a unit test is a method of testing the correctness of a particular module of source code.

The idea is to write test cases for every non-trivial function or method in the module so that each test case is separate from the others if possible.

What is Unit Testing?

When we write programs we can not make them perfect from the first time. When you compile your project you face the first test for your application. The compiler checks the syntax and stops if it contains errors. This is a nice assistance but it does not end here. The program may contain bugs, and it CERTANLY contains bugs. Then we have to hunt them down and correct the application so the bug count is low enough. We can not correct all the bugs but we can reduce them to a decent level.So here come the Unit Tests. They help us hunt the bugs by writing tests. So the tests are programs, methods actually. They test every part of our program to see if it works right.

Benefits of Unit Testing

The goal of unit testing is to isolate each part of the program and show that the individual parts are correct. It provides a written contract that the piece must satisfy. This isolated testing provides two main benefits:
1. Encourages change
Unit testing allows the programmer to refactor code at a later date, and make sure the module still works correctly (regression testing). This provides the benefit of encouraging programmers to make changes to the code since it is easy for the programmer to check if the piece is still working properly.
2. Simplifies Integration
Unit testing helps eliminate uncertainty in the pieces themselves and can be used in a bottom-up testing style approach. By testing the parts of a program first and then testing the sum of its parts will make integration testing easier.

Limitations of Unit Testing

It is important to realize that unit-testing will not catch every error in the program. By definition, it only tests the functionality of the units themselves. Therefore, it will not catch integration errors, performance problems and any other system-wide issues. In addition, it may not be trivial to anticipate all special cases of input the program unit under study may receive in reality. Unit testing is only effective if it is used in conjunction with other software testing activities.

No comments: