3

我有一个基于控制台的 DUNITX 单元测试程序,我正在尝试将其转换为 FMX GUI 输出。阅读了此处的 embarcadero 文档后,我通过执行以下操作修改了测试程序文件:

  1. 包括 DUnitX.Loggers.GUIX 单元
  2. 注释掉 DUnitX.Loggers.Console 单元
  3. 注释掉 {$APPTYPE CONSOLE} 指令
  4. 将 Logger 更改为 logger := TGUIXTestRunner.Create(nil);

修改后的清单如下所示:

program HTMLParserTest;

{$IFNDEF TESTINSIGHT}
//{$APPTYPE CONSOLE}
{$ENDIF}{$STRONGLINKTYPES ON}
uses
  System.SysUtils,
  {$IFDEF TESTINSIGHT}
  TestInsight.DUnitX,
  {$ENDIF }
 //  DUnitX.Loggers.Console,
  DUnitX.Loggers.GUIX,
  DUnitX.Loggers.Xml.NUnit,
  DUnitX.TestFramework,
  test.ITUtils.Delphi in 'test.ITUtils.Delphi.pas',
  ITUtils.Delphi in '..\Concept Test\ITUtils.Delphi.pas',
  test.ITsimplehtmlparser.Delphi in 'test.ITsimplehtmlparser.Delphi.pas',
  ITTools.simplehtmlparser.Delphi in '..\Concept 
   Test\ITTools.simplehtmlparser.Delphi.pas';

var
  runner : ITestRunner;
  results : IRunResults;
  logger : ITestLogger;
  nunitLogger : ITestLogger;
begin
{$IFDEF TESTINSIGHT}
  TestInsight.DUnitX.RunRegisteredTests;
  exit;
{$ENDIF}
try
//Check command line options, will exit if invalid
TDUnitX.CheckCommandLine;
//Create the test runner
runner := TDUnitX.CreateRunner;
//Tell the runner to use RTTI to find Fixtures
runner.UseRTTI := True;
//tell the runner how we will log things
//Log to the console window
// logger := TDUnitXConsoleLogger.Create(true);
logger := TGUIXTestRunner.Create(nil);
runner.AddLogger(logger);
//Generate an NUnit compatible XML File
nunitLogger := TDUnitXXMLNUnitFileLogger.Create(TDUnitX.Options.XMLOutputFile);
runner.AddLogger(nunitLogger);
runner.FailsOnNoAsserts := False; //When true, Assertions must be made during tests;

//Run tests
results := runner.Execute;
if not results.AllPassed then
  System.ExitCode := EXIT_ERRORS;

{$IFNDEF CI}
//We don't want this happening when running under CI.
if TDUnitX.Options.ExitBehavior = TDUnitXExitBehavior.Pause then
begin
  System.Write('Done.. press <Enter> key to quit.');
  System.Readln;
end;
{$ENDIF}
except
on E: Exception do
  System.Writeln(E.ClassName, ': ', E.Message);
end;
end.

当我运行它(使用 Tokyo 10.2)时,我收到以下错误:

在此处输入图像描述

我还应该注意,我必须在我的库路径中包含 DUNITX 源的路径,因为它没有找到要编译的 fmx 表单。

我认为我在这里缺少一些东西,任何帮助都将不胜感激,因为文档在这方面有点薄。

谢谢

4

1 回答 1

7

这是我使用的模板,允许我在 VCL、FMX 或控制台应用程序之间进行选择:

program UnitTests;


///// UI Selection - Pick only 1! //////////////////////////////
{DEFINE UseVCL}
{$DEFINE UseFMX}
{DEFINE UseWinConsole}
////////////////////////////////////////////////////////////////


{$IFDEF UseWinConsole}
{$DEFINE UseConsole}
{$ENDIF}

{$IFDEF UseConsole}
{$APPTYPE CONSOLE}
{$ENDIF}

uses
  {$IFDEF UseVCL}
  VCL.Forms,
  DUnitX.Loggers.GUI.VCL,
  {$ENDIF}

  {$IFDEF UseFMX}
  FMX.Forms,
  DUnitX.Loggers.GUIX,
  {$ENDIF}

  {$IFDEF UseConsole}
  DUnitX.ConsoleWriter.Base,
  DUnitX.Loggers.Console,
  DUnitX.Loggers.XML.NUnit,
  DUnitX.Loggers.Text,
  DUnitX.Loggers.XML.xUnit,
  {$ENDIF}

  {$IFDEF UseWinConsole}
  DUnitX.Windows.Console,
  WinAPI.Windows,
  WinAPI.Messages,
  {$ENDIF}

  System.SysUtils,
  DUnitX.Generics,
  DUnitX.InternalInterfaces,
  DUnitX.WeakReference,
  DUnitX.FixtureResult,
  DUnitX.RunResults,
  DUnitX.Test,
  DUnitX.TestFixture,
  DUnitX.TestFramework,
  DUnitX.TestResult,
  DUnitX.TestRunner,
  DUnitX.Utils,
  DUnitX.IoC,
  DUnitX.MemoryLeakMonitor.Default,

//      [TestFixturesUnit1] in '[TestFixturesUnit1].pas',
//      [TestFixturesUnit2] in '[TestFixturesUnit2].pas',
//      [TestFixturesUnit3] in '[TestFixturesUnit3].pas',

  DUnitX.DUnitCompatibility;

{$R *.res}

/////////////////////////////////////////////////////////////////////////
{$IFDEF UseVCL}
begin
  Application.Initialize;
  Application.CreateForm(TGUIVCLTestRunner, GUIVCLTestRunner);
  Application.Run;
{$ENDIF}
/////////////////////////////////////////////////////////////////////////
{$IFDEF UseFMX}
begin
  Application.Initialize;
  Application.CreateForm(TGUIXTestRunner, GUIXTestRunner);
  Application.Run;
{$ENDIF}
/////////////////////////////////////////////////////////////////////////
{$IFDEF UseConsole}
var
  runner : ITestRunner;
  results : IRunResults;
  logger : ITestLogger;
  nunitLogger : ITestLogger;

begin
   try
      //Create the runner
      runner := TDUnitX.CreateRunner;
      runner.UseRTTI := True;
      //tell the runner how we will log things
      logger := TDUnitXConsoleLogger.Create(true);
      nunitLogger := TDUnitXXMLNUnitFileLogger.Create;
      runner.AddLogger(logger);
      runner.AddLogger(nunitLogger);

      //Run tests
      results := runner.Execute;

      System.Write('Done.. press <Enter> key to quit.');
      System.Readln;

   except
      on E: Exception do
         System.Writeln(E.ClassName, ': ', E.Message);
   end;
{$ENDIF}
/////////////////////////////////////////////////////////////////////////

end.
于 2017-06-29T11:47:02.160 回答