TestExtensions Unit
Classes Interfaces
DUnit: An XTreme testing framework for Delphi programs.

Classes
TActiveTest  A test decorator for running tests in a separate thread
TExceptionTestCase  A test decorator for running tests expecting a specific exceptions to be thrown.
TRepeatedTest  A test decorator that runs a test repeatedly.
TTestDecorator A Decorator for Tests.
TTestSetup  A Decorator to set up and tear down additional fixture state.

Interfaces
ITestDecorator General interface for test decorators

Author
The DUnit Group.


HTML generated by Time2HELP
http://www.time2help.com
TActiveTest Object
A test decorator for running tests in a separate thread

Unit
TestExtensions

Declaration
TActiveTest = class(TTestDecorator)

Todo


HTML generated by Time2HELP
http://www.time2help.com
TExceptionTestCase Object
A test decorator for running tests expecting a specific exceptions to be thrown.

Unit
TestExtensions

Declaration
TExceptionTestCase = class(TTestDecorator)

Todo


HTML generated by Time2HELP
http://www.time2help.com
TRepeatedTest Object
Methods
A test decorator that runs a test repeatedly.

Unit
TestExtensions

Declaration
TRepeatedTest = class(TTestDecorator)

Description
Use TRepeatedTest to run a given test or suite a specific number of times.

Introduced Methods
CountEnabledTestCases
CountTestCases
Create  Construct decorator that repeats the decorated test.
GetName
RunBare


TRepeatedTest Example

    function UnitTests: ITestSuite;
    begin
      Result := TRepeatedTest.Create(ATestArithmetic.Suite, 10);
    end;


HTML generated by Time2HELP
http://www.time2help.com
CountEnabledTestCases method
Overrides the inherited behavior to included the number of repetitions.

Applies to
TRepeatedTest

Declaration
Function CountEnabledTestCases: integer;

Returns
Iterations * inherited CountEnabledTestCases

Implementation

function TRepeatedTest.CountEnabledTestCases: integer;
begin
  Result := inherited CountTestCases * FTimesRepeat;
End;


HTML generated by Time2HELP
http://www.time2help.com
CountTestCases method
Overrides the inherited behavior to included the number of repetitions.

Applies to
TRepeatedTest

Declaration
Function CountTestCases: integer;

Returns
Iterations * inherited CountTestCases

Implementation

function TRepeatedTest.CountTestCases: integer;
begin
  Result := inherited CountTestCases * FTimesRepeat;
End;


HTML generated by Time2HELP
http://www.time2help.com
Create method
Construct decorator that repeats the decorated test.

Applies to
TRepeatedTest

Declaration
Constructor Create(ATest: ITest; Iterations: integer; AName: string = '');

Description
The ITest parameter can hold a single test or a suite. The Name parameter is optional.

Parameters
ATest The test to repeat.
Itrations The number of times to repeat the test.
AName An optional name to give to the decorator instance

Implementation

constructor TRepeatedTest.Create(ATest: ITest; Iterations: integer;
  AName: string);
begin
  inherited Create(ATest, AName);
  FTimesRepeat := Iterations;
End;


HTML generated by Time2HELP
http://www.time2help.com
GetName method

Applies to
TRepeatedTest

Declaration
Function GetName: string;

Implementation

function TRepeatedTest.GetName: string;
begin
  Result := Format('%d x %s', [FTimesRepeat, getTest.Name]);
End;


HTML generated by Time2HELP
http://www.time2help.com
RunBare method
Overrides the behavior of the base class as to execute the test repeatedly.

Applies to
TRepeatedTest

Declaration
Procedure RunBare(ATestResult: TTestResult);

Implementation

procedure TRepeatedTest.RunBare(ATestResult: TTestResult);
var
  i: integer;
begin
  assert(assigned(ATestResult));

  for i := 0 to FTimesRepeat - 1 do
  begin
    if ATestResult.shouldStop then
      Break;
    inherited RunBare(ATestResult);
  end;
End;


HTML generated by Time2HELP
http://www.time2help.com
TTestDecorator Object
Properties Methods
A Decorator for Tests.

Unit
TestExtensions

Declaration
TTestDecorator = class(TAbstractTest, ITest, ITestDecorator)

Description
Use TTestDecorator as the base class for defining new test decorators. Test decorator subclasses can be introduced to add behaviour before or after a test is run.

Introduced Public Properties
Test 

Introduced Methods
CountEnabledTestCases
CountTestCases
Create  Decorate a test.
GetName
GetTest
LoadConfiguration
RunBare
SaveConfiguration
Tests


HTML generated by Time2HELP
http://www.time2help.com
Test property

Applies to
TTestDecorator

Declaration
Property Test : ITest Read GetTest;


HTML generated by Time2HELP
http://www.time2help.com
CountEnabledTestCases method

Applies to
TTestDecorator

Declaration
Function CountEnabledTestCases: integer;

Implementation

function TTestDecorator.CountEnabledTestCases: integer;
begin
  if Enabled then
    Result := FTest.countEnabledTestCases
  else
    Result := 0;
End;


HTML generated by Time2HELP
http://www.time2help.com
CountTestCases method

Applies to
TTestDecorator

Declaration
Function CountTestCases: integer;

Implementation

