Introduction to Unit Testing

Unit TestingIn Test Driven Development, Unit Testing is one of its core action. What is unit testing?

The term Unit Testing was first coined by Kent Beck. He is also one of the original signatories of Agile Manifesto in 2001.

From the MSDN article on unit testing:

“The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. Each unit is tested separately before integrating them into modules to test the interfaces between modules. Unit testing has proven its value in that a large percentage of defects are identified during its use.
The most common approach to unit testing requires drivers and stubs to be written. The driver simulates a calling unit and the stub simulates a called unit. The investment of developer time in this activity sometimes results in demoting unit testing to a lower level of priority and that is almost always a mistake. Even though the drivers and stubs cost time and money, unit testing provides some undeniable advantages. It allows for automation of the testing process, reduces difficulties of discovering errors contained in more complex pieces of the application, and test coverage is often enhanced because attention is given to each unit.”

Source: MSDN

From Wikipedia :

“In computer programming, unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures are tested to determine if they are fit for use.[1] Intuitively, one can view a unit as the smallest testable part of an application. In procedural programming, a unit could be an entire module, but is more commonly an individual function or procedure. In object-oriented programming, a unit is often an entire interface, such as a class, but could be an individual method.[2] Unit tests are short code fragments[3] created by programmers or occasionally by white box testers during the development process.

Ideally, each test case is independent from the others. Substitutes such as method stubs, mock objects,[4] fakes, and test harnesses can be used to assist testing a module in isolation. Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended.”

Source: Wikipedia

It is quite clear that unit testing is a piece of code used to test other code. It is written for the most basic building block of code. I believe the keywords are atomic, isolation and test results.

Unit testing is performed at the atomic level, the code is isolated from the rest of the code and test results are used to determine if the code is good.

In extreme programming, it is stressed that the test code must be written before the code itself.

“Unit tests are one of the corner stones of Extreme Programming (XP). But unit tests XP style is a little different. First you should create or download a unit test framework to be able to create automated unit tests suites. Second you should test all classes in the system. Trivial getter and setter methods are usually omitted. You will also create your tests first, before the code. ”

Source: Extreme Programming

Now, in order to do unit testing, we do need a unit test framework. I did find a unit testing framework for PHP in PHPUnit.

“PHPUnit is a programmer-oriented testing framework for PHP.

It is an instance of the xUnit architecture for unit testing frameworks.”

Source: PHPUnit

A lot of tools for Unit Testing are written for Java, however there are plenty of ports to popular programming languages. Such as PHPUnit above, so a quick search on google should help you find a unit testing framework suitable for use in your chosen programming language.

Please share in the comment section below your favorite unit testing framework.