testrunner.pp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2004 by Dean Zobec, Michael Van Canneyt
  4. an example of a console test runner of FPCUnit tests.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. }
  11. program testrunner;
  12. {$mode objfpc}
  13. {$h+}
  14. uses
  15. custapp, Classes, SysUtils, fpcunit, suiteconfig, testreport, testregistry;
  16. const
  17. ShortOpts = 'alh';
  18. Longopts: Array[1..5] of String = (
  19. 'all','list','format:','suite:','help');
  20. Version = 'Version 0.2';
  21. type
  22. TTestRunner = Class(TCustomApplication)
  23. private
  24. FXMLResultsWriter: TXMLResultsWriter;
  25. protected
  26. procedure DoRun ; Override;
  27. procedure doTestRun(aTest: TTest); virtual;
  28. public
  29. constructor Create(AOwner: TComponent); override;
  30. destructor Destroy; override;
  31. end;
  32. constructor TTestRunner.Create(AOwner: TComponent);
  33. begin
  34. inherited Create(AOwner);
  35. FXMLResultsWriter := TXMLResultsWriter.Create;
  36. end;
  37. destructor TTestRunner.Destroy;
  38. begin
  39. FXMLResultsWriter.Free;
  40. end;
  41. procedure TTestRunner.doTestRun(aTest: TTest);
  42. var
  43. testResult: TTestResult;
  44. begin
  45. testResult := TTestResult.Create;
  46. try
  47. testResult.AddListener(FXMLResultsWriter);
  48. aTest.Run(testResult);
  49. FXMLResultsWriter.WriteResult(testResult);
  50. finally
  51. testResult.Free;
  52. end;
  53. end;
  54. procedure TTestRunner.DoRun;
  55. var
  56. I : Integer;
  57. S : String;
  58. begin
  59. S:=CheckOptions(ShortOpts,LongOpts);
  60. If (S<>'') then
  61. Writeln(S);
  62. if HasOption('h', 'help') or (ParamCount = 0) then
  63. begin
  64. writeln(Title);
  65. writeln(Version);
  66. writeln('Usage: ');
  67. writeln('-l or --list to show a list of registered tests');
  68. writeln('default format is xml, add --format=latex to output the list as latex source');
  69. writeln('-a or --all to run all the tests and show the results in xml format');
  70. writeln('The results can be redirected to an xml file,');
  71. writeln('for example: ./testrunner --all > results.xml');
  72. writeln('use --suite=MyTestSuiteName to run only the tests in a single test suite class');
  73. end
  74. else;
  75. if HasOption('l', 'list') then
  76. begin
  77. if HasOption('format') then
  78. begin
  79. if GetOptionValue('format') = 'latex' then
  80. writeln(GetSuiteAsLatex(GetTestRegistry))
  81. else
  82. writeln(GetSuiteAsXML(GetTestRegistry));
  83. end
  84. else
  85. writeln(GetSuiteAsXML(GetTestRegistry));
  86. end;
  87. if HasOption('a', 'all') then
  88. begin
  89. doTestRun(GetTestRegistry)
  90. end
  91. else
  92. if HasOption('suite') then
  93. begin
  94. S := '';
  95. S:=GetOptionValue('suite');
  96. if S = '' then
  97. for I := 0 to GetTestRegistry.Tests.count - 1 do
  98. writeln(GetTestRegistry[i].TestName)
  99. else
  100. for I := 0 to GetTestRegistry.Tests.count - 1 do
  101. if GetTestRegistry[i].TestName = S then
  102. begin
  103. doTestRun(GetTestRegistry[i]);
  104. end;
  105. end;
  106. Terminate;
  107. end;
  108. var
  109. App: TTestRunner;
  110. begin
  111. App := TTestRunner.Create(nil);
  112. App.Initialize;
  113. App.Title := 'FPCUnit Console Test Case runner.';
  114. App.Run;
  115. App.Free;
  116. end.