NOthingyoumissed

George Mauer is on the Net

Re: What is Unit Testing?

In a post last month Zachariah Young posts that 

… unit tests … verify that the business logic is correct.  I believe that for it to be unit testing it has to use a testing framework like nunit or junit.

I say thar be dragons!  Tying your understanding of unit testing to a specific tool allows you to neatly sidestep thinking about whatThar be dragons  testing actually does.  Additionally, you deny credit to an excellent tool like NUnit by pigeonholing its purpose.

Consider for a minute what NUnit actually is.  Yes their own website claims that they are specifically a “unit testing framework for all” but what does it actually do?  In my opinion it is a framework for setting up an application’s state, executing code, and then asserting that the resulting state is as you expected. That’s pretty broad.  Way broader than the confines of mere unit testing.  

Take the excellent WatiN or (no longer maintained) NUnitASP tools.  Both can be run using NUnit but neither is used to build a true unit test.  WatiN for example remote controls your browser to investigate how your page will react to certain stiumuli.  What unit does WatiN test?  Your Page_Load method?  Your page renderer?  Whether the viewstate persists that you had entered some text halfway up the page before submitting and the text is not cleared when your page returns with a validation error?  The localhost routing mechanism on your PC?  Or does it test the integration of all of these?

I made the same exact mistake myself when I decided to ‘see what this TDD thing’ is all about last April.  I ultimately had to delete my entire testing project after I came to the realization that each test bit off far too large a chunk making it utterly impossible to maintain once true refactoring started.  I nearly cried that day.

But out of the ashes I was reborn with what I hope (but don’t really expect) to be the final zen-like understanding of this unit testing thing.  The first aha moment was when I finally caved in and decided to learn Rhino Mocks and my understanding sharpened when I caved again and looked into the new Rhino Mocks 3.5 AAA syntax.  Overall the lesson seems to be that I’m always wrong and I will eventually give in on everything and like it.  My girlfriend will be happy to hear that.

Let me tell you how I understand unit testing now.  Imagine a room.  You are standing on the outside and can comunicate with its ouccupants through a two way loudspeaker.  This room is your unit – if it helps you can imagine the room as painted black.  Now in testing you present your unit with a problem:

Given that you are on 735 Bourbon St New Orleans, Louisiana how far is it to my grandmother’s house?

I used to think that unit testing involved merely waiting for the answer:

Your grandmother’s house is 1345 miles away and you haven’t visited in months you shmuck.

Now I realize its more about everything else that is said.  “Where does your grandmother live?” and “Can you be so kind as to  slip an atlas under the door?” should definitely be questions that you hear over that loudspeaker.  If you’ve separated your resposibilities properly the unit will also require a calculator that given two points on a map can tell you the distance (the specific calculator implementation should depend on whether you intend to fly or use the highway system).  The unit might even request that you hang a EnRouteToGrandmas = true sign on the door.  

The point is that you’re not so concerned with the final answer as much as you are with the unit asking the right questions.  Afterall, without that information know that it could not possibly be doing what you mean for it to do!

And so I define unit testing as

Verifying that given an input, a piece of code makes all the external requests that you would expect where the unit is small enough that the number of these can be enumerated with ease.

Note that this definition does not necessitate automated testing and this is something that I again think is basically correct.  After all, a framework should not do anything that you cannot do for yourself.

The beautiful part is if you’re limiting the amount of requests that you’re going to expect – let’s say no more than four – single responsibility emerges almost all on its own.  After all, how much can a piece of code do if its not communicating with the external world?

So there you go, thats my understanding of unit testing.  I look forward to being proven wrong in the future and having to refactor everything all over again.  But until that time,  Zachariah, you’ve been blogo-served!

Disclaimer:  I do not know Zachariah personally but he seems like a smart guy that is highly involved in the ALT.Net community.  I do not purport to be more knowledgeable than him in programming issues, I just disagree with him on this one point.

kick it on DotNetKicks.com

January 15, 2009 Posted by | ALT.Net, Programming | , , | Leave a comment