Unit Testing

Unit testing is a programming technique to verify that a software component is working as expected. Unit tests examine code at a granular level, and are typically small and should not take long to write. For example, a unit test may assert that a method or function is properly calculating the results of a nearest neighbor algorithm. The developer that wrote the code that is being tested should also write the unit test for that code.

Unit tests are often considered to be a developer s first line of defense against software defects. They are typically written in parallel to the code that is being tested. Some programmers even choose to write them first, and then develop code to meet the expectations of the test.

This strategy of writing the tests before or in parallel to the code yields many benefits. Developers are more likely to catch bugs early on in the development cycle because they are writing the test while the code is still fresh in their minds. In addition, developers spend more time scrutinizing and thinking about the code they just wrote thereby catching errors and shortcomings early. When it is time to wire together the application, many of the software defects will have been discovered yielding a more robust initial implementation. It is a mistake to try to leave unit test development to a later stage of the project. It will be tedious and difficult to remember the mechanics of the original code.If programmers are disciplined about their unit test development, they will eventually have a suite of unit tests. This suite will be invaluable as developers refactor their code at later stages of the project. If a developer is concerned about whether a code change will break the application, he/she can run the unit test suite to see if that refactoring impacted the codebase.

If a bug is discovered at a later stage of the project, it is good practice to isolate the bug with a unit test that exposes that bug. After the developer has corrected the defect, he/she will be assured that it will not emerge again, because the unit test is now part of the test suite.
In theory, unit tests are supposed to test code in isolation and should be independent as possible of other libraries or services. This line of thinking has the side-benefit of influencing developers to make code more modular, and configurable. Mock objects can assist in keeping the test isolated. A mock object is a software component that attempts to replicate a real object, and has mock or canned behavior. In general, developers should attempt to test in a reproducible, static environment and mock objects can help achieve that objective. In practice, it may be difficult or laborious to achieve this rigor. But just remember that some testing is better than no testing. Or an imperfect test is better than no test. Unit testing is an important tool for developers, but as with all techniques and practices, try to take a practical approach.

There are many unit test frameworks. Here are a few

There are appropriate and inappropriate uses of unit testing. It is best to first test boundary conditions and code that is most likely to break. By contrast, in an OO language, it is a waste of time to test getters and setters. Developers should strive to be efficient in their approach by focusing on code that is complex and error prone. With a limited time budget, following this advice will yield a good bang for the buck.

Unit tests are well adapted to testing model, middle-tier, or back-end code, but are not well suited for testing UIs or views. For UI testing choose another testing methodology.

Documentation