testrunner.pp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. program testrunner;
  2. {$mode objfpc}
  3. {$h+}
  4. uses HeapTrc,
  5. custapp, Classes, SysUtils, fpcunit, testregistry, xmltestreport, plaintestreport,
  6. sdo, sdo_datafactory, sdo_dataobject,
  7. sdo_field_imp, sdo_imp_utils, sdo_linked_list,
  8. sdo_type, test_type, test_field_imp, test_dataobjectlist,
  9. test_dataobject, test_utils, test_xsdhelper, test_serializer,
  10. test_equalityhelper, test_changesummary, test_suite_utils, sdo_xsd_helper,
  11. test_xpathhelper, sdo_xpath_helper, test_copyhelper,
  12. sdo_consts, sdo_serialization, sdo_serialization_utils,
  13. sdo_types, sdo_utils, sdo_changesummary,
  14. sdo_serialization_xml, test_convert_helper, test_property, test_xsdparser;
  15. const
  16. ShortOpts = 'alh';
  17. Longopts: Array[1..5] of String = (
  18. 'all','list','format:','suite:','help');
  19. Version = 'Version 0.2';
  20. type
  21. TTestRunner = Class(TCustomApplication)
  22. private
  23. FXMLResultsWriter: TXMLResultsWriter;
  24. protected
  25. procedure DoRun ; Override;
  26. procedure doTestRun(aTest: TTest); virtual;
  27. public
  28. constructor Create(AOwner: TComponent); override;
  29. destructor Destroy; override;
  30. end;
  31. constructor TTestRunner.Create(AOwner: TComponent);
  32. begin
  33. inherited Create(AOwner);
  34. FXMLResultsWriter := TXMLResultsWriter.Create(AOwner);
  35. end;
  36. destructor TTestRunner.Destroy;
  37. begin
  38. FXMLResultsWriter.Free;
  39. end;
  40. procedure TTestRunner.doTestRun(aTest: TTest);
  41. var
  42. testResult: TTestResult;
  43. begin
  44. testResult := TTestResult.Create;
  45. try
  46. testResult.AddListener(FXMLResultsWriter);
  47. aTest.Run(testResult);
  48. FXMLResultsWriter.WriteResult(testResult);
  49. finally
  50. testResult.Free;
  51. end;
  52. end;
  53. procedure TTestRunner.DoRun;
  54. var
  55. I : Integer;
  56. S : String;
  57. begin
  58. S:=CheckOptions(ShortOpts,LongOpts);
  59. If (S<>'') then
  60. Writeln(S);
  61. if HasOption('h', 'help') or (ParamCount = 0) then
  62. begin
  63. writeln(Title);
  64. writeln(Version);
  65. writeln('Usage: ');
  66. writeln('-l or --list to show a list of registered tests');
  67. writeln('default format is xml, add --format=latex to output the list as latex source');
  68. writeln('-a or --all to run all the tests and show the results in xml format');
  69. writeln('The results can be redirected to an xml file,');
  70. writeln('for example: ./testrunner --all > results.xml');
  71. writeln('use --suite=MyTestSuiteName to run only the tests in a single test suite class');
  72. end
  73. else;
  74. if HasOption('l', 'list') then
  75. begin
  76. if HasOption('format') then
  77. begin
  78. if GetOptionValue('format') = 'text' then
  79. writeln(GetSuiteAsPlain(GetTestRegistry))
  80. else
  81. writeln(GetSuiteAsXML(GetTestRegistry));
  82. end
  83. else
  84. writeln(GetSuiteAsXML(GetTestRegistry));
  85. end;
  86. if HasOption('a', 'all') then
  87. begin
  88. doTestRun(GetTestRegistry)
  89. end
  90. else
  91. if HasOption('suite') then
  92. begin
  93. S := '';
  94. S:=GetOptionValue('suite');
  95. if S = '' then
  96. for I := 0 to GetTestRegistry.Tests.count - 1 do
  97. writeln(GetTestRegistry[i].TestName)
  98. else
  99. for I := 0 to GetTestRegistry.Tests.count - 1 do
  100. if GetTestRegistry[i].TestName = S then
  101. begin
  102. doTestRun(GetTestRegistry[i]);
  103. end;
  104. end;
  105. Terminate;
  106. end;
  107. var
  108. App: TTestRunner;
  109. begin
  110. DeleteFile(sdoExpandLocalFileName('heaptrace.txt'));
  111. SetHeapTraceOutput('heaptrace.txt');
  112. App := TTestRunner.Create(nil);
  113. App.Initialize;
  114. App.Title := 'FPCUnit Console Test Case runner.';
  115. App.Run;
  116. App.Free;
  117. end.