3

I've just begun reading "The Art Of Unit Testing" by Roy Osherove, and while I'm mostly finding the material very helpful, he makes a statement about not using messages in your Assert statements.

"Please never, ever, use this parameter. Just make sure your test name explains what's supposed to happen."

My question is, in you experience do you find this advice to be good or bad? If you use assert messages in your unit tests, what information do you use it to capture beyond what a well named unit test could give you in the first place?

2
  • Why do you need asserts in unit tests? Can you not just check-and-fail?
    – James
    Apr 7, 2014 at 19:55
  • Wont it fail if the assertion fails?
    – py_script
    Apr 7, 2014 at 20:01

1 Answer 1

8

This sort of advice makes two key assumptions:

  1. You're going to be spending more time looking at some pass/fail summary of all your tests than the assert output.
  2. Your tests are only testing a single thing.

Now #1 is pretty much guaranteed to be true. #2 is less often going to be true, even if it's good advice. The spirit of the advice is: you should be able to tell at a glance why your unit tests did not pass.

The book's advice combines with these two assumptions to achieve the spirit of the advice. In my experience, this spirit is really what is key to follow. Once you have a good test name, and your test only tests one thing (even if you use multiple asserts to do it) - everything else is gravy.

At that point, if you find it useful to use messages to differentiate asserts, go nuts. If you find it more useful to save time typing assert messages that nobody looks at, go nuts. The important thing is that you can tell at a glance what failed your tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.