Delphi DunitX FMX GUI记录器

时间:2017-06-29 11:07:54

标签: unit-testing delphi dunitx

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

  1. 包括DUnitX.Loggers.GUIX单位
  2. 注释掉DUnitX.Loggers.Console单元
  3. 注释掉了{$ APPTYPE CONSOLE}指令
  4. 将记录器更改为记录器:= TGUIXTestRunner.Create(nil);
  5. 修改后的列表如下所示:

    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.
    

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

    enter image description here

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

    我认为这里有一些我不知道的东西,任何帮助都会受到赞赏,因为文档在这方面有点薄。

    由于

1 个答案:

答案 0 :(得分:3)

这是我使用的模板,允许我在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,
  {$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.