断言哪个返回 bool?
NUnit 是否有返回 bool 的 Assert 方法?他们都无效返回。我使用的是 2.5.3 版本
谢谢
Does NUnit have an Assert method that returns a bool? They all return void. I am using version 2.5.3
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Assert 类的所有成员都是空的。对于每个 void,如果断言不正确,则会引发 NUnit.Framework.AssertionException。
我想您可以围绕这些空缺编写自己的包装器,如果没有抛出异常,则返回 true,并捕获任何异常并返回 false,但我不确定这会给您带来什么功能。
您能解释一下您想要实现的目标需要断言返回布尔值吗?
All members of the Assert class are voids. For each void, if the assertion is incorrect an NUnit.Framework.AssertionException is thrown.
I suppose you could write your own wrappers around these voids that returned true if no exception was thrown and caught any exception and returned false, but I'm not sure what functionality this would give you.
Can you explain what you are trying to achieve that requires an assertion to return a bool?
哇,十多年后我来到这里,寻找这个问题的答案。
我正在编写一个类来添加到我的测试助手中,该类将在反序列化后对嵌套对象和数组执行“深度”等于。我想利用 Nunit 的 Assert.Equals()。比较不同数值类型中包含的值比听起来要困难得多。不同位大小的有符号和无符号整数、小数、浮点数、双精度数、字节、sbyte,甚至像 IntPtr 这样的奇怪东西... Assert.Equals() 可以很好地处理所有这些,即使所讨论的类型彼此不兼容。
我已经合法地使用 Nunit 作为我的测试框架,因此我想避免引入一种不同的等于比较方法,该方法可能具有与其他单元测试已经使用的方法不同的行为。
但是,我不想抛出这个断言。我想对整个深度等于中的所有差异进行分类,然后我可能决定抛出,但这与手头的问题无关。
当然,我可以轻松地将 try/catch 包装在每个相关的 Assert.Equals() 事件周围,或者将它们替换为对单个包装方法的调用;但我喜欢使用调试器来完成我的流程,并且我引入的抛出和重定向越少,就越容易遵循。
从本质上讲,深度 equals 方法是递归的,因此已经存在一些跳跃,我不想添加更多。
TLDR:我想要一个更优雅的解决方案。
我通常使用他们的Assert.Equals(预期,实际)模式,但与我合作过的很多开发人员更喜欢使用另一种 Assert.That(actual, Is.EqualTo(expected)) 模式,因为它读起来更像一个句子。
经过对反编译器的一些调查后,我发现您可以独立于 Assert.That() 使用 Is.EqualTo() 约束,完全避免异常:
完全公开,我在 .Net 4.8 中执行此操作,在撰写本文时该版本已经过时,但这也应该适用于 .net5、core、standard、mono 以及 Nunit 已移植到的其他任何地方。
Wow, I came along here over a decade later, looking for an answer to this exact question.
I'm writing a class to add to my test helpers that would perform a 'deep' equals, into nested objects, and arrays after being deserialized. I wanted to utilize Nunit's Assert.Equals(). Comparing values contained in different number value types is much harder than it first sounds. Integers, both signed and unsigned, of different bit sizes, decimals, floats, doubles, byte, sbyte, and even the odd stuff like IntPtr... Assert.Equals() holds up well with all of these, even if the types in question are incompatible with each other.
I was already legitimately using Nunit as my testing framework, so I wanted to avoid introducing a different equals comparison method that would potentially have different behavior than the one that other unit tests were already utilizing.
However, I didn't want this Assert to throw. I want to catalog all differences throughout the deep equals, and I may decide to throw afterwards, but that's irrelevant to the question at hand.
Sure, I could easily wrap a try/catch around every relevant occurrence of Assert.Equals(), or replace them with to calls to a single wrapped method; but I like to use to my debugger to walk through my process, and the less throws and redirects I introduce the easier it is to follow.
By it's very nature, a deep equals method is recursive, so there already is some bouncing around, and I don't want to add more.
TLDR: I wanted a more elegant solution.
I normally use their Assert.Equals(expected, actual) pattern, but a lot of developers I've worked with prefer to use the alternative Assert.That(actual, Is.EqualTo(expected)) pattern, because it reads more like a sentence.
After a little investigation with a decompilier, I found is that you can use the Is.EqualTo() constraint independently of Assert.That(), avoiding exceptions entirely:
Full disclosure, I'm doing this in .Net 4.8, which is pretty much obsolete at the time of this writing, but this also should work on .net5, core, standard, mono and wherever else Nunit has been ported to.