Today I came across Shouldly, as I followed a link in a tweet by Rob Conery. I have a thing for nifty mini-projects, so I immediately git clone http://github.com/shouldly/shouldly.git‘d it, and pulled it into a small thing I am building.

What is Shouldly? Basically, it’s just a niftier way of writing assert statements in tests. It fits right in between NUnit and Rhino Mocks, so you will get the most out of it if you are using those two for your unit tests. Check out this repository test – first, arrange and act:

and then, usually the assert would look something like this:

– yielding error messages like this:

which is probably OK and acceptable, because how would NUnit know any better than that?

Check out what Shouldly can do:

– yielding error messages like this:

which IMO is just too nifty to ignore!

Shouldly takes advantage of the fact the the current StackTrace has all the information we’re after when the assert is an extension method, which is extremely cool and well thought out.

Moreover, it integrates with Rhino Mocks, yielding better messages when doing AssertWasCalled stuff: Instead of just telling that the test did not pass, it tells you exactly which calls were recorded and which one was expected – a thing that Rhino Mocks has always been missing.

Conclusion: YourNextProject.ShouldContain("Shouldly")

Shouldly – better assertions

4 thoughts on “Shouldly – better assertions

  • 2010-09-21 at 13:47
    Permalink

    Very nice! I’ve always hated the Assert.AreEqual(…) syntax. The main issue with the AreEqual syntax, is the parameter order confusion. Over the last year or so, I’ve switched to the Assert.That(…) syntax. But this looks way more promising. Good find, mookid.

    Reply
  • 2010-09-21 at 16:13
    Permalink

    Yeah, I agree that Assert.AreEqual(…) is sub-optimal, but I think the Assert.That(…) syntax is clunky and awkward to type. Fluent interfaces are seldomly good to speed-type.

    Reply
  • 2010-09-22 at 18:35
    Permalink

    Oh yeah, Fabio Maulo used to tweet a lot about SharpTestsEx, but I never got interested in that one.

    The interesting part is IMO not about having extension methods to improve readability, it’s about having better error messages when assertions fail. Does NUnitEx/SharpTestsEx provide that as well? Can’t seem to find any information about that.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: