I tried out pyHamcrest (which is hamcrest for python, obviously) and almost immediately fell in love with it. Writing assertions using it means typing somewhat more code, but in the end results are much nicer.

Old style of writing assertions is somewhat like:

assert len(sections) > 3

With Hamcrest, I would write it:

assert_that(sections, has_length(greater_than(3)))

So, more code, but somewhat closer to natural language than in the first way. Where the Hamcrest shines is reporting failures though. Instead of old: assertation failed message, you get following:


Expected: an object with length of a value greater than <3> but: was [obj at 0x01…] with length of <1>”

So, it tells you what was expected, what was the real result and why it didn’t match to the expected result. Extracting that error and presenting it on a report is actually useful and gives to even to a non-programmer an idea why the test case was reported as a failure.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s