首页 > 解决方案 > 使用 TDUnitX.CurrentRunner.Log 时无日志输出

问题描述

我不确定如何使用 DUnitX 将一些字符串输出到控制台窗口,所以这是我的尝试:

unit Unit1;

interface
uses
  DUnitX.TestFramework;

type
  [TestFixture]
  TMyTestObject = class(TObject)
  public
    // Sample Methods
    // Simple single Test
    // Test with TestCase Attribute to supply parameters.
    [Test]
    [TestCase('TestA','1,2,3')]
    [TestCase('TestB','3,4,7')]
    procedure Test1(AValue1, AValue2, _Result : Integer);
  end;

procedure TMyTestObject.Test1(AValue1, AValue2, _Result: Integer);
begin
  TDUnitX.CurrentRunner.Log(TLogLevel.Information, 'Information');
end;

initialization
  TDUnitX.RegisterTestFixture(TMyTestObject);
end.

什么都没有打印出来,所以我应该怎么写呢?

标签: delphidunitx

解决方案


DUnitX 在单元 DUnitX.TestFramework 中使用一对 Log 和 Status 方法在 TObject 上定义了一个类助手。只要此单元在您的使用列表中,您就可以在任何对象上调用这些方法,例如通过以下方式:

uses
  DUnitX.TestFramework;

[...]

procedure TMyTestObject.Test1(AValue1, AValue2, _Result: Integer);
begin
  self.Log(TLogLevel.Information, 'Information');
end;

该语法之所以有效,是因为类助手是一种将方法和属性添加到已定义类的方法。通过在 TObject 上定义一个类助手,所有对象都被扩展了。

Embarcadero Wiki: Class and Record Helpers (Delphi)中描述了类助手


推荐阅读