Search code examples
unit-testingdelphidunitx

Delphi DunitX FMX GUI logger


I have a console based DUNITX unit test program which I am trying to convert to the FMX GUI output. Having read the embarcadero docs here, I modified the Test program file by doing the following:

  1. included the DUnitX.Loggers.GUIX unit
  2. Commented out the DUnitX.Loggers.Console unit
  3. Commented out the {$APPTYPE CONSOLE} directive
  4. Changed the Logger to logger := TGUIXTestRunner.Create(nil);

the modified listing looks like this:

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.

When I run this (using Tokyo 10.2) I get the following error :

enter image description here

I should also note that I had to include the path to the DUNITX source in my library path as it didn't find the fmx form to compile.

I assume there is something i am missing here, any help would be appreciated as the documentation is a little thin on the ground for this.

Thanks


Solution

  • This is a template that I use that allows me to choose between VCL,FMX or Console apps:

    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.