简体   繁体   English

断言与真断言

[英]Assert.That vs Assert.True

What to prefer: 喜欢什么:

Assert.That(obj.Foo, Is.EqualTo(true))

or 要么

Assert.True(obj.Foo)

For me, both asserts are equivalent, so which one should be prefered? 对我来说,这两个断言是等价的,那么应该首选哪一个呢?

In this particular case, there is no difference: you will see the output of roughly the same level of detail (ie it tells you that something that was expected to evaluate to true has evaluated to false ). 在这种特殊情况下,没有什么区别:您将看到大致相同级别的详细信息的输出(即,它告诉您预期要评估为true某些内容已评估为false )。 Same goes for 一样

Assert.IsTrue(obj.Foo);

and

Assert.That(obj.Foo, Is.True);

Your team should pick one style of assertions, and stick with it throughout all your tests. 您的团队应选择一种断言样式,并在所有测试中坚持使用。 If your team prefers the Assert.That style, then you should use Assert.That(obj.Foo, Is.True) . 如果您的团队更喜欢Assert.That样式,则应使用Assert.That(obj.Foo, Is.True)

Assert.That is called the constraint-based model. Assert.That这称为基于约束的模型。 It's flexible because the method takes a parameter of IConstraint type. 它很灵活,因为该方法采用IConstraint类型的参数。 This means you can structure your code with a more generic hierarchy or calling structure, passing in any old IConstraint . 这意味着您可以使用更通用的层次结构或调用结构来构造代码,并传入任何旧的IConstraint It means you can build your own custom constraints 这意味着您可以建立自己的自定义约束

That's a design issue. 那是一个设计问题。 As everyone is saying, no matter what you still need to provide decent error message feedback. 就像大家都说的那样,不管您仍然需要提供什么体面的错误消息反馈。

So ok, you executed your test suite on CI server and unfortunately, one of them failed. 好的,您在CI服务器上执行了测试套件,不幸的是,其中一个失败了。 You open the logs and see next message 您打开日志并查看下一条消息

 BusinessLogicTests.LoginTests.UserAutoLoginTests failed: expected true but was false 

Now how on earth would you get what happened wrong with this tests, if all the information you see, is that somewhere in AutoLoginTests bool was expected true, but received false? 现在,如果您看到的所有信息都是,AutoLoginTests bool中的某个地方应该是正确的,但收到的是错误的,那么您将如何弄清楚该测试出了什么问题? Now you need to go to the source file of your tests cases and see, what assertion failed. 现在,您需要转到测试用例的源文件,看看什么断言失败了。 You see 你看

Assert.True(obj.Foo)

Amazingly.. it's still hard to tell what's wrong, only if you developed this module 1 hour ago. 令人惊讶的是,仅在1小时前开发了此模块,仍然很难分辨出问题所在。 You still need to go deeper into tests sources and probably production sources or even debug your code, so that you can at last figure out, that you misspelled a variable inside function call or used wrong predicate to filter registered users. 您仍然需要更深入地研究测试源,甚至可能是生产源,甚至调试您的代码,以便最终弄清楚函数调用中的变量拼写错误或使用错误的谓词来过滤注册用户。 Thus you blocked immediate feedback from tests, which is very valuable. 因此,您阻止了测试的即时反馈,这非常有价值。

My point is that it's dosn't matter how fluent your assertions are (which are also relevant), but what information you expose in case of failing tests and how fast can you get the underlying reason of failure, even if you worked a long time ago with this functionaluty 我的观点是,断言的流畅程度(也很相关)都无关紧要,但是即使测试时间很长,在测试失败的情况下您可以暴露哪些信息,以及获得故障的根本原因的速度有多快以前有这个功能

This is a bit nit-picky but IMHO I think that in this case Assert.That is unnecessarily verbose and can therefore be regarded as obfuscation. 这有点挑剔,但恕我直言,我认为在这种情况下Assert.That 。这是不必要的冗长,因此可以视为混淆。 In other words, Assert.True is a little cleaner, more straightforward and easier to read and therefore comprehend. 换句话说, Assert.True稍微更清洁,更直接,更易于阅读,因此可以理解。

To get a little more nit-picky I would suggest using the Assert.IsTrue API instead of Assert.True since, again IMHO, IsTrue "reads" better. 为了更加挑剔,我建议使用Assert.IsTrue API而不是Assert.True因为恕我直言, IsTrue更好地“读取”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM