1

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.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
kokokok
  • 1,030
  • 1
  • 9
  • 26
  • [OutputDebugString](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363362)? – whosrdaddy Dec 23 '16 at 12:56
  • Oh, thank you. I was looking for a specific function of DUnitX like TDUnitX.CurrentRunner.Log or similar and I did not realized I could use directly OutputDebugString – kokokok Dec 23 '16 at 17:03
  • I don't know DUnitX well, but it seems that `TDUnitX.CurrentRunner.Log` or `TDUnitX.CurrentRunner.Status` would do the trick, look at the examples for DUnitX that come with Delphi – whosrdaddy Dec 23 '16 at 19:16

2 Answers2

3
  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;
Johan
  • 74,508
  • 24
  • 191
  • 319
  • 5
    The use of false in TDUnitXConsoleLogger.Create(false); is essential. That boolean determines whether the logging is quiet or not. If it is quiet, you do not see very much....... – user424855 Mar 20 '17 at 06:17
  • Once you have created loggers you need to call Log function into your test: Assert.Log('logging something'). I think is evident but missing in the answer – Rubén Pozo Aug 28 '18 at 10:37
  • @RubénPozo, as far as I can tell `Assert.Log` does not exist. See: https://github.com/VSoftTechnologies/DUnitX/blob/master/DUnitX.Assert.pas – Johan Oct 24 '18 at 11:30
  • Log is implemented in DUnitX.TestFramework.TTestFixtureHelper: https://github.com/VSoftTechnologies/DUnitX/blob/master/DUnitX.TestFramework.pas – Rubén Pozo Oct 25 '18 at 09:42
0

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;
Jesse Lee
  • 174
  • 1
  • 6