问题描述
我可以用两种方式之一写一个断言消息.说明成功:
I can write an assertion message one of two ways. Stating success:
assertEquals( "objects should be identical", expected, actual );
或说明被破坏的条件:
assertEquals( "objects aren't identical", expected, actual );
JUnit是否专门为此提供了标准?如果不是,那么双方的论点是什么?
Is there a standard for this in JUnit specifically? If not, what are the arguments for each side?
P.S.我在网上看到的文章都在没有说明的情况下展示了这两个文章,因此只说搜索Google"是无法解决的!
P.S. I've seen articles on the web demonstrating both of these without explanation, so just saying "search Google" is not an answer!
[UPDATE]
每个人都对我使用assertEquals
感到困惑,因此该消息可能毫无用处.但这当然只是因为我想简单地说明这个问题.
Everyone is getting hung up on the fact that I used assertEquals
and therefore the message is probably useless. But of course that's just because I wanted to illustrate the question simply.
所以想象一下它是:
assertTrue( ... big long multi-line expression ... );
一条消息有用的地方.
推荐答案
我很少打扰消息,至少对于assertEquals
是如此.任何明智的测试跑步者都会向您说明您正在使用assertEquals
以及本应相等的两件事.您的任何一条消息都没有提供更多的信息.
I rarely even bother with a message, at least for assertEquals
. Any sensible test runner will explain that you were using assertEquals
and the two things which were meant to be equal. Neither of your messages give more information than that.
我通常会发现单元测试失败是暂时性的-我会迅速找出问题所在并加以解决. 找出问题所在"通常涉及足够的细节,以至于单个消息不会有太大改变.考虑通过收到一条消息节省的时间"与花费在思考消息上的时间":)
I usually find that unit test failures are transient things - I'll rapidly find out what's wrong and fix it. The "finding out what's wrong" usually involves enough detail that a single message isn't going to make much difference. Consider "time saved by having a message" vs "time spent thinking of messages" :)
好的,我可能使用一条消息:当文本中有一个紧凑的描述,而该描述从对象的字符串表示中不明显时.
Okay, one case where I might use a message: when there's a compact description in text which isn't obvious from the string representation of the object.
例如:比较以毫秒为单位存储的日期时,预计日期为12月1日".
For example: "Expected date to be December 1st" when comparing dates stored as milliseconds.
我不会担心您如何准确地表达它:只需确保从消息中清楚地表明您的意思. 应该"或不是"都可以-只是"12月1日"就不明显了.
I wouldn't worry about how you express it exactly though: just make sure it's obvious from the message which way you mean. Either "should be" or "wasn't" is fine - just "December 1st" wouldn't be obvious.
这篇关于JUnit消息是否应该说明成功或失败的条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!