How to log in DUnitX?

1.7k views Asked by At

Using Rad Studio 10 Seattle, DUnitX and TestInsight, I would need to show some texts in the console or any log screen. How can it be done? I have not been able to find it in the web.

2

There are 2 answers

4
Greg Bailey On BEST ANSWER
  procedure CreateRunner;
  var fn : TFileName;
  begin
    runner := TDUnitX.CreateRunner;
    runner.UseRTTI := True;

    fn := IncludeTrailingPathDelimiter(ExtractFilePath(Application.ExeName)) + 'dUnitX.Log';

    ConsoleLogger := TDUnitXConsoleLogger.Create(false);
    TextFileLogger:= TDUnitXTextFileLogger.Create(fn);
    nunitLogger   := TDUnitXXMLNUnitFileLogger.Create(TDUnitX.Options.XMLOutputFile);

    runner.AddLogger(ConsoleLogger );
    runner.AddLogger(TextFileLogger);
    runner.AddLogger(nunitLogger);
  end;

Here's how to add log messages, fragments copied from https://github.com/VSoftTechnologies/DUnitX/blob/d5861ce0de6a9fbfdc8c158b0b1c8614082c188d/Examples/DUnitX.Examples.General.pas

 [TestFixture('ExampleFixture1','General Example Tests')]
   TMyExampleTests = class
   public
     [Test]
     procedure LogMessageTypes;
   end;

procedure TMyExampleTests.LogMessageTypes;
begin
  TDUnitX.CurrentRunner.Log(TLogLevel.Information, 'Information');
  TDUnitX.CurrentRunner.Log(TLogLevel.Warning, 'Warning');
  TDUnitX.CurrentRunner.Log(TLogLevel.Error, 'Error');
end;

If you want to have a less convoluted syntax, you can always add an interposer for the Assert class like so

Assert = class(DUnitX.Assert.Assert) //Or (DunitX.Assert.Ex.Assert)
public
  class procedure Log(const message: string);
end;

class procedure Assert.Log(const message: string);
begin
  TDUnitX.CurrentRunner.Log(TLogLevel.Information, message);
end;
0
Jesse Lee On

https://github.com/jsf3rd/DUnitX.git/trunk/Examples

var
  runner : ITestRunner;
  logger : ITestLogger;
begin
  try
    //Create the runner
    runner := TDUnitX.CreateRunner;
    runner.UseRTTI := True;

    //tell the runner how we will log things

    if TDUnitX.Options.ConsoleMode <> TDunitXConsoleMode.Off then
    begin
      logger := TDUnitXConsoleLogger.Create(TDUnitX.Options.ConsoleMode = TDunitXConsoleMode.Quiet);
      runner.AddLogger(logger);
    end;

    //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;