当您进行单元测试时,断言是否多余?
我还没有习惯编写单元测试,我想在小工具的完整框架上执行此操作(使其使用起来更安全)。这样我肯定会学到比到目前为止学到的更多的单元测试知识。
然而,我确实习惯于在任何我看到有上下文需要确定的地方系统地添加断言(在最终版本中被删除)。主要是作为函数实现的先决条件,以及每次我检索必须正确的信息(例如一个著名示例的 C/C++ 指针有效性)。
现在我问:当你进行单元测试时,断言是多余的吗?因为当您测试一些代码的行为时,它看起来是多余的;但同时它不是相同的执行上下文。
我应该两者都做吗?
I'm not used yet to write unit tests and I want to do this on a full framework of little tools (making it more secure to use). That way I'll certainly learn more about unit tests than what I learnt until now.
However I'm really used to add assertions systematically everywhere I see there is a context to be sure about (that are removed in the final release). Mostly as preconditions in functions implementations and each time I retrieve informations that have to be correct (like C/C++ pointers validity for a famous example).
Now I'm asking : are assertions redundant when you have unit tests? Because it looks redundant as you're testing the behaviour of a bit of code; but in the same time it's not the same execution context.
Should I do both?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
检查先决条件的断言可以帮助检测和定位集成错误。也就是说,虽然单元测试证明方法在正确使用(调用)时可以正确运行,但检查先决条件的断言可以检测对该方法的错误使用(调用)。使用断言会导致错误代码快速失败,这有助于调试。
Assertions that check preconditions can help detect and locate integration bugs. That is, whereas unit tests demonstrate that a method operates correctly when it is used (called) correctly, assertions that check preconditions can detect incorrect uses (calls) to the method. Using assertions causes faulty code to fail fast, which assists debugging.
断言不仅验证代码,而且作为一种文档形式,告知读者各种结构的属性,他们可以确定在执行时会满足这些属性(例如
node->next != NULL
)。这有助于在您阅读代码时创建代码的心理模型。断言还可以防止运行时出现灾难情况。例如,
在没有燃料的情况下尝试发射可能会造成灾难性的后果。您的单元测试可能已经捕获了这种情况,但您总是有可能错过它,并且由于未满足条件而中止比运行时崩溃和烧毁要方式。
所以,简而言之,我会把它们留在那里。添加它们是一个好习惯,忘记它也没有任何好处。
Assertions not only validate the code but serve as a form of documentation that informs readers about properties of various structures that they can be sure will be satisfied at that point in execution (e.g.
node->next != NULL
). This helps to create a mental model of the code when you're reading through it.Assertions also serve to prevent disaster scenarios during runtime. e.g.
Trying to launch when there is no fuel might be disastrous. Your unit tests might have caught this scenario but it's always possible that you've missed it and an abort because a condition is unmet is Way better than a crash and burn during runtime.
So, in short, I'd keep them there. It's a good habit to add them and there's no gain in unlearning it.
我想说你两者都需要。单元测试测试您的方法内断言是否正确;)
I would say that you need both. Unit tests test that your in-method asserts are correct ;)
单元测试和断言是截然不同的事情,但它们是相辅相成的。
断言处理方法的正确输入输出。
单元测试旨在确保单元按照某些管理规则(例如规范)工作。一个单元可以是 ax 方法、一个类或一组协作类(有时此类测试称为“集成测试”),
因此方法平方根的断言沿着输入和输出的方向都是非负的数字。
另一方面,Unitests 可以测试 9 的平方根是否为 3,并且某个负数的平方根会产生异常。
Unit testing and assertions are vastly different things, yet they complement each other.
Assertions deals with proper input-output of methods.
Unit tests deal with ensuring a unit works according to some governing rules, e.g. a specification. A unit may be ax method, a class, or a set of collaborating classes (sometimes such tests go by the name "integration tests")
So the assertions for a method square root are along the lines of the input and output both be non negative numbers.
Unitests on the other hand may test that square root of 9 is 3 and that square root of some negative number yields an exception.