TextTestRunner Unit
Classes Types Routines
DUnit: An XTreme testing framework for Delphi programs.

Classes
TTextTestListener 

Types
TRunnerExitBehavior This type defines what the RunTest and RunRegisteredTests methods will do when testing has ended.

Routines
RunRegisteredTests 
RunTest Run the given test suite

Author
The DUnit Group.


HTML generated by Time2HELP
http://www.time2help.com
TTextTestListener Object
Methods

Unit
TextTestRunner

Declaration
TTextTestListener = class(TInterfacedObject, ITestListener)

Introduced Methods
AddError
AddFailure
EndTest
PrintErrors
PrintFailures
PrintHeader
Report
StartTest
TestingEnds
TestingStarts
TruncateString


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

Applies to
TTextTestListener

Declaration
Procedure AddError(error: TTestFailure);

Implementation

procedure TTextTestListener.AddError( error :TTestFailure);
begin
    write('E');
End;


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

Applies to
TTextTestListener

Declaration
Procedure AddFailure(failure: TTestFailure);

Implementation

procedure TTextTestListener.AddFailure(failure :TTestFailure);
begin
    write('F');
End;


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

Applies to
TTextTestListener

Declaration
Procedure EndTest(test :ITest);

Implementation

procedure TTextTestListener.EndTest(test: ITest);
begin

End;


HTML generated by Time2HELP
http://www.time2help.com
PrintErrors method
Prints the errors to the standard output

Applies to
TTextTestListener

Declaration
Function PrintErrors(r :TTestResult): string;

Implementation

function TTextTestListener.PrintErrors(r :TTestResult) :string;
var
  i       :integer;
  error   :TTestFailure;
begin
    result := '';
    if (r.errorCount <> 0) then begin
        if (r.errorCount = 1) then
            result := result + format('There was %d error:', [r.errorCount]) + CRLF
        else
            result := result + format('There were %d errors:', [r.errorCount]) + CRLF;

        for i := 0 to r.errors.Count-1 do begin
            error :=  TObject(r.errors[i]) as TTestFailure;
            result := result + format('%d) %s: %s: %s', [
                                       i+1,
                                       error.failedTest.name,
                                       error.thrownExceptionName,
                                       error.thrownExceptionMessage
                                       ]) + CRLF;
        end;
        result := result + CRLF
    end
End;


HTML generated by Time2HELP
http://www.time2help.com
PrintFailures method
Prints failures to the standard output

Applies to
TTextTestListener

Declaration
Function PrintFailures(r :TTestResult): string;

Implementation

function TTextTestListener.PrintFailures(r :TTestResult): string;
var
  i       :integer;
  failure :TTestFailure;
begin
    result := '';
    if (r.failureCount <> 0) then begin
        if (r.failureCount = 1) then
            result := result + format('There was %d failure:', [r.failureCount]) + CRLF
        else
            result := result + format('There were %d failures:', [r.failureCount]) + CRLF;

        for i := 0 to r.failures.Count-1 do begin
            failure := TObject(r.failures[i]) as TTestFailure;
            result := result + format('%d) %s: %s: %s', [
                                       i+1,
                                       failure.failedTest.name,
                                       failure.thrownExceptionName,
                                       failure.thrownExceptionMessage
                                       ]) + CRLF;
        end;
        result := result + CRLF
    end
End;


HTML generated by Time2HELP
http://www.time2help.com
PrintHeader method
Prints the header of the Report

Applies to
TTextTestListener

Declaration
Function PrintHeader(r :TTestResult): string;

Implementation

function TTextTestListener.PrintHeader(r :TTestResult):string;
begin
  result := '';
  if r.wasSuccessful then begin
      result := result + CRLF;
      result := result + format('OK: %d tests'+CRLF, [r.runCount]);
  end
  else begin
      result := result + CRLF;
      result := result + 'FAILURES!!!'+CRLF;
      result := result + 'Test Results:'+CRLF;
      result := result + format('Run:      %8d'+CRLF+'Failures: %8d'+CRLF+'Errors:   %8d'+CRLF,
                        [r.runCount, r.failureCount, r.errorCount]
                        );
  end
End;


HTML generated by Time2HELP
http://www.time2help.com
Report method
Prints failures to the standard output

Applies to
TTextTestListener

Declaration
Function Report(r :TTestResult): string;

Implementation

function TTextTestListener.Report(r :TTestResult) :string;
begin
    result := PrintHeader(r) +
              PrintErrors(r) +
              PrintFailures(r);
End;


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

Applies to
TTextTestListener

Declaration
Procedure StartTest(test :ITest);

Implementation

procedure TTextTestListener.StartTest(test :ITest);
begin
    write('.');
End;


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

Applies to
TTextTestListener

Declaration
Procedure TestingEnds(testResult :TTestResult);

Implementation

procedure TTextTestListener.TestingEnds(testResult :TTestResult);
var
  h, m, s, l :Word;
begin
  endTime   := now;
  runTime   := endTime-startTime;
  writeln;
  DecodeTime(runTime, h,  m, s, l);
  writeln(Format('Time: %d:%2.2d:%2.2d.%d', [h, m, s, l]));
  writeln(Report(testResult));
  writeln;
End;


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

Applies to
TTextTestListener

Declaration
Procedure TestingStarts;

Implementation

procedure TTextTestListener.TestingStarts;
begin
  writeln;
  writeln('DUnit / Testing');
  startTime := now;
End;


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

Applies to
TTextTestListener

Declaration
Function TruncateString(s :string; len :integer): string;

Implementation

function TTextTestListener.TruncateString(s :string; len :integer) :string;
begin
    if Length(s) > len then
        result := copy(s, 1, len) + '...'
    else
        result := s
End;


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

Unit
TextTestRunner

Declaration
Function RunRegisteredTests(exitBehavior :TRunnerExitBehavior = rxbContinue): TTestResult;

Implementation

function RunRegisteredTests(exitBehavior :TRunnerExitBehavior = rxbContinue) :TTestResult;
begin
   Result := RunTest(registeredTests, [TTextTestListener.Create]);
End;


HTML generated by Time2HELP
http://www.time2help.com
RunTest Routine
Run the given test suite

Unit
TextTestRunner

Declaration
Function RunTest(suite :ITest; exitBehavior :TRunnerExitBehavior = rxbContinue): TTestResult;

Implementation

function RunTest(suite :ITest; exitBehavior :TRunnerExitBehavior = rxbContinue) :TTestResult;
begin
  Result := RunTest(suite, [TTextTestListener.Create]);
  case exitBehavior of
    rxbPause:
      try
        writeln('Press <RETURN> to continue.');
        readln
      except
      end;
    rxbHaltOnFailures:
      with Result do
      begin
        if not WasSuccessful then
          System.Halt(ErrorCount+FailureCount);
      end
    // else fall through
  end;
End;


HTML generated by Time2HELP
http://www.time2help.com
TRunnerExitBehavior Type
This type defines what the RunTest and RunRegisteredTests methods will do when testing has ended.

Unit
TextTestRunner

Declaration

TRunnerExitBehavior = (
    rxbContinue,
    rxbPause,
    rxbHaltOnFailures
    );


Values
rxbContinue Just return the TestResult.
rxbPause Pause with a ReadLn before returnng the TestResult.
rxbHaltOnFailures Halt the program if errors or failures occurred, setting the program exit code to FailureCount+ErrorCount; behave like rxbContinue if all tests suceeded.

See Also


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