What should you write in a test Assert message? - ISerializable - Roy Osherove's Blog

What should you write in a test Assert message?

The Assert message in a test is one of the most important parts. It's there to tell us what went wrong with our code. what we expected to happen but didn't, and what should have happened instead. A good assert message helps us track bugs and understand unit tests more easily.
Sadly, most developers new to automated unit tests usually leave the assert messages to last (which means they never get written correctly).
 
DO
  • Express what should have happened or what did not happen
    • "Foo should have thrown an exception"
    • "Foo did not throw any exception" (not as good as the previous one)
    • "Foo should have returned a new ID"
    • "Foo should not allow negative numbers as parameters"
    • "Foo should open the connection before returning it"
    • "Foo did not open the connection before returning it"
    • "Person class did not have a valid GUID by default"
Don't
  • Provide empty or meaningless messages
  • Provide messages that repeat the name of the test case
  • Provide messages that simply state the test inputs
 
Published Monday, April 11, 2005 1:50 AM by RoyOsherove

Comments

Sunday, April 10, 2005 11:26 PM by TrackBack

# New Team System Stuff - 2005-04-11

Visual Studio Team System

Richard Campbell has announced that he and Carl Franklin will tape an episode...
Thursday, April 14, 2005 8:43 PM by Dale Emery

# re: What should you write in a test Assert message?

I find that I use the explanatory messages less and less.

If I use great names for my tests (as you recommend in an earlier article), the test name usually tells me everything I need to know about the failure.

The only time I need explanatory messages is when I have multiple assertions in one test method. But that violates another of your excellent articles (about testing only one thing).

As I make each test test only one thing, I need multiple assertions less and less. And as I write great names for tests, the explanatory messages become redundant.
Thursday, April 14, 2005 9:56 PM by Andy Sipe

# re: What should you write in a test Assert message?

I also find myself using messages in asserts less and less.

I find the use of the messages in asserts to be a subtle form of duplication. As tests & code get refactored the messages tend not get updated and over time the messages become misleading. The test name/method name is usually enough.

Thursday, April 21, 2005 10:18 PM by TrackBack

# New Team System Stuff - 2005-04-11

Friday, June 01, 2007 12:56 PM by Gary Sherman

# Elevating consulting-ware

Back when I was working at Clarify, a good chunk of my time was spent doing custom development work,