简体   繁体   English

JUnit断言方法应该用正面还是负面的措辞?

[英]JUnit assertion methods should be phrased in the positive or the negative?

Should I be writing 我应该写作吗?
assertTrue("User logged in", user.isLoggedIn());
or 要么
assertTrue("User is not logged in", user.isLoggedIn());

The former provides better reading inside the source files: 前者在源文件中提供了更好的读取:
"I assert that the following is true: User logged in." “我断言以下情况属实:用户已登录。”

The error message could be read both ways: 可以通过两种方式读取错误消息:
java.lang.AssertionError: User logged in
"There is an error in asserting that the user is logged in" “声明用户已登录时出错”
"The error is that the user is logged in." “错误是用户已登录。”

JUnit documentation doesn't provide a clear guide to which it should be, except it is JUnit文档没有提供它应该是什么的明确指南,除了它
"the identifying message for the {@link AssertionError}", “{@link AssertionError}的识别信息”,
And in both cases, the text identifies the test being run. 在这两种情况下,文本都标识了正在运行的测试。

What's the common usage? 常见用法是什么?

How about: 怎么样:

assertTrue("User should be logged in", user.isLoggedIn());

Works both ways. 两种方式都有效。

Be strictly positive in your assert messages 在断言消息中严格要求肯定

Use positive assert text like in your first example, or like: 像第一个例子中那样使用正断言文本,或者像:

assertTrue("User is logged in", user.isLoggedIn());

The reasons are: 原因是:

  • positive assertion is shorter 积极的断言更短
  • there is one asserted condition you are checking, and many possible reasons, why it went wrong. 你正在检查一个断言的条件,以及许多可能的原因,为什么它出错了。 Do not attempt to detect the cause, just state, what assertion has failed. 不要试图检测原因,只是说明,断言失败了什么。
  • it is more readable in your code. 它在您的代码中更具可读性。 It is generally recommended to code in positive expressions, which save few negations of the conditions in the mind of the reader 通常建议使用正面表达式进行编码,这样可以减少读者心中对条件的否定
  • it is still readable in error traces, which are not supposed to be understood by common user, but by a programmer, who will end up in code anyway. 它仍然可以在错误跟踪中读取,这些跟踪不应该由普通用户理解,而是由程序员理解,他们最终将以代码结束。 And even sysadmin, who would not have access to the code would provide author with complete error message and programmer would understand, it comes from an assert. 甚至那些无法访问代码的sysadmin也会为作者提供完整的错误信息,程序员会理解,它来自一个断言。

Trying to provide "all contextual information" in assert message is not improving the situation, it rather creates information mess. 试图在断言消息中提供“所有上下文信息”并不能改善这种情况,而是会造成信息混乱。

You know, good programmers debug a code and provide working and shorter code . 你知道, 优秀的程序员调试代码并提供工作和更短的代码

Take using positive assert messages as first thing to do in this direction. 使用肯定的断言消息作为这个方向的第一件事。

The other direction - patching the code with more and more unnecessary stuff is paving way to the programming hell. 另一个方向 - 用越来越多不必要的东西修补代码正在为编程地狱铺平道路。

Well, you could also state your assumption, and then how the assumption didn't hold. 那么,你也可以陈述你的假设,然后假设不成立。 Like so: 像这样:

assertTrue("Expected user to be logged it, and wasn't", user.isLoggedIn());

Makes for clearer messages, but longer to type and read. 使消息更清晰,但输入和读取的时间更长。

To avoid that question, I more and more tend to use assertThat instead of "low-level" assert* methods. 为了避免这个问题,我越来越倾向于使用assertThat而不是“低级”断言*方法。 Indeed, like this article explains, assertThat will give you a very clear error message in case of failure. 实际上,就像这篇文章所解释的那样,assertTat会在出现故障时给你一个非常明确的错误信息。

You could use : 你可以使用:

assertTrue("Test if user is logged in", user.isLoggedIn());

When you do this, you're verifying that user.isLoggedIn() is true, you can't really say that user is logged in or not, you don't know yet, you're just testing it. 执行此操作时,您将验证user.isLoggedIn()是否为true,您无法确定用户是否已登录,您还不知道,您只是在测试它。

Interesting, I would use: 有意思,我会用:

assertTrue("user should be logged in", user.isLoggedIn());

which tells me what is expected state of this assertion. 这告诉我这个断言的预期状态是什么。

I think the best choice is the one you understand. 我认为最好的选择就是你理解的那个。

You should include both cases. 你应该包括两种情况。 You have better test case when you triangulate your assertions. 当你对断言进行三角测量时,你有更好的测试用例。

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

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