browsertestrunner.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. { This unit contains the TTestRunner class, a base class for the browser test
  2. runner for fpcunit.
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (C) 2017 Michael Van Canneyt
  5. This library is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Library General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or (at your
  8. option) any later version.
  9. This program is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  12. for more details.
  13. You should have received a copy of the GNU Library General Public License
  14. along with this library; if not, write to the Free Software Foundation,
  15. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. **********************************************************************}
  17. unit BrowserTestRunner;
  18. {$mode objfpc}
  19. interface
  20. uses
  21. CustApp, browserapp, Classes, SysUtils, FPCUnit, TestRegistry, FPCUnitReport, htmlTestReport;
  22. const
  23. Version = '0.9';
  24. type
  25. { TRunForm }
  26. { TTestRunner }
  27. TTestRunner = class(TBrowserApplication)
  28. private
  29. FRunFormClass: TRunFormClass;
  30. FShowProgress: boolean;
  31. procedure DoRunAgain(Sender: TObject);
  32. procedure ShowTests(T: TTestsuite);
  33. protected
  34. property ShowProgress: boolean read FShowProgress write FShowProgress;
  35. procedure DoRun; override;
  36. procedure DoTestRun(ATest: TTest); virtual;
  37. //procedure ExtendXmlDocument(Doc: TXMLDocument); virtual;
  38. function GetResultsWriter: TCustomResultsWriter; virtual;
  39. public
  40. procedure RunTests;
  41. Property RunFormClass : TRunFormClass Read FRunFormClass Write FRunFormClass;
  42. end;
  43. implementation
  44. type
  45. { TProgressWriter }
  46. TProgressWriter = class({TNoRefCountObject, }ITestListener)
  47. private
  48. FSuccess: boolean;
  49. procedure WriteChar(c: char);
  50. public
  51. destructor Destroy; override;
  52. { ITestListener interface requirements }
  53. procedure AddFailure(ATest: TTest; AFailure: TTestFailure); override;
  54. procedure AddError(ATest: TTest; AError: TTestFailure); override;
  55. procedure StartTest(ATest: TTest); override;
  56. procedure EndTest(ATest: TTest); override;
  57. procedure StartTestSuite(ATestSuite: TTestSuite); override;
  58. procedure EndTestSuite(ATestSuite: TTestSuite); override;
  59. end;
  60. procedure TProgressWriter.WriteChar(c: char);
  61. begin
  62. write(c);
  63. // flush output, so that we see the char immediately, even it is written to file
  64. //Flush(output);
  65. end;
  66. destructor TProgressWriter.Destroy;
  67. begin
  68. // on destruction, just write the missing line ending
  69. writeln;
  70. inherited Destroy;
  71. end;
  72. procedure TProgressWriter.AddFailure(ATest: TTest; AFailure: TTestFailure);
  73. begin
  74. FSuccess := false;
  75. writechar('F');
  76. if ATest=nil then;
  77. if AFailure=nil then;
  78. end;
  79. procedure TProgressWriter.AddError(ATest: TTest; AError: TTestFailure);
  80. begin
  81. FSuccess := false;
  82. writechar('E');
  83. if ATest=nil then;
  84. if AError=nil then ;
  85. end;
  86. procedure TProgressWriter.StartTest(ATest: TTest);
  87. begin
  88. FSuccess := true; // assume success, until proven otherwise
  89. if ATest=nil then;
  90. end;
  91. procedure TProgressWriter.EndTest(ATest: TTest);
  92. begin
  93. if FSuccess then
  94. writechar('.');
  95. if ATest=nil then ;
  96. end;
  97. procedure TProgressWriter.StartTestSuite(ATestSuite: TTestSuite);
  98. begin
  99. // do nothing
  100. if ATestSuite=nil then;
  101. end;
  102. procedure TProgressWriter.EndTestSuite(ATestSuite: TTestSuite);
  103. begin
  104. // do nothing
  105. if ATestSuite=nil then;
  106. end;
  107. { TTestRunner }
  108. procedure TTestRunner.ShowTests(T : TTestsuite);
  109. Var
  110. B : TTestTreeBuilder;
  111. begin
  112. B:=THTMLTreeBuilder.Create('');
  113. try
  114. B.ShowSuite(T);
  115. Finally
  116. FreeAndNil(B);
  117. end;
  118. end;
  119. procedure TTestRunner.DoRunAgain(Sender : TObject);
  120. begin
  121. RunTests;
  122. end;
  123. procedure TTestRunner.DoRun;
  124. var
  125. R : TRunForm;
  126. begin
  127. If Assigned(RunFormClass) then
  128. begin
  129. R:=RunFormClass.Create(Self);
  130. R.OnRun:=@DoRunAgain;
  131. R.Initialize;
  132. end
  133. else
  134. RunTests;
  135. Terminate;
  136. end;
  137. procedure TTestRunner.RunTests;
  138. begin
  139. ShowTests(GetTestRegistry);
  140. DoTestRun(GetTestRegistry);
  141. end;
  142. procedure TTestRunner.DoTestRun(ATest: TTest);
  143. var
  144. ResultsWriter: TCustomResultsWriter;
  145. ProgressWriter: TProgressWriter;
  146. TestResult: TTestResult;
  147. begin
  148. ResultsWriter := GetResultsWriter;
  149. TestResult := TTestResult.Create;
  150. ProgressWriter:=nil;
  151. try
  152. if ShowProgress then
  153. begin
  154. ProgressWriter := TProgressWriter.Create;
  155. TestResult.AddListener(ProgressWriter);
  156. end
  157. else
  158. ProgressWriter := nil;
  159. TestResult.AddListener(ResultsWriter.TestListener);
  160. ATest.Run(TestResult);
  161. ResultsWriter.WriteResult(TestResult);
  162. finally
  163. FreeAndNil(TestResult);
  164. FreeAndNil(ResultsWriter);
  165. FreeAndNil(ProgressWriter);
  166. end;
  167. end;
  168. function TTestRunner.GetResultsWriter: TCustomResultsWriter;
  169. begin
  170. Result:=THTMLResultsWriter.Create(Nil);
  171. end;
  172. end.