Unit Testing Basics: Make sure the test can fail

Though I should know better by now, I still find myself on occasion (just on occasion, really) making a ‘rookie’ mistake.

You create a spec and code it so that it will pass.  All is good.  Then in some later scenario that the spec should have covered, you find yourself getting a failure.  Consternation, angst…..after 20 minutes (if you are lucky), you discover…the spec was written such that it never could have failed.

When I first was trying to learn unit testing basics, I would get stuck on the advice, “Make sure your tests fail, then make them pass.”  Well, that was easy enough, punch in an Assert.IsTrue(1 == 2).  Obviously, that wasn’t the point.  You needed to set up your tests so that you were setting up expectations and assertions that were relevant to what was being tested.  Make *that* fail, then make it pass.

At least for me, it is still too easy to get in the flow and figure I know what a legitimate failure is, without thinking about it enough.  Doh!

So, make sure your tests fail for a legitimate reason.  That’s the ‘red’ part of red-green-refactor.

posted on Friday, August 28, 2009 7:25 PM Print
No comments posted yet.

Post Comment

Title *
Name *
Comment *  
Please add 7 and 2 and type the answer here: