What makes a good failure message for testunit or other nunit style frameworks?

倾然丶 夕夏残阳落幕 提交于 2019-12-23 13:16:12

问题


In Ruby's test/unit, and other such nunit style frameworks, what makes a good failure message?

Should the failure message merely describe how the expected value does not match the expected value?

assert_match("hey", "hey this is a test", "The word does not exist in the string")

Should it describe what you expected to happen?

assert_match("hey", "hey this is a test", "I expected hey to be in the string")

Should it describe why you wanted the behavior to happen?

assert_match("hey", "hey this is a test", "Program should provide a greeting")

Should it describe why you thought the test may fail?

assert_match("konnichiwa", "konnichiwa this is a test",
  "Program failed to use supplied i18n configuration")

Should information about tests also exist in the name of the test method, and in the name of the test case?

This is based on Ruby "test/unit" , how do I display the messages in asserts


回答1:


the failure message is supposed to add context to the failure message. So anything that saves you having to drill into the test code to know what failed.

So if the [method name, expected, actual] set is adequate for the above purpose, you can skip the failure message. If you need more information, then you add the optional failure message.

e.g. Expected true but was false, doesn't tell me anything.

You can use a failure message so that Return value should contain only multiples of 10. Expected true but was false

You can first try to use more descriptive matchers. So that failures read Expected all items to be divisible by 10 but was [10,20,35,40] does.

Personally I prefer matchers... use failure messages as the last resort. (because like comments, it decays. You need discipline to ensure that the failure message is updated if you change the check.)



来源:https://stackoverflow.com/questions/8468410/what-makes-a-good-failure-message-for-testunit-or-other-nunit-style-frameworks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!