browsertestrunner.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. { This unit contains the TTestRunner class, a base class for the console 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,
  22. FPCUnit, TestRegistry,
  23. FPCUnitReport, htmlTestReport;
  24. const
  25. Version = '0.9';
  26. type
  27. { TRunForm }
  28. TRunForm = class(TComponent)
  29. private
  30. FOnRun: TNotifyEvent;
  31. Public
  32. Procedure Initialize; virtual;
  33. Property OnRun : TNotifyEvent Read FOnRun Write FOnRun;
  34. end;
  35. TRunFormClass = class of TRunForm;
  36. { TTestRunner }
  37. TTestRunner = class(TBrowserApplication)
  38. private
  39. FRunFormClass: TRunFormClass;
  40. FShowProgress: boolean;
  41. procedure DoRunAgain(Sender: TObject);
  42. procedure ShowTests(T: TTestsuite);
  43. protected
  44. property ShowProgress: boolean read FShowProgress write FShowProgress;
  45. procedure DoRun; override;
  46. procedure DoTestRun(ATest: TTest); virtual;
  47. //procedure ExtendXmlDocument(Doc: TXMLDocument); virtual;
  48. function GetResultsWriter: TCustomResultsWriter; virtual;
  49. public
  50. procedure RunTests;
  51. Property RunFormClass : TRunFormClass Read FRunFormClass Write FRunFormClass;
  52. end;
  53. implementation
  54. type
  55. { TProgressWriter }
  56. TProgressWriter = class({TNoRefCountObject, }ITestListener)
  57. private
  58. FSuccess: boolean;
  59. procedure WriteChar(c: char);
  60. public
  61. destructor Destroy; override;
  62. { ITestListener interface requirements }
  63. procedure AddFailure(ATest: TTest; AFailure: TTestFailure); override;
  64. procedure AddError(ATest: TTest; AError: TTestFailure); override;
  65. procedure StartTest(ATest: TTest); override;
  66. procedure EndTest(ATest: TTest); override;
  67. procedure StartTestSuite(ATestSuite: TTestSuite); override;
  68. procedure EndTestSuite(ATestSuite: TTestSuite); override;
  69. end;
  70. { TRunForm }
  71. procedure TRunForm.Initialize;
  72. begin
  73. // Do nothing
  74. end;
  75. procedure TProgressWriter.WriteChar(c: char);
  76. begin
  77. write(c);
  78. // flush output, so that we see the char immediately, even it is written to file
  79. //Flush(output);
  80. end;
  81. destructor TProgressWriter.Destroy;
  82. begin
  83. // on destruction, just write the missing line ending
  84. writeln;
  85. inherited Destroy;
  86. end;
  87. procedure TProgressWriter.AddFailure(ATest: TTest; AFailure: TTestFailure);
  88. begin
  89. FSuccess := false;
  90. writechar('F');
  91. if ATest=nil then;
  92. if AFailure=nil then;
  93. end;
  94. procedure TProgressWriter.AddError(ATest: TTest; AError: TTestFailure);
  95. begin
  96. FSuccess := false;
  97. writechar('E');
  98. if ATest=nil then;
  99. if AError=nil then ;
  100. end;
  101. procedure TProgressWriter.StartTest(ATest: TTest);
  102. begin
  103. FSuccess := true; // assume success, until proven otherwise
  104. if ATest=nil then;
  105. end;
  106. procedure TProgressWriter.EndTest(ATest: TTest);
  107. begin
  108. if FSuccess then
  109. writechar('.');
  110. if ATest=nil then ;
  111. end;
  112. procedure TProgressWriter.StartTestSuite(ATestSuite: TTestSuite);
  113. begin
  114. // do nothing
  115. if ATestSuite=nil then;
  116. end;
  117. procedure TProgressWriter.EndTestSuite(ATestSuite: TTestSuite);
  118. begin
  119. // do nothing
  120. if ATestSuite=nil then;
  121. end;
  122. { TTestRunner }
  123. procedure TTestRunner.ShowTests(T : TTestsuite);
  124. Var
  125. B : TTestTreeBuilder;
  126. begin
  127. B:=THTMLTreeBuilder.Create('');
  128. try
  129. B.ShowSuite(T);
  130. Finally
  131. FreeAndNil(B);
  132. end;
  133. end;
  134. procedure TTestRunner.DoRunAgain(Sender : TObject);
  135. begin
  136. RunTests;
  137. end;
  138. procedure TTestRunner.DoRun;
  139. var
  140. R : TRunForm;
  141. begin
  142. If Assigned(RunFormClass) then
  143. begin
  144. R:=RunFormClass.Create(Self);
  145. R.OnRun:=@DoRunAgain;
  146. R.Initialize;
  147. end;
  148. RunTests;
  149. Terminate;
  150. end;
  151. procedure TTestRunner.RunTests;
  152. begin
  153. ShowTests(GetTestRegistry);
  154. DoTestRun(GetTestRegistry);
  155. end;
  156. procedure TTestRunner.DoTestRun(ATest: TTest);
  157. var
  158. ResultsWriter: TCustomResultsWriter;
  159. ProgressWriter: TProgressWriter;
  160. TestResult: TTestResult;
  161. begin
  162. ResultsWriter := GetResultsWriter;
  163. TestResult := TTestResult.Create;
  164. ProgressWriter:=nil;
  165. try
  166. if ShowProgress then
  167. begin
  168. ProgressWriter := TProgressWriter.Create;
  169. TestResult.AddListener(ProgressWriter);
  170. end
  171. else
  172. ProgressWriter := nil;
  173. TestResult.AddListener(ResultsWriter.TestListener);
  174. ATest.Run(TestResult);
  175. ResultsWriter.WriteResult(TestResult);
  176. finally
  177. FreeAndNil(TestResult);
  178. FreeAndNil(ResultsWriter);
  179. FreeAndNil(ProgressWriter);
  180. end;
  181. end;
  182. function TTestRunner.GetResultsWriter: TCustomResultsWriter;
  183. begin
  184. Result:=THTMLResultsWriter.Create(Nil);
  185. end;
  186. end.