Delphi unit test tool DUnit Introduction

xiaoxiao2021-03-06  40

Delphi unit test tool DUnit Introduction

Dunit Basic Introduction

Dunit is a member of the XUnit family for DEPHI unit testing. It is a tool for Extreme Programming Test to implement Xtreme Testing. Dunit is a FREE test tool without code coverage.

Dunit's official Web Site is https://sourceforge.net/projects/dunit/.

Use Dunit to take a look at DOC / readme.html in the DUnit installation directory. This article is also written in Readme.

Configure the test environment

Download DUNIT should be decompressed before using Dunit. Then add DUnit's path to the library-> library path in the menu Tools-> Environment Options.

Main file of dunit

File

Description

Testframework.pas

The Framework itself.

TESTEXTENSIS.PAS.PAS

Decorator classes That May Be buy to extend Test Cases.

Guitesting.pas

Classes for Testing User Interfaces (Forms and Dialogs).

TextTestrunner.pas

Routines to run tests in console mode.

Guitestrunner.pas

The Graphical User Interface to the framework ..

Guitestrunner.dfm

The Guitestrunner Form

Dunit Basic Realization Method (GUI Mode)

The basic implementation of DUnit is to separate the test code (unit) and the test code (unit). Provide an Framework and an run interface. All test units should inherit TTestCase.

Run the GUI interface

Run Testcase

What you should pay attention to here is that the setup method and Teardown are called for each test method. If you want to run SETUP and TEARDOWN, you should use the TTestSetup class, and the "DUnit Additional Function" section later.

Create a simple example

Create a tested Project

Create a Project called BetestProject, save the province's Unit1 as a BetestUnit.PAS file. Remove the province's TFORM1 to get a PUBLIC's function betersTFunction, the betestFunction code is as follows:

Function Betestform.betestFunction (i, j: integer): integer;

Begin

Result: = i * j;

END;

Create a test Project

Create a new Project

Create a Project, named TestProject. If you don't place with BetestProject, add the storage path of BetestProject to the library-> library path in the menu Tools-> Environment Options.

Write Testcase

Delete the provincial Unit1 (Form1), create a Unit, and be careful not for Form.

Save the created Unit as Testunit, add the following code in Interface

Uses

Testframework, betestunit;

TestFramework is every TestCase must be used, and the definition of the TTestCase and other classes to be used later are in the TestFramework. BetestUnit is the test unit to be tested.

Define TestCase, the test class definition code is as follows: TTestCaseFirst = Class (TTESTCASE)

Private

Betestform: Tbertestform; // To test the class

protected

Procedure setup; override; // Initialization class

Procedure Teardown; Override; // Clear Data

Published

Procedure testfirst; // First test method

Procedure testsecond; // second test method

END;

When defining a test method, Dunit is looking for and automatically registered with RTTI (Runtime Type Information), and the specific implementation is through code.

Testframework.registerTest (TTestCaseFirst.suite);

This code will be mentioned later, TTestCaseFirst.suit is looking for rules:

1. Test method is a procedure without parameters

2, the test method is declared as public as Published

Setup, Teardown is before running the test method, all the initialization and clearance of the class to be tested before, and all the initialization and clearing of the class to be tested.

The following is the code implemented:

Procedure ttestcasefirst.setup;

Begin

Betestform: = TbertestForm.create (NIL);

END;

Procedure ttestcasefirst.teardown;

Begin

Betestform.destroy;

END;

Procedure ttestcasefirst.testfirst; // First test method

Begin

Check (Betestform.betestFunction (1, 3) = 3, 'First Test Fail');

END;

Procedure ttestcasefirst.testsecond; // second test method

Begin

Check (Betestform.betestFunction (1, 3) = 4, 'Second Test Fail');

END;

// register testcase

INITIALIZATION

Testframework.registerTest (TTestCaseFirst.suite);

End.

Check is a method provided by the TestCase class. The following is the implementation code of TestCase:

Procedure TTestCase.check (Condition: Boolean; Msg: String);

Begin

IF (Not Condition) THEN

Fail (MSG, CalleRaddr);

END;

If Check is not passed, Dunit will report an error. Error prompts are defined in the second parameter, other related classes and methods, please see the connecting machine document, documentation

DUnit installation directory /Doc/api/idh_library_dunit_-_xtreme_Unit_testing_for_delphi.htm

The Initialzation code completes the registration of the test unit.

Modify Project main file

The last step before run is to modify the Project main file TestProject.dprise. First use the menu project-> view source to open TestProject.dpr.

The modified code is as follows:

Program TestProject;

Uses

Forms,

Testframework,

Guitestrunner,

Testunit in 'testunit.pas';

{$ R * .res}

Begin

Application.INITIALIZE; // Application.run;

Guitestrunner.RunregisteredTests;

End.

The above bold code is to increase and modify.

Run test example

The results of the test are as follows:

Use TestSuite

The purpose of using TestSuite is to manage TestCase, if we add a Testcase as follows

TTestCaseSecond = Class (ttestcase)

Published

