开发者

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

开发者 https://www.devze.com 2023-01-13 05:05 出处:网络
Should I be writing assertTrue(\"User logged in\", user.isLoggedIn()); or assertTrue(\"User is not logged in\", user.isLoggedIn());

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

"the identifying message for the {@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.

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. Indeed, like this article explains, assertThat will give you a very clear error message in case of failure.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消