Page 165 -
P. 165
TABLE 7-9. Test frameworks available for languages
Language Framework name (URL)
Java JUnit (http://www.junit.org)
Visual Studio .NET NUnit (http://www.nunit.org)
C CUnit (http://cunit.sourceforge.net)
C++ CppUnit (http://cppunit.sourceforge.net)
SmallTalk SUnit (http://sunit.sourceforge.net)
Perl Test (http://search.cpan.org/~sburke/Test)
Python PyUnit (http://pyunit.sourceforge.net)
Borland Delphi DUnit (http://dunit.sourceforge.net)
Test All of the Code, Test All of the Possibilities
The name “unit test” comes from the fact that each individual unit of code is tested sepa-
rately. In object-oriented languages like Java, C#, and SmallTalk, the units are objects. In
imperative languages like C, the units will correspond to functions or modules; in func-
tional languages like Lisp and SML, the units will generally be functions. (Some lan-
guages, like Visual Basic and Perl, can be either imperative or object-oriented.)
It takes multiple tests to verify a single unit. The framework will have a way to build suites
of test cases and indicate that they correspond to a specific unit.
A good test verifies many aspects of the software, including (but not limited to) these
attributes:
• The unit correctly performs its intended functions.
• The unit functions properly at its boundary conditions (like null or zero values).
• The unit is robust (it handles unexpected values and error conditions gracefully).
Unit tests must be able to run within a developer’s test environment. Real-time or produc-
tion resources like databases, data feeds, input files, and user input are not necessarily
available to the test. To get around this limitation, a programmer can use a mock object—an
object that simulates a resource that is unavailable at the time of the test. (It is beyond the
scope of this book to describe how to implement mock objects.)
JUnit
JUnit is the unit testing framework for Java. It was created by Erich Gamma and Kent
Beck, based on Beck’s work with SmallTalk. JUnit has been very influential in the world
of unit testing; many unit test frameworks are ported from, or based on, JUnit. The test
case examples below are JUnit test cases, which are part of an automated suite of unit tests
for the FeeCalculation( ) function above. The unit tests allowed the programmers doing a
code review of this function to successfully refactor it without injecting defects. After each
refactoring, they executed the unit tests. If any of them failed, the programmers tracked
down the problem and fixed it.
DESIGN AND PROGRAMMING 157