使用断言与验证的指南

发布于 2024-09-03 01:11:53 字数 340 浏览 2 评论 0原文

我是单元测试新手,正在学习如何使用 NUnit 和 Moq。 NUnit 提供了 Assert 语法来测试单元测试中的条件,而 Moq 则提供了一些 Verify 函数。在某种程度上,这些似乎提供了相同的功能。

我如何知道何时使用 AssertVerify 更合适?

也许 Assert 更适合确认状态,而 Verify 更适合确认行为 (古典与模仿)?

I'm new to unit testing, and I'm learning how to use NUnit and Moq. NUnit provides Assert syntax for testing conditions in my unit tests, while Moq provides some Verify functions. To some extent these seem to provide the same functionality.

How do I know when it's more appropriate to use Assert or Verify?

Maybe Assert is better for confirming state, and Verify is better for confirming behavior (Classical versus Mockist)?

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

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

发布评论

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

评论(2

弄潮 2024-09-10 01:11:53

您关于断言确认状态和验证确认行为的假设是正确的。

您断言一个结果或一个值

您验证是否已使用适当的参数调用了方法。

Your assumption about Assert to confirm State and Verify to confirm behavior is correct.

You Assert a result, or a value

You Verify that a method has been called with appropriate parameters.

风尘浪孓 2024-09-10 01:11:53

从selenium的角度来看,Assert是一种验证,如果不满足,它会停止测试并报告失败。相反,验证是一种验证,如果不满足,则继续测试,并在执行结束时报告测试失败。

因此,如果验证是相关的,我建议使用断言。如果验证不相关,则使用验证。

参考:https://www.softwaretestingmaterial.com/difference- Between-assert -并验证/

From selenium point of view, Assert is a validation that if is not meet, it stops the test there and reports as fail. Instead, verify is a validation that if not meet, then continues with the test and it reports the test as failed at the end of the execution.

So, if the validations are dependent, I recommend to use assert. if validations are not dependent, then use verify.

Reference: https://www.softwaretestingmaterial.com/difference-between-assert-and-verify/

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