simpletestrunner.pas 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. { This unit contains the TTestRunner class, a base class for the a simple
  2. console test runner for fpcunit that can be used with FPC's testsuite. It only
  3. generates a plain test report, always runs all tests and sets the exitcode.
  4. Copyright (C) 2006 Vincent Snijders (original consoletestrunner.pas)
  5. Copyright (C) 2016 Sven Barth
  6. This library is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU Library General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or (at your
  9. option) any later version.
  10. This program is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  13. for more details.
  14. You should have received a copy of the GNU Library General Public License
  15. along with this library; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. }
  18. unit simpletestrunner;
  19. {$mode objfpc}{$H+}
  20. interface
  21. uses
  22. custapp, Classes, SysUtils, fpcunit, testregistry,
  23. fpcunitreport, plaintestreport;
  24. const
  25. Version = '0.1';
  26. type
  27. { TTestRunner }
  28. TTestRunner = class(TCustomApplication)
  29. protected
  30. procedure DoRun; override;
  31. function DoTestRun(ATest: TTest): Boolean;
  32. end;
  33. implementation
  34. function TTestRunner.DoTestRun(ATest: TTest): Boolean;
  35. var
  36. ResultsWriter: TCustomResultsWriter;
  37. TestResult: TTestResult;
  38. begin
  39. ResultsWriter := TPlainResultsWriter.Create(Nil);
  40. TestResult := TTestResult.Create;
  41. try
  42. TestResult.AddListener(ResultsWriter);
  43. ATest.Run(TestResult);
  44. ResultsWriter.WriteResult(TestResult);
  45. Result := (TestResult.NumberOfErrors = 0) and (TestResult.NumberOfFailures = 0);
  46. finally
  47. TestResult.Free;
  48. ResultsWriter.Free;
  49. end;
  50. end;
  51. procedure TTestRunner.DoRun;
  52. begin
  53. if not DoTestRun(GetTestRegistry) then
  54. ExitCode := 1;
  55. Terminate;
  56. end;
  57. end.