代码契约真的有助于单元测试吗?

发布于 2024-09-11 06:44:59 字数 120 浏览 8 评论 0原文

我对单元测试有相当多的了解。我一直在尝试阅读有关代码契约的内容。它真的有助于单元测试吗?它是否被高估了,尤其是当我们谈论代码契约有助于进行单元测试时。我特意指的是 .net 4.0 中的合约。我使用 nunit 进行单元测试。

I have fair amount of knowledge on unit testing. I have been trying to read about code contracts. Does it really help unit testing? Is it over-rated especially when we talk about code-contract helping to do unit testing. I am specifically referring to contracts in .net 4.0. I use nunit for unit testing.

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

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

发布评论

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

评论(3

生生漫 2024-09-18 06:44:59

。单元测试基本上是一个契约,它规定 MyMethod() 接受 X 并期望 Y 将成为结果,如果它不这样做,那么单元测试就会失败,并且作为 MyMethod() 的开发人员,您会收到警告,指出您破坏了里面有东西。代码契约确实可以帮助您编写单元测试,因为契约中的需求使您在编写单元测试时更容易了解单元测试的需求。然而,代码契约的真正原因不是为了您,而是为了使用您创建的 API 的其他开发人员。单元测试可以让您了解正确的输入和输出,但是当您将代码发布到野外时,单元测试不会与 .dll 一起发布。代码契约使其他开发人员能够通过编译时契约和检查了解这些相同的需求。合约可以防止那些有不阅读方法文档并开始传递内容的可怕倾向的开发人员(我),所以现在他们将通过合约得到主动警告。

Yes and No. Unit tests are basically a contract that says, MyMethod() takes X and expects that Y will be a result, and when it doesn't do that, then unit tests fail and you are alerted as the developer of MyMethod() that you broke something inside it. Code contracts do help you write unit tests, because the requirements in the contracts make it easier for you to know the requirements of the unit tests when you write them. However, the true reason for code contracts is not for you, but for other developers using the API that you create. Unit tests let you know proper inputs and outputs, but when you release code into the wild, unit tests aren't released with the .dll. Code contracts give other developers the benefit of knowing, through compile-time contracts and checking, those same requirements. Contracts protect against those developers (me) that have a horrible tendency to not read the method documentation and just start passing in things, so now they will be actively warned through contracts.

寄离 2024-09-18 06:44:59

代码契约可用于无法使用单元测试的事情(接口契约)。它们应用于继承链(在手动单元测试中很容易犯错误)。它们自动提供文档(单元测试无法做到这一点)。他们可以在生产中提供运行时检查(单元测试无法做到这一点)。

另一方面,契约只有在执行时才会失败,因此如果没有单元测试,您就无法保证代码质量(即所有代码都满足各种契约)。这两个概念是互补的。

Code contracts can be used for things you can't use unit tests (contracts for interfaces). They are applied in inheritance chains (where you can easily make mistakes with manual unit testing). They provide documentation automatically (something unit tests can't do). They can provide runtime checks in production (something unit tests can't do).

On the other hand, contracts only fail when they are excercised, and so without unit tests you have no assurances of code quality (i.e. that all of your code fullfills the various contracts). The two concepts are complimentary.

[旋木] 2024-09-18 06:44:59

不,我不认为代码契约可以帮助您编写单元测试。单元测试定义给定操作的行为和约束。单元测试中编写的规范之一可能是方法的参数不能为空。

在这种情况下,您仍然需要编写单元测试。代码契约是实现规范的一种方式,但不是唯一的方式。

换句话说,不要假设使用代码契约意味着您不必编写单元测试!如果有人更改或删除代码契约,您将不会有测试告诉您预期的规范已失败。

No, I don't think that code contracts help you write unit tests. Unit tests define the behavior and constraints of a given action. One of those specifications written in the unit tests might be that arguments to a method cannot be null.

In that case, you still need to write the unit test. The code contract is a way to implement your specification, but not the only way.

In other words, do not assume that using a code contract means that you do not have to write a unit test! If someone changes the code contract or removes it, you will not have a test telling you that that intended specification has failed.

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