Monday, May 23, 2005

Taxonomy of Tests: Unit Tests

At the most granular level is the unit test. Unit tests are written by developers to verify, on in Test Driven Development specify, the functionality of a very small piece of code to the satisfaction of the developer. Usually a unit test works on one class or a small cluster of classes at a time. One of the key attributes of a unit test is the ability to test a small unit in isolation. We want to prove that the unit of code itself is working without corruption or interference from other classes.

Purposely designing code in order to create isolated unit tests is part of a successful Test Driven Development effort. Mocks and stubs are an invaluable tool to create the isolation. Some people insist that all external dependencies (databases, Active Directory, web services, etc.) be mocked out in unit tests. My best advice is to consciously quarantine inconvenient things behind gateways with an abstracted interface. Use Inversion of Control to minimize dependencies from the majority of your .NET code to these gateways. When you can't eliminate a dependency, use Dependency Injection to provide a mechanism to replace the real thing with a mock or a stub. I have had very positive (well, mostly) experience with the Model-View-Presenter (MVP) pattern described by Martin Fowler as a strategy to maximize the testability of user interface code.

Unfortunately, some units of code only exist to manipulate the database or interact with infrastructure services like messaging. Not to mention that you still have to unit test the UI screens. MVP is not enough in and of itself to guarantee a properly functioning user interface (trust me on this one). You can either marginalize this code by not writing tests and hope for the best (or let the acceptance tests take care of it), or bite the bullet and write a battery of tests that are connected. My advice is to put some ketchup on the bullet and just do it.

I do prefer to separate connected and non-connected unit tests into separate test fixture assemblies. I generally lean on only the non-connected tests during refactoring or new development. I do think it is absolutely necessary to run the integrated unit tests in both your check-in procedure and the automated build.

There is a school of thought that says if you really unit test well enough against the mocks, integrated testing is unnecessary. The NEO project is the best attempt I've seen anybody use to actually approach this ideal. I still think this philosophy is unattainable and possibly dangerous.

I've been in some heated arguments on the merits of mocking ADO.NET when testing custom data access classes. I mostly vote no to mocking ADO.NET. In this case, the unit of valuable work is manipulating the data in the database. Who cares if the code sets the “expected” parameter values when the real stored procedure has been changed in the database behind your back? In this case, the effort to value ratio is all wrong. Of course, when you’re a consultant onsite at the client you work according to their goofy development standards and politely say “Thank you sir, may I have another?”

Next up, Acceptance tests...


Anonymous Anonymous said...

Yay! A pragmatic perspective. No absolute religious fanaticism here. I commend you.


5:03 PM, May 23, 2005  
Blogger Jeremy D. Miller said...

Thanks for the comment Darrell. I don't play very well with XP purists anyway.

5:09 PM, May 23, 2005  
Anonymous Anonymous said...

Hey! I know this is somewhat off topic but I was wondering which
blog platform are you using for this website? I'm getting sick and tired of Wordpress because I've had issues with hackers and I'm looking at options for another platform. I would be awesome if you could point me in the direction of a good platform.

Also visit my web site -

12:28 AM, May 10, 2013  
Anonymous Anonymous said...

Hello, Neat post. There is an issue along with your web site in
web explorer, may check this? IE still is the marketplace chief and a
big part of other people will leave out your wonderful writing due to this problem.

my website:

1:17 PM, May 10, 2013  
Anonymous Anonymous said...

Thank you for sharing your info. I really appreciate your
efforts and I am waiting for your further write ups thank
you once again.

Review my weblog:

2:45 AM, May 11, 2013  
Anonymous Anonymous said...

Yes! Finally someone writes about Visit Website.

Here is my web-site :: relevant webpage

2:13 PM, May 12, 2013  
Anonymous Anonymous said...

I visited several sites except the audio quality for audio songs current
at this web site is in fact excellent.

Have a look at my web-site; marriage counselor

12:02 AM, May 19, 2013  
Anonymous Anonymous said...

whoah this weblog is magnificent i love reading your articles.
Keep up the great work! You realize, a lot of people are hunting
round for this info, you could aid them greatly.

My site - Bestventureinc.Com

10:16 AM, May 19, 2013  
Anonymous Anonymous said...

You've made some decent points there. I looked on the internet for additional information about the issue and found most individuals will go along with your views on this web site.

my homepage: pandoas box

12:15 PM, May 19, 2013  
Anonymous Anonymous said...

Hello there! I know this is kinda off topic but I was wondering which
blog platform are you using for this website?
I'm getting fed up of Wordpress because I've had issues with
hackers and I'm looking at alternatives for another platform. I would be fantastic if you could point me in the direction of a good platform.

my site

4:30 AM, May 20, 2013  
Anonymous Anonymous said...

I really like it when people come together and share views.
Great website, stick with it!

Here is my web-site ... home-page

12:32 AM, May 25, 2013  
Anonymous Anonymous said...

Amazing! This blog looks exactly like my old one! It's on a completely different subject but it has pretty much the same page layout and design. Great choice of colors!

Feel free to visit my blog post;

1:24 PM, June 07, 2013  
Anonymous Anonymous said...

Please let me know if you're looking for a article writer for your weblog. You have some really good posts and I believe I would be a good asset. If you ever want to take some of the load off, I'd
really like to write some material for your
blog in exchange for a link back to mine. Please send me an
email if interested. Thank you!

Check out my webpage:

3:50 PM, June 07, 2013  
Anonymous Anonymous said...

Greetings! Very helpful advice in this particular article!
It's the little changes that make the largest changes. Thanks a lot for sharing!

Also visit my site ...

4:57 PM, June 07, 2013  
Anonymous Anonymous said...

This article will assist the internet visitors for creating new weblog
or even a weblog from start to end.

Look into my web page: Itllhappenzine.blogspot.Com

5:57 PM, June 08, 2013  

Post a Comment

<< Home