PROCEDURE TESTHRID;

END;

After adding the testthrid to implement the code, increase the Initialization code

Testframework.registerTest (TTestCaseSecond.suite);

After running, we can see the results as follows:

If we change the code at the initialization as follows:

INITIALIZATION

Testframework.registerTest ('Simple Suite', TTestCaseFirst.suite);

Testframework.registerTest ('Simple Suite', TTestCaseSecond.suite);

End.

The result of running is as follows:

This is a simple TestSuite used, we put TestCaseFirst and TestCaseSecond to SIMPLE Suite to manage.

For complex applications, we can also use multi-layer TestSuite to manage. Add a function first:

Function UnitTests: Itestsuite;

VAR

AtestSuite, Btestsuite: TTESTSUITE

Begin

Btestsuite: = TTESTSUITE.CREATE ('Some Trivial Tests ",

[

TTestCaseFirst.suite,

TTestCaseSecond.suite

]);

AtestSuite: = TTestSuite.create ('Some Other Tests');

AtestSuite.Addtest (TTestCaseFirst.suite);

AtestSuite.addtest (btestsuite);

Result: = atestsuite;

END;

Let's create a layer of TestSuite, BtestSuite. Then add BtestSuite to ATestSuite.

Finally, change the code at the initialization as follows:

INITIALIZATION

Testframework.registerTest ('Simple Test', UnitTests);

End.

The registration of AtestSuite is OK, the following is the result:

Console mode

If you want to run TestCase directly in the DOS mode, just modify the DPR file.

{$ Apptype console}

Program TestProject;

Uses

Forms,

Testframework,

Guitestrunner,

TextTestrunner,

Testunit in 'testunit.pas';

{$ R * .res}

Begin

Application.INITIALIZE;

// guitestrunner.runregisteredTests;

TextTrunner.RunregisteredTests;

End.

First define the application type, add {$ apptype console}, and then use TextTestrunner to replace Guitestrunner. In case, the test program will give a report after running all TestCase. If you want to reach a certain error, stop running, you can use

TextTestrunner.RunregisteredTests (RXBhaltonfailures);

Dunit Additional Function

Use Dunit's additional features to join in Usees:

TESTEXTENSIS, // NEeded for TrepeatedTest

The main additional features of DUnit are:

1. Repeat a Testcase

2, use TTestSetup class initial test

Dunit's Testextensions also mentioned two class TactiveTest, TexceptionTestCase implementation:

3, run test in standalone thread

4, Exception test

But in Dunit's latest source code, these two classes simply inherited TTestDecorator without any modifications, and these two classes are not mentioned in Dunit's readme. Therefore, it should belong to a class that has not been implemented.

Repeat Testcase

To run a TestCase repeatedly, you only need to register the registration code in Initialization

Testframework.registerTest (TTestCaseFirst.suite);

Simple replacement:

Testframework.registerTest (TtestCasefirst.Suite, 2));

Yes, the first parameter of TrepeatedTest.create is the TestSuite / TestCase to be repeated, the second parameter represents the number of times. The results after run are as follows:

Please note that more "2x" in front of TestCaseFirst.

Use the TTestSetup class

The role of using the TTESTSETUP class is to run a setup several TEARDOWN before and after running all test methods. Can be used to create database connections, etc.

To use TTestSetup, we first declare a new class in the testUnit created in the "Create a simple example" section (first add Dialogs, Testextensions in USES).

TestSetupTest = Class (TTestSetup)

protected

Procedure setup; override; // Initialization class

Procedure Teardown; Override; // Clear Data

END;

Join the implementation code

Procedure TestSetupTest.Setup;

Begin

ShowMessage ('TestSetupTest Setup');

END;

Procedure TestsetupTest.teardown;

Begin

ShowMessage ('TestSetupTest TEARDOWN');

END;

Modify TTestCaseFirst.Setup and TTestCaseFirst.teardown, add the following to bold statements.

Procedure ttestcasefirst.setup;

Begin

Betestform: = TbertestForm.create (NIL);

ShowMessage ('TTestCaseFirst setup');

END;

Procedure ttestcasefirst.teardown;

Begin

Betestform.destroy;

ShowMessage ('TTestCaseFirst Teardown');

END;

Finally change the initialization to

INITIALIZATION

//Testframework.registertest (TestCaseFirst.suite) ;TestFramework.registerTest (TestSetupTest.create (TTESTSEFIRST.SUITE));

End.

The results after run are as follows:

Note that "[D]" is added in front of TTestCaseFirst. When you run a test, you can clearly see the TestSetupTest class only run once, while the setup and Teardown in TTestCaseFirst run twice.

Test EXCEPTION

Although TexceptionTestCase is not implemented, Dunit has an example of how to test Exception in the source code / Examples / TESTEXCEPTION directory.

The main implementation is in Procedure TtestmyObject.checkexception and Procedure TtestmyObjectOverriderUntest.Runtest. Specific implementation can be seen from the code.

转载请注明原文地址:https://www.9cbs.com/read-69988.html

New Post(0)