Go Up to DUnit Overview
New for 2010! Video: Delphi Unit Tests, by Mike Rozlog
The structure of a unit test largely depends on the functionality of the class and method you are testing. The Unit Test Wizards generate skeleton templates for the test project, setup and teardown methods, and basic test cases. You can then modify the templates, adding the specific test logic to test your particular methods.
The following describes the procedures for creating a Unit Test Project and a Unit Test Case. Follow these procedures in the order shown. You must create the Unit Test Project prior to creating the associated test cases. The Unit Test Case Wizard is available only if the current active project is a Unit Test Project.
To create a test project
- Choose File > New > Other.
- Open the Unit Test folder.
- Double-click the Test Project gallery item. This starts the Test Project Wizard and displays the Specify Test Project Details page.
- Fill in the appropriate details or accept the defaults. Enter the following:
- Project name: Enter the name for the new test project, or accept the default. The default is the name of the active project with the word Tests appended to the name. If there is no active project, the default will be UnitTest with a sequence number appended.
- Location: Enter the full pathname for the folder in which to create the test project, or accept the default. The default is a subfolder named test under the active project folder. If there is no active project, then the default is the default project folder. You can click the ellipsis (...) to display a Browse dialog box from which you can select the location.
- Personality: Select the personality (code language) from the drop down list, or accept the default. The default is the personality of the active project.
- If you do not want the test project added to your project group, uncheck the Add to Project Group check box.
- Click Next to proceed, or click Finish to accept the remaining defaults. If the Finish button is available, you can click it at any point to accept the default values for any remaining fields and immediately generate the new test project. Otherwise, click Next to proceed to the Specify Test Framework Options page.
- Fill in the appropriate details or accept the defaults.Enter the following:
- Test Framework: For the Delphi and C++ personalities, only the DUnit framework is supported, so you cannot change this value.
- Test Runner: Choose either GUI or Console from the drop-down list. The Console Test Runner directs output to the console. The GUI Test Runner displays the results interactively in a GUI window, with results color-coded to indicate success or failure.
- Click Finish. The Test Project Wizard generates the test project template. For Delphi, this also adds the necessary source references into the test project template; you can skip the next step. For C++Builder, you must manually link the test project with the C++ classes you want to test; proceed to the next step.
- For C++Builder, only: Link the new test project to the source to be tested. For C++Builder, you must manually link the test project with the C++ code you want to test. You can use any of the following techniques to do this, as appropriate:
- Add the C++ code directly to the test project.
- Add an .obj file to the test project.
- Add a .lib file (either a static library or an import library for a DLL) to the test project.
- Include the header file that implements the class you want to test. See the next section, Adding files to a test project, for instructions.
Adding files to a test project
- Open the test project file and activate it. To activate the file, select the file in the Project Manager and click the Activate button.
- In the Project Manager, right-click the test project name. This displays a pop-up menu of project operations.
- Choose Add.... This displays the Add to Project file browser, from which you can select the file to include in your test project.
- Select the file to add and click OK. This adds the select file to the test project.
To create a test case
- Click the Code tab for the file containing the classes you want to test. This makes the file active in the Code Editor.
- Choose File > New > Other.
- Open the Unit Test folder.
- Double-click the Test Case gallery item. This starts the Test Case Wizard and displays the Select Methods to Test page.
- Enter the name of the source file that contains the classes you want to test. You can click the ellipsis (...) to display an Open dialog box, from which you can select the file.
- Select the classes and methods for which you want to create tests. In the Available classes and methods list, click the check box next to an item to select or deselect it. By default, all classes and methods are selected. You can deselect individual methods in the list. The wizard generates test case templates for the checked (selected) methods only. If you deselect a class, the wizard ignores the entire class and all of its methods, even if you do not deselect the methods. If you select a class but do not select any methods in that class, the wizard generates a test case for the class, but does not generate any test methods for that class.
- Click Next to proceed, or click Finish to accept the remaining defaults. If the Finish button is available, you can click it at any point to accept the default values for any remaining fields and immediately generate the new test case. Otherwise, click Next to proceed to the Specify Test Case Details page.
- Fill in the appropriate details or accept the defaults.
- Test Project: Select the test project from the drop down list. The default is the active test project; if you just created a test project using the Test Project Wizard, then the new test project is the default.
- File name: Enter a filename for the test case you are creating or accept the default. The default is the name of the source file to be tested, with the prefix Test added to the name.
- Test Framework:For the Delphi for Win32 and C++ personalities, only the DUnit framework is supported, so you cannot change this value.
- Base Class: Select the base class from the drop down list or accept the default. The default is TTestCase, which is the default base TestCase class. In most cases, you can use the default. However, you can specify a custom TTestCase class that you have created. In addition, if you are testing a hierarchy of objects, you can derive the new test case from the TestCase class of a base object of the object being tested. This allows a derived class to inherit a test created for the base type for that class.
- Click Finish. The wizard generates a test case file with the name you specified.
To write a test case
- Add code to the SetUp and TearDown methods in the test case template(s), if needed.
- Add asserts to the test methods.
To run a test case in the GUI Test Runner
- Activate the file containing the classes you want to run. Select the file in the Project Manager and then click the Activate button.
- Choose Run > Run. The GUI Test Runner starts up immediately on execution of your application.
- Select one or more tests from the tests list.
- Click the Run button. The test results appear in the Test Results window. Any test highlighted with a green bar passed successfully. Any test highlighted in red failed. Any test highlighted in yellow was skipped.
- Review the test results.
- Fix the bugs and rerun the tests.