使用断言与验证的指南
我是单元测试新手,正在学习如何使用 NUnit 和 Moq。 NUnit 提供了 Assert 语法来测试单元测试中的条件,而 Moq 则提供了一些 Verify 函数。在某种程度上,这些似乎提供了相同的功能。
我如何知道何时使用 Assert
或 Verify
更合适?
也许 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您关于断言确认状态和验证确认行为的假设是正确的。
您断言一个结果或一个值
您验证是否已使用适当的参数调用了方法。
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.
从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/