RAD Studio (Common)
ContentsIndex
PreviousUpNext
Unit Testing Overview

RAD Studio integrates two open-source testing frameworks, DUnit and NUnit, for developing and running automated test cases for your applications. The DUnit framework is available for Delphi and C++. The NUnit framework is available for Delphi for .NET and C# only. These frameworks simplify the process of developing tests for classes and methods in your application. Using unit testing in combination with refactoring can improve your application stability. Testing a standard set of tests every time a small change is made throughout the code makes it more likely that you will catch any problems early in the development cycle. 

Both testing frameworks are based on the JUnit test framework and share much of the same functionality. 

This topic includes the following information:

  • What Is Installed
  • Test Projects
  • Test Cases
  • Test Fixtures

By default, both frameworks are installed during the complete RAD Studio installation. When installing individual personalities, the test framework(s) supported by those personalities will be installed.

DUnit

For Delphi and C++Builder, the DUnit framework is installed automatically by the RAD Studio installer. You can find many DUnit resources in the \source\DUnit directory, under your installation root directory. These resources include source files, documentation, and test examples. For C++Builder, the following C++ header and library files are also provided for use as C++ test projects:

  • GUITestRunner.hpp
  • XMLTestRunner.hpp
  • TextTestRunner.hpp
  • TestFramework.hpp
  • DUnitMainForm.hpp
  • DUnitAbout.hppdir
  • dunitrtl.lib
Note: These files are not part of the standard DUnit distribution. These files are prebuilt by CodeGear and included with C++Builder for your convenience.
In general when using DUnit, include at least one test case and one or more test fixtures. Test cases typically include one or more assertion statements to verify the functionality of the class being tested. 

DUnit is licensed under the Mozilla Public License 1.0 (MPL).

NUnit

The NUnit framework is available for the Delphi for .NET and C# personalities only. 

During the installation process, you are prompted to install NUnit, which you can accept or decline. You can change the location for installing NUnit, or you can accept the default; the default is C:\Program Files\NUnit V2.x, where x is a point release number. The installation directory includes a number of resources including documentation and example tests. 

NUnit is the name of the .NET testing framework and can be used with both Delphi for .NET and C# projects. There are some subtle but important differences between the way NUnit and DUnit work. For example, NUnit does not link in .dcu files, but DUnit does.  

In general when using NUnit, include at least one test case and one or more test fixtures. Test cases typically include one or more assertion statements to verify the functionality of the class being tested.

A test project encapsulates one or more test cases and is represented by a node in the IDE Project Manager. RAD Studio provides the Test Project Wizard, which you can use to create a basic test project. Once you have a test project that is associated with a code project, you can create test cases and add them to the test project.

In a typical unit test project, each class to be tested has a corresponding test class; however, this is not required. The test class is also referred to as a test case. Depending on which framework you are using, the test class may be derived from a specific test case base class. In general, a test case has a set of one or more methods that correspond to one of the methods in the class to be tested. More than one test case can be included in a test project. This ability to group and combine tests into test cases—and test cases into test projects—is what sets a test case apart from simpler forms of testing (such as using print statements or evaluating debugger expressions). Each test case and test project can be reused and rerun, and can be automated through the use of batch files, build scripts, or other types of testing systems. 

Generally, it is recommended that you create your tests in a project separate from the source file project. That way, you do not have to go through the process of removing your tests from your production application. RAD Studio provides the Test Case Wizard to help you create basic test cases, which you can then customize as needed.

The term test fixture refers to the combination of multiple test cases, which test logically related functionality. You can define test fixtures in your test case. Typically, you will instantiate your objects, initialize variables, set up database connection, and perform maintenance tasks in the SetUp and TearDown sections. As long as your tests all act upon the same objects, you can include a number of tests in any given test fixture.

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!