我如何测试函数和过程,因为它们不属于 Delphi 中的类?
我在一个名为 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
您无法使用向导测试独立函数,但使用 DUnit 测试独立函数不是问题。
示例
You can't test a standalone function using the wizard but it's not a problem to test a standalone function with DUnit.
Example
真正的代码需要维护。真实代码的假设没有得到很好的记录。真正的代码是由忘记或从不知道这些假设的人更改的。相信测试,不要相信代码。
真正的 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.