function TTestDecorator.CountTestCases: integer;
begin
  if Enabled then
    Result := FTest.countTestCases
  else
    Result := 0;
End;


HTML generated by Time2HELP
http://www.time2help.com
Create method
Decorate a test.

Applies to
TTestDecorator

Declaration
Constructor Create(ATest: ITest; AName: string = '');

Description
If no name parameter is given, the decorator will be named as the decorated test, with some extra information prepended.

Parameters
ATest The test to decorate.
AName Optional name to give to the decorator.

Implementation

constructor TTestDecorator.Create(ATest: ITest; AName: string = '');
begin
  if AName <> '' then
    inherited Create(AName)
  else
    inherited Create(ATest.name);
  FTest := ATest;
  FTests:= TInterfaceList.Create;
  FTests.Add(FTest);
End;


HTML generated by Time2HELP
http://www.time2help.com
GetName method

Applies to
TTestDecorator

Declaration
Function GetName: string;

Implementation

function TTestDecorator.GetName: string;
begin
  Result := Format('[d] %s', [getTest.Name]);
End;


HTML generated by Time2HELP
http://www.time2help.com
GetTest method

Applies to
TTestDecorator

Declaration
Function GetTest: ITest;

Implementation

function TTestDecorator.GetTest: ITest;
begin
  Result := FTest;
End;


HTML generated by Time2HELP
http://www.time2help.com
LoadConfiguration method

Applies to
TTestDecorator

Declaration
Procedure LoadConfiguration(const iniFile :TIniFile; const section :string);

Implementation

procedure TTestDecorator.LoadConfiguration(const iniFile: TIniFile; const section: string);
begin
  FTest.LoadConfiguration(iniFile, section)
End;


HTML generated by Time2HELP
http://www.time2help.com
RunBare method
Overrides the inherited behavior and executes the decorated test's RunBare instead

Applies to
TTestDecorator

Declaration
Procedure RunBare(ATestResult: TTestResult);

Implementation

procedure TTestDecorator.RunBare(ATestResult: TTestResult);
begin
  FTest.RunBare(ATestResult);
End;


HTML generated by Time2HELP
http://www.time2help.com
SaveConfiguration method

Applies to
TTestDecorator

Declaration
Procedure SaveConfiguration(const iniFile :TIniFile; const section :string);

Implementation

procedure TTestDecorator.SaveConfiguration(const iniFile: TIniFile; const section: string);
begin
  FTest.SaveConfiguration(iniFile, section)
End;


HTML generated by Time2HELP
http://www.time2help.com
Tests method

Applies to
TTestDecorator

Declaration
Function Tests: IInterfaceList;

Implementation

function TTestDecorator.tests: IInterfaceList;
begin
   Result := FTests;
End;


HTML generated by Time2HELP
http://www.time2help.com
TTestSetup Object
Methods
A Decorator to set up and tear down additional fixture state.

Unit
TestExtensions

Declaration
TTestSetup = class(TTestDecorator)

Description
Subclass TestSetup and insert it into your tests when you want to set up additional state once before the tests are run.

Introduced Methods
Create
RunBare
Setup  Sets up the fixture.
TearDown  Tears down the fixture.


TTestSetup Example

    function UnitTests: ITest;
    begin
      Result := TSetubDBDecorator.Create(TDatabaseTests.Suite, 10);
    end; 


HTML generated by Time2HELP
http://www.time2help.com
Create method

Applies to
TTestSetup

Declaration
Constructor Create(ATest: ITest; AName: string = '');

Implementation

constructor TTestSetup.Create(ATest: ITest; AName: string = '');
begin
  inherited Create(ATest, AName);
End;


HTML generated by Time2HELP
http://www.time2help.com
RunBare method

Applies to
TTestSetup

Declaration
Procedure RunBare(ATestResult: TTestResult);

Implementation

procedure TTestSetup.RunBare(ATestResult: TTestResult);
begin
  try
    Setup;
  except
    on E: Exception do
      assert(false, 'Setup failed: ' + e.message);
  end;
  try
    inherited RunBare(ATestResult);
  finally
    try
      TearDown;
    except
      on E: Exception do
        assert(false, 'Teardown failed: ' + e.message);
    end;
  end;
End;


HTML generated by Time2HELP
http://www.time2help.com
Setup method
Sets up the fixture.

Applies to
TTestSetup

Declaration
Procedure Setup;

Description
Override to set up additional fixture state.


HTML generated by Time2HELP
http://www.time2help.com
TearDown method
Tears down the fixture.

Applies to
TTestSetup

Declaration
Procedure TearDown;

Description
Override to tear down the additional fixture state.


HTML generated by Time2HELP
http://www.time2help.com
ITestDecorator Interface
Properties Methods
General interface for test decorators

Unit
TestExtensions

Declaration
ITestDecorator = interface(ITest)

Introduced Properties
Test

Introduced Methods
GetTest Get the decorated test


HTML generated by Time2HELP
http://www.time2help.com
Test Property

Applies to
ITestDecorator

Declaration
Property Test : ITest Read GetTest;


HTML generated by Time2HELP
http://www.time2help.com
GetTest Method
Get the decorated test

Applies to
ITestDecorator

Declaration
Function GetTest: ITest;

Returns
The decorated test


HTML generated by Time2HELP
http://www.time2help.com