frameworktest.pp 3.4 KB

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