是否有使用 NUnit 测试复杂函数的通用方法?
有没有一种通用方法可以使用 NUnit 测试具有多个参数的复杂函数?我认为测试每个条件是非常困难或不可能的。
恐怕函数中未预期的参数组合在测试中也不是预期的。
所以预期的情况不会失败,而是意外的情况发生。
谢谢
is there a common way to test complex functions with several parameters with NUnit? I think it is very hard or impossible to test every condition.
I'm afraid the combination of parameters that isn't expected in the function is also not expected in the test.
So the expected condition will not fail but the unexpected.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这应该不难测试。如果是,则该方法不是为可测试性而设计的,这是一种代码味道,告诉您需要重构它。
我倾向于在这些情况下编写测试如下(其他人可能有更好的建议):
如果您的方法采用大量参数,请考虑重构它以采用带有信息的对象,以便您可以将其规则封装在对象中,并将对象传递给方法。
This shouldn't be hard to test at all. If it is, the method isn't designed for testability, and that is a code smell that tells you that you need to refactor it.
I tend to write tests in these cases as follows (others may have better suggestions):
If your method takes a lot of parameters, consider refactoring it to take an object with the information on it, so that you can encapsulate the rules for it in the object, and pass the object to the method.
对于 NUnit 中的数据驱动测试,有
[TestCase]
属性。单元测试通常不会测试所有可能的场景。他们只是测试代表性的输入集,这些输入集很好地覆盖了 SUT 对各种输入的操作。只需选择一些特征输入,就可以了。For data-driven tests in NUnit, there is
[TestCase]
attribute. Unit tests usually dont't test every possible scenario. They just test representative set of inputs, which have good coverage of what the SUT does on various inputs. Just pick some characteristic inputs, and you'll be fine.不知道这是否是您正在寻找的东西,但是有一个由 Microsoft Research 创建的自动化单元测试生成器,名为 PEX。
Don't know if this is the kind of thing you are looking for, but there is an automated unit test generator that was created by Microsoft research called PEX.
使用RowTest类似的问题可以在
C#, NUnit Assert in 找到a Loop
看看@“Sam Holder”的回复,我从中复制了代码,做了一些调整。
Use RowTest similar question can be found at
C#, NUnit Assert in a Loop
have a look at @"Sam Holder" reply, I copied the code from it, with few tweaks.
我同意 Mike Hofer 的观点,这个问题表明了代码的味道。
尽管如此,NUnit 有一个组合属性,如果你没有重构/重新设计。
I agree with Mike Hofer, that the question indicates a code smell.
Nevertheless, NUnit has a Combinatorial attribute that might help you, if you're not refactoring/redesigning.