frameworktest.pp 3.5 KB

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