Making Tests More Readable with Fluent Assert

Posted almost 2 years ago on April 11, 2010

Last week I discovered the Fluent Assert project. It allows me to write tests that even humans can read:

test_example3

Now, isn’t your mouth watering for some fantastically readable .Net BDD tests?

I like this style because my previous attempts at making unit tests readable has always seemed like somewhat of a failure. Using this framework there is really no room for readability or design error.

What you end up with is very short, terse, well documented methods that do one thing. That sounds very clean code-like! ...and it is.

This framework drives me to a good unit test fixture design in the same way my unit tests drive me to a good production code design.

Another important note about this framework: it is extremely simplistic. The framework really does nothing more than take the test methods that you write and give you a nice fluent syntax.

Added Goodness

All of the short methods and good naming that this framework leads you to gives you a lot of re-usability. Consider the test that I write to ensure that the pie is not done when it’s been baking for only 30 minutes:

test_example2

All of my previous setup and arrangement can be re-used without having to use inheritance or any other tricks to keep my code as readable as possible.

Minor Nit

There is duplication going on with my naming. I already write my fixture class name and unit test methods with the BDD style, and now I’m repeating myself with this fluent syntax. Example:

Capture

Perhaps this is less of a problem with the Fluent Assert framework and more a problem with the C# language!

Comments

Jon Kruger writes...

Check out what Michael is doing...
http://github.com/walkingdisaster/lingual

April 19, 2010

Jeff writes...

Is there an example somewhere that includes the implementations of all those components in the fluent check? "When_pie_is_done" or whatever - is that a method?

October 01, 2010

New Comment