如何测试函数和过程,因为它们不属于Delphi中的类?

Dan*_*llo 6 delphi testing unit-testing dunit

我在旧单元中有几个小功能Utils.pas.

现在我想重构一些,但我认为最好先编写测试.有了DUnit,我认为没有课程就不可能.

所以我想知道如何在重构之前测试它们?

编辑:

我认为这是不可能的,因为我试图使用测试用例向导在Delphi中添加测试用例.请看下面的图片,没有任何类和方法,所以我无法创建它.

在此输入图像描述

Mar*_*tos 8

AFAICT,DUnit不要求测试中的代码作为类方法存在.只有测试用例本身必须是类.

编辑:向导只是一个方便.你不必使用它.

  • 它手动工作.关键是向导无法为方法创建测试用例. (2认同)

Lie*_*ers 7

您无法使用向导测试独立功能,但使用DUnit测试独立功能不是问题.

  //***** A Standalone function te be tested in a unit far, far away
  function Add(v1, v2: Integer): Integer;
  ...

  //***** A testclass to contain the testmethods calling our 
  //      standalone function     
  TTestAdd = class(TTestcase)
  published
    procedure AddingComplement_ShouldEqualZero;
    procedure AddingNegativeNumbers_ShouldBeLessThanZero
    ...
  end;

  implementation

  procedure TTestAdd.AddingComplement_ShouldEqualZero;
  begin
    // Setup, Execute & Verify
    CheckEquals(0, Utils.Add(-1, 1), 'Complement doesn''t add to zero');
  end;

  procedure TTestAdd.AddingNegativeNumbers_ShouldBeLessThanZero
  begin
    // Setup, Execute & Verify
    CheckEquals(-3, Utils.Add(-1, -2), 'Add doesn''t add');
  end;
Run Code Online (Sandbox Code Playgroud)

  • @Smasher:见Marcelo的回复.当我们发布答案时,没有提到向导.AFAIK,这个向导无论如何都不会为你节省很多工作. (2认同)