Unit Testing Overview
Go Up to DUnit Overview
New for 2010! Video: Delphi Unit Tests, by Mike Rozlog
RAD Studio integrates an open-source testing framework, DUnit, for developing and running automated test cases for your applications. The DUnit framework is available for Delphi and C++. This framework simplifies 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.
The DUnit testing framework is based on the JUnit test framework and shares much of the same functionality.
This topic includes the following information:
- What Is Installed
- Test Projects
- Test Cases
- Test Fixtures
To install the DUnit feature follow these steps:
- Select Tools > Manage Platforms to open the Feature Manager.
- On the Additional Options tab, check the DUnit Testing Frameworks feature to add it to your RAD Studio installation.
- Select Apply to apply the selected changes.
When the changes are applied, the Feature Manager shows an Operation Complete screen.
What Is Installed
By default, the DUnit framework is installed during the complete RAD Studio installation or during installation of either of the individual personalities (Delphi or C++Builder).
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:
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 Version 1.1.
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.