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

发布于 2024-11-15 05:55:39 字数 303 浏览 3 评论 0原文

我在一个名为 Utils.pas 的旧单元中有几个小函数。

现在我想重构其中一些,但我认为最好先编写测试。对于 DUnit,我认为没有类是不可能的。

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

编辑:

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

在此处输入图像描述

I have several little functions in an old Unit called Utils.pas.

Now I'd like refactoring some of them, but I think it's better to write test before. With DUnit I think it's impossible without a class.

So I'd like to know how can I test them before refactoring?

Edit:

I thought it was impossible because I was trying to add a test case in Delphi using Test Case Wizard. See the picture bellow that there aren't any classes and methods, so I'm not be able to create it.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦明 2024-11-22 05:55:39

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

编辑:向导只是为了方便。你不必使用它。

AFAICT, DUnit does not require code under test to exist as class methods. Only the test cases themselves must be classes.

EDIT: The wizard is just a convenience. You don't have to use it.

诗化ㄋ丶相逢 2024-11-22 05:55:39

您无法使用向导测试独立函数,但使用 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;

You can't test a standalone function using the wizard but it's not a problem to test a standalone function with DUnit.

Example

  //***** 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;
等风也等你 2024-11-22 05:55:39

真正的代码需要维护。真实代码的假设没有得到很好的记录。真正的代码是由忘记或从不知道这些假设的人更改的。相信测试,不要相信代码。

真正的 TDD 允许您在实现之前创建对象及其方法。无论如何,在编写测试用例之前,您需要一个清晰的模型。

因此,生成对象,添加方法、参数等。可能使用 UML2 是最好的,然后为它们编写测试用例,然后实现对象。之后运行分析器并找出您的代码到底有多糟糕,然后进行重构。

作为一般解决方案,几乎总是最好编写工厂对象来实例化和初始化对象。您越接近核心功能,这一点就越重要。

为预期的失败和异常编写测试。使用检查来确定。

最后编写每个测试并观察它失败,然后再编写代码使其成功。

Real code needs to be maintained. Real code has assumptions that are not well documented. Real code is changed by people who forget or never knew those assumptions. Trust the tests, dont trust the code.

Real TDD allows you to create the object and its methods before implementation. You need a clear model before you can write a test case anyway.

So generate the object(s), add the methods, parameters etc. Probably using UML2 would be best, then write the test cases for those, and then implement the objects. After that run the profiler and find out how horrible your code really is, and refactor.

As a general solution it is almost always best to write a factory object to instantiate and initialize your objects. The closer you get to core functionality the more this becomes important.

Write tests for your expected failures and exceptions. use a check to make sure.

Finally write each test and watch it fail before you write the code to make it succeed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文