Delphi 中的单元测试 - 你做得怎么样?

发布于 2024-07-05 05:37:37 字数 1448 浏览 10 评论 0原文

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

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

发布评论

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

评论(9

你是年少的欢喜 2024-07-12 05:37:38

我们尝试在 Delphi 5 中使用 DUnit,但效果不佳。 特别是如果您正在实现 COM 接口,我们会发现许多依赖项来设置所有测试基础设施。 不知道新版本的测试支持是否有所改善。

We tried to use DUnit with Delphi 5, but it didn't work well. Specially if you are implementing COM interfaces, we found many dependencies to setup all the test infrastructure. I don't know if the test support has improved in newer versions.

一桥轻雨一伞开 2024-07-12 05:37:38

DUnit2 可从 http://members.optusnet.com.au/~mcnabp/

DUnit2 的修改比原始 dunit 更频繁。 它也适用于 Delphi 2009。

尝试: http://sourceforge.net/projects/dunit2/ - 随着原作者 Peter McNab 几年前去世,它发生了变化。 dunit 邮件列表上仍有一些活动。

DUnit2 is available from http://members.optusnet.com.au/~mcnabp/

DUnit2 is modified more regularly than the original dunit. It also works on Delphi 2009.

Try: http://sourceforge.net/projects/dunit2/ - it moved as the original author Peter McNab passed away several years ago. Still some activity on the dunit mailing list.

很酷不放纵 2024-07-12 05:37:38

正在开发中的现代 Delphi 版本的新单元测试框架:https://github.com/VSoftTechnologies/DUnitX

There's a new unit testing framework for modern Delphi versions in development: https://github.com/VSoftTechnologies/DUnitX

雪若未夕 2024-07-12 05:37:38

您可以查看我们的 SynCommons 开源单元中提供的单元测试类。 它在我们的开源框架中用于所有回归测试。 它也许不是最好的,但值得一看。

请参阅http://blog.synopse.info/ post/2010/07/23/Unit-Testing-light-in-Delphi

为了实现单元测试,您只需通过创建这样的类来声明一个新的测试用例:

type
  TTestNumbersAdding = class(TSynTestCase)
  published
    procedure TestIntegerAdd;
    procedure TestDoubleAdd;
  end;

procedure TTestNumbersAdding.TestDoubleAdd;
var A,B: double;
    i: integer;
begin
  for i := 1 to 1000 do
  begin
    A := Random;
    B := Random;
    CheckSame(A+B,Adding(A,B));
  end;
end;

然后创建一个测试套件,并运行它。

在即将推出的 1.13 版本中,还有一种新的日志记录机制,可以对任何引发的异常进行堆栈跟踪,就像 MadExcept 一样,使用 .map 文件内容作为源。

它现在由单元测试类使用,因此任何失败都将在日志中创建一个包含源代码行和堆栈跟踪的条目:

C:\Dev\lib\SQLite3\exe\TestSQL3.exe 0.0.0.0 (2011-04-13)
Host=Laptop User=MyName CPU=2*0-15-1027 OS=2.3=5.1.2600 Wow64=0 Freq=3579545
TSynLogTest 1.13 2011-04-13 05:40:25

20110413 05402559 fail  TTestLowLevelCommon(00B31D70) Low level common: TDynArray "" stack trace 0002FE0B SynCommons.TDynArray.Init (15148) 00036736 SynCommons.Test64K (18206) 0003682F SynCommons.TTestLowLevelCommon._TDynArray (18214) 000E9C94 TestSQL3 (163) 

不带日志记录的测试套件和带日志记录的测试套件之间的区别仅在于:

procedure TSynTestsLogged.Failed(const msg: string; aTest: TSynTestCase);
begin
  inherited;
  with TestCase[fCurrentMethod] do
    fLogFile.Log(sllFail,'%: % "%"',
      [Ident,TestName[fCurrentMethodIndex],msg],aTest);
end;

日志记录机制可以做的不仅仅是记录测试:您可以记录方法的递归调用,选择要在日志中显示的信息,从客户端分析应用程序,将发布的属性、TList 或 TCollection 内容以 JSON 形式写入日志内容等等...

第一次读取 .map 文件时,会创建一个 .mab 文件,其中包含所需的所有符号信息。 您可以将带有 .exe 的 .mab 文件发送到您的客户端,甚至可以将其内容嵌入到 .exe 中。 此 .mab 文件经过优化:927,984 字节的 .map 压缩为 71,943 字节的 .mab 文件。

因此,这个单元可以被认为是 DUnit 和 MadExcept 结合的产物,完全是开源的。 :)

其他信息可以在我们的论坛上找到。 请随意询问。 欢迎提供反馈和功能请求! 适用于 Delphi 6 至 XE。

You could take a look at the unit testing classes available in our SynCommons open source unit. It's used in our Open-Source framework for all regression tests. It's perhaps not the best, but it's worth taking a look at it.

See http://blog.synopse.info/post/2010/07/23/Unit-Testing-light-in-Delphi

In order to implement an unit test, you just declare a new test case by creating a class like this:

type
  TTestNumbersAdding = class(TSynTestCase)
  published
    procedure TestIntegerAdd;
    procedure TestDoubleAdd;
  end;

procedure TTestNumbersAdding.TestDoubleAdd;
var A,B: double;
    i: integer;
