{$mode objfpc} {$h+} { $Id$ This file is part of the Free Component Library (FCL) Copyright (c) 2004 by Dean Zobec, Michael Van Canneyt an example of a console test runner of FPCUnit tests. See the file COPYING.FPC, included in this distribution, for details about the copyright. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. **********************************************************************} unit testreport; interface uses classes, SysUtils, fpcunit, testutils; type TXMLResultsWriter = class(TNoRefCountObject, ITestListener) public procedure WriteHeader; procedure WriteResult(aResult: TTestResult); {ITestListener} procedure AddFailure(ATest: TTest; AFailure: TTestFailure); procedure AddError(ATest: TTest; AError: TTestFailure); procedure StartTest(ATest: TTest); procedure EndTest(ATest: TTest); end; { TLatexResultsWriter = class(TNoRefCountObject, ITestListener) public procedure AddFailure(ATest: TTest; AFailure: TTestFailure); procedure AddError(ATest: TTest; AError: TTestFailure); procedure StartTest(ATest: TTest); procedure EndTest(ATest: TTest); end;} function TestSuiteAsXML(aSuite: TTestSuite): string; function TestSuiteAsLatex(aSuite:TTestSuite): string; function GetSuiteAsXML(aSuite: TTestSuite): string; function GetSuiteAsLatex(aSuite: TTestSuite): string; function TestResultAsXML(aTestResult: TTestResult): string; implementation procedure TXMLResultsWriter.WriteHeader; begin writeln(''); writeln(''); end; procedure TXMLResultsWriter.WriteResult(aResult: TTestResult); begin writeln(''); writeln(TestResultAsXML(aResult)); writeln(''); end; {TXMLResultsWriter} procedure TXMLResultsWriter.AddFailure(ATest: TTest; AFailure: TTestFailure); begin writeln(''); writeln('', AFailure.ExceptionMessage, ''); writeln(''); end; procedure TXMLResultsWriter.AddError(ATest: TTest; AError: TTestFailure); begin writeln(''); writeln('', AError.ExceptionMessage, ''); writeln('', AError.SourceUnitName, ''); writeln('', AError.MethodName, ''); writeln('', AError.LineNumber, ''); writeln(''); end; procedure TXMLResultsWriter.StartTest(ATest: TTest); begin writeln(''); end; procedure TXMLResultsWriter.EndTest(ATest: TTest); begin writeln(''); end; function TestSuiteAsXML(aSuite:TTestSuite): string; var i: integer; begin Result := '' + System.sLineBreak; for i := 0 to aSuite.Tests.Count - 1 do if TTest(aSuite.Tests.Items[i]) is TTestSuite then Result := Result + TestSuiteAsXML(TTestSuite(aSuite.Tests.Items[i])) else if TTest(aSuite.Tests.Items[i]) is TTestCase then Result := Result +'' + TTestcase(aSuite.Tests.Items[i]).TestName + '' + System.sLineBreak; Result := Result + '' + System.sLineBreak; end; function TestSuiteAsLatex(aSuite:TTestSuite): string; var i,j: integer; s: TTestSuite; begin Result := '\flushleft' + System.sLineBreak; for i := 0 to aSuite.Tests.Count - 1 do begin s := TTestSuite(ASuite.Tests.Items[i]); Result := Result + s.TestSuiteName + System.sLineBreak; Result := Result + '\begin{itemize}'+ System.sLineBreak; for j := 0 to s.Tests.Count - 1 do if TTest(s.Tests.Items[j]) is TTestCase then Result := Result + '\item[-] ' + TTestcase(s.Tests.Items[j]).TestName + System.sLineBreak; Result := Result +'\end{itemize}' + System.sLineBreak; end; end; function GetSuiteAsXML(aSuite: TTestSuite): string; begin if aSuite <> nil then begin if aSuite.TestName = '' then aSuite.TestName := 'Test Suite'; Result := TestSuiteAsXML(aSuite) end else Result := ''; end; function GetSuiteAsLatex(aSuite: TTestSuite): string; begin if aSuite <> nil then begin Result := '\documentclass[a4paper,12pt]{article}' + System.sLineBreak; Result := Result + '\usepackage{array}' + System.sLineBreak; Result := Result + '\usepackage{mdwlist}' + System.sLineBreak + System.sLineBreak; Result := Result + '\begin{document}' + System.sLineBreak + System.sLineBreak; if aSuite.TestName = '' then aSuite.TestName := 'Test Suite'; Result := Result + TestSuiteAsLatex(aSuite); Result := Result + '\end{document}'; end else Result := ''; end; function TestResultAsXML(aTestResult: TTestResult): string; var i: longint; f: TTestFailure; begin with aTestResult do begin Result := '' + intToStr(RunTests) + '' + System.sLineBreak; Result := Result + '' + intToStr(NumberOfErrors) + '' + System.sLineBreak; Result := Result + '' + intToStr(NumberOfFailures) + ''; if NumberOfErrors <> 0 then begin Result := Result + System.sLineBreak; Result := Result + ''; for i := 0 to Errors.Count - 1 do begin Result := Result + System.sLineBreak; Result := Result + '' + System.sLineBreak; f := TTestFailure(Errors.Items[i]); Result := Result + ' ' + f.AsString + '' + System.sLineBreak; Result := Result + ' ' + f.ExceptionClassName + '' + System.sLineBreak; Result := Result + ' ' + f.ExceptionMessage + '' + System.sLineBreak; Result := Result + ' ' + f.SourceUnitName + '' + System.sLineBreak; Result := Result + ' ' + IntToStr(f.LineNumber) + '' + System.sLineBreak; Result := Result + ' ' + f.MethodName + '' + System.sLineBreak; Result := Result + '' + System.sLineBreak; end; Result := Result + ''; end; if NumberOfFailures <> 0 then begin Result := Result + System.sLineBreak; Result := Result + '' + System.sLineBreak; for i := 0 to Failures.Count - 1 do begin Result := Result + '' + System.sLineBreak; f := TTestFailure(Failures.Items[i]); Result := Result + ' ' + f.AsString + '' + System.sLineBreak; Result := Result + ' ' + f.ExceptionClassName + '' + System.sLineBreak; Result := Result + ' ' + f.ExceptionMessage + '' + System.sLineBreak; Result := Result + '' + System.sLineBreak; end; Result := Result + ''; end; end; end; end.