Search The Blog
About this site

@RoyOsherove

Subscribe!

This site aims to connect all the dots of my online activities - from tools, books blogs and twitter accounts, to upcoming conferences, engagements and user group talks.

from 5whys.com
Twitter: @RoyOsherove
My Book: The Art of Unit Testing
Latest Posts
« Making your tests withstand design and interface changes - remove code duplication | Main | Agile Israel news and updates »
Monday
Apr112005

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
 

PrintView Printer Friendly Version

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>