begin
  for i := 1 to 1000 do
  begin
    A := Random;
    B := Random;
    CheckSame(A+B,Adding(A,B));
  end;
end;

Then you create a test suit, and run it.

In the up-to-come 1.13 version, there is also a new logging mechanism with stack trace of any raised exception and such, just like MadExcept, using .map file content as source.

It's now used by the unit testing classes, so that any failure will create an entry in the log with the source line, and stack trace:

C:\Dev\lib\SQLite3\exe\TestSQL3.exe 0.0.0.0 (2011-04-13)
Host=Laptop User=MyName CPU=2*0-15-1027 OS=2.3=5.1.2600 Wow64=0 Freq=3579545
TSynLogTest 1.13 2011-04-13 05:40:25

20110413 05402559 fail  TTestLowLevelCommon(00B31D70) Low level common: TDynArray "" stack trace 0002FE0B SynCommons.TDynArray.Init (15148) 00036736 SynCommons.Test64K (18206) 0003682F SynCommons.TTestLowLevelCommon._TDynArray (18214) 000E9C94 TestSQL3 (163) 

The difference between a test suit without logging and a test suit with logging is only this:

procedure TSynTestsLogged.Failed(const msg: string; aTest: TSynTestCase);
begin
  inherited;
  with TestCase[fCurrentMethod] do
    fLogFile.Log(sllFail,'%: % "%"',
      [Ident,TestName[fCurrentMethodIndex],msg],aTest);
end;

The logging mechanism can do much than just log the testing: you can log recursive calls of methods, select the information you want to appear in the logs, profile the application from the customer side, writing published properties, TList or TCollection content as JSON into the log content, and so on...

The first time the .map file is read, a .mab file is created, and will contain all symbol information needed. You can send the .mab file with the .exe to your client, or even embed its content to the .exe. This .mab file is optimized: a .map of 927,984 bytes compresses into a 71,943 .mab file.

So this unit could be recognized as the natural child of DUnit and MadExcept wedding, in pure OpenSource. :)

Additional information is available on our forum. Feel free to ask. Feedback and feature requests are welcome! Works from Delphi 6 up to XE.

等风也等你 2024-07-12 05:37:38

通常我创建一个单元测试项目(文件->新建->其他->单元测试->测试项目)。 它包含我需要的东西,所以到目前为止已经足够好了。

我使用delphi 2007,所以我真的不知道2006年是否可用。

Usually I create a Unit test project (File->New->Other->Unit Test->Test Project). It contains the stuff I need so it's been good enough so far.

I use delphi 2007 so I don't really know if this is available in 2006.

|煩躁 2024-07-12 05:37:38

我们使用 DUnit 对所有逻辑代码进行单元测试,并使用 AQTime< 中包含的代码覆盖率分析器/a> 检查代码中的所有路径是否都由测试执行。

We do unit testing of all logic code using DUnit and use the code coverage profiler included in AQTime to check that all paths through the code are executed by the tests.

女中豪杰 2024-07-12 05:37:38

我们有两种方法,首先我们有由开发人员运行的 Dunit 测试 - 这些可以确保刚刚更改的代码仍然像以前一样工作。 另一种方法是使用 CruiseControl.NET 构建可执行文件,然后在每次进行更改时运行 dunit 测试,以确保更改不会产生意外后果。

我们的大部分代码库都没有测试,因此自动测试是持续开发的一个例子,以确保我们的应用程序按我们认为的方式工作。

We have two approaches, first we have Dunit tests that are run buy the developers - these make sure that the code that has just been changed still works as before. The other approach is to use CruiseControl.NET to build executables and then run the dunit tests everytime a change is made, to ensure that there are no unintended consequences of the change.

Much of our codebase has no tests, so the automatic tests are a case of continuous development in order to ensure our applications work as we think they should.

何止钟意 2024-07-12 05:37:38

DUnit 有一些附加组件,也许这值得在 SO 上添加一个新条目。 我现在可以放在列表中的两个是

  1. FastMM4集成:单元测试将自动检测内存泄漏(和其他事情),适用于 DUnit 9.3 和更新版本
  2. OpenCTF 是一个“组件测试”
    基于DUnit的框架',它
    动态创建测试
    项目中的所有组件
    表单、框架和数据模块,以及
    使用自定义规则测试它们(开源)

There are some add-ons for DUnit, maybe this is worth a new entry on SO. Two which I can put on the list now are

  1. FastMM4 integration: Unit tests will automatically detect memory leaks (and other things), works with DUnit 9.3 and newer
  2. OpenCTF is a 'component test
    framework' based on DUnit, it
    creates the tests dynamically for
    all components in the project's
    forms, frames and datamodules, and
    tests them using customized rules (open source)
神魇的王 2024-07-12 05:37:38

DUnit 是与 win32 Delphi 一起使用的 xUnit 类型的单元测试框架。 自 Delphi 2005 起,DUnit 已集成到 IDE 的某个点。 Delphi IDE 的其他 DUnit 集成工具可以在 找到在这里。 DUnit 附带带有示例的文档

DUnit is a xUnit type of unit testing framework to be used with win32 Delphi. Since Delphi 2005 DUnit is integrated to a certan point into the IDE. Other DUnit integration tools for the Delphi IDE can be found here. DUnit comes with documentation with examples.

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