Qualities of a Good Unit Test
Many developers, including me sorry to say, treat unit test code as a second class citizen. After all, NUnit test fixture classes aren't going into production, so why should you put a lot of time, effort, and thought into them? An unfortunate reality is that unit tests are rarely "write-only" code. Badly written unit tests or interdependent unit tests hamper refactoring or adding new code to the system. I've heard of teams that were afraid to add new code to a system because the changes would introduce test failures. One of the advantages of automated unit testing should be the ability to safely modify existing code. Bad unit tests turn this advantage on its head.
I'm giving an internal presentation today on mock objects and I prepared a slide on the "Qualities of a Good Unit Test." I actually couldn't find much in the way of resources on the web for this, so I made up the list below. I'd love to hear from the rest of you what your best practices are for unit tests.
Unit Tests Should Be Atomic
A unit test should only test a small piece of functionality. This falls inline with the idea of getting into a rhythm of "write a little test, write a little code." A litmus test is to ask yourself if any part of the unit test could stand alone in a separate unit test. Another feedback loop for your unit testing quality is the amount of time you spend with the debugger. If your unit tests are coarse, a test failure is more difficult to find. If the unit test exercises a small amount of code, the test failure cause can usually be spotted very quickly.
Order Independent and Isolated
There should never be an order dependency, intentional or not, between unit tests. Problems arise when one unit test leaves some kind of dirty data laying around. Testing against a database or some sort of static cache is a common culprit. The theme of the day was mocking (actually Episode III), so put evil stateful things behind nice, deterministic mocks or stubs.
Intention Revealing
A unit test can, and should, be a valuable form of documentation. At best, a unit test should explain the intended usage and function of a class. At worst, the unit test should still be easy to debug in the case of a regression failure. Excessive data setup can obfuscate a unit test beyond any hope of comprehension.
Easy to Setup
How easily can I set up an environment to run the tests? If the answer is "not very," I might never get the tests running. As much as possible, decouple unit tests from external dependencies like databases and web services by using mocks or stubs. When you do test against an external dependency, the dependency better be setup correctly by the automated build.
Runs Fast
You're never going to run the test just once. For the sake of constant feedback, successful continuous integration, and your sanity, make sure your tests run quickly. Thirty minute build and unit test cycles are nothing but friction. Just to keep in the mock rut, mock things that make network calls to speed up your testing. I like to segregate connected tests into a seperate assembly. This way you can quickly run the majority of your unit tests on demand without the lag from network calls.
There's surely some obvious things I'm missing, but I'm short on sleep (and a midnight movie awaits) at the moment so that's all I've got.

91 Comments:
I would go further and define a unit test. Then define an integration test. Then define an acceptance test. A unit test doesn't care about code outside of the component. External dependencies are mocked so that a unit test will only exercise a single componet. If a test crosses component boundaries (logical layers, network, database, etc), then it is not a unit test and is very hard to be run over and over in the build process. If the test calls across component boundaries, it is an integration test and requires environment setup to be run. An acceptance test exercises an entire feature from entry point to the deepest point in the call stack. I think those distinctions would help.
Nice site!
[url=http://rxzgqjng.com/esvd/pdep.html]My homepage[/url] | [url=http://rdamksvz.com/qiht/gpat.html]Cool site[/url]
Great work!
My homepage | Please visit
Good design!
http://rxzgqjng.com/esvd/pdep.html | http://ptnncgbj.com/txfi/pdgn.html
What about user interface ?
Can we talk about Dialog Unit Test ?
Great article! Thanks.
Thanks for interesting article.
Nice! Nice site! Good resources here. I will bookmark!
I see first time your site guys. I like you :)
Excellent website. Good work. Very useful. I will bookmark!
UmbzAi You have a talant! Write more!
Bu9Izp Hello all!
3OLaCE The best blog you have!
bPpWkk Magnific!
Good job!
Good job!
Magnific!
Please write anything else!
Hello all!
Please write anything else!
Thanks to author.
Please write anything else!
Wonderful blog.
J7nhka write more, thanks.
actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.
Nice Article.
Good job!
Good job!
Hello all!
Wonderful blog.
actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.
Thanks to author.
Nice Article.
Magnific!
Wonderful blog.
Please write anything else!
Thanks to author.
What is a free gift ? Aren't all gifts free?
Build a watch in 179 easy steps - by C. Forsberg.
Hello all!
All generalizations are false, including this one.
Oops. My brain just hit a bad sector.
Give me ambiguity or give me something else.
Clap on! , Clap off! clap@#&$NO CARRIER
Change is inevitable, except from a vending machine.
Energizer Bunny Arrested! Charged with battery.
Give me ambiguity or give me something else.
What is a free gift ? Aren't all gifts free?
What is a free gift ? Aren't all gifts free?
Save the whales, collect the whole set
Nice Article.
When there's a will, I want to be in it.
A flashlight is a case for holding dead batteries.
Energizer Bunny Arrested! Charged with battery.
Friends help you move. Real friends help you move bodies
Clap on! , Clap off! clap@#&$NO CARRIER
Give me ambiguity or give me something else.
What is a free gift ? Aren't all gifts free?
Hello all!
Energizer Bunny Arrested! Charged with battery.
When there's a will, I want to be in it.
The gene pool could use a little chlorine.
Magnific!
Please write anything else!
A lot of people mistake a short memory for a clear conscience.
Friends help you move. Real friends help you move bodies.
A flashlight is a case for holding dead batteries.
Good job!
Oops. My brain just hit a bad sector.
Change is inevitable, except from a vending machine.
Good job!
Good job!
Please write anything else!
Oops. My brain just hit a bad sector.
Beam me aboard, Scotty..... Sure. Will a 2x10 do?
All generalizations are false, including this one.
Build a watch in 179 easy steps - by C. Forsberg.
Save the whales, collect the whole set
If ignorance is bliss, you must be orgasmic.
If ignorance is bliss, you must be orgasmic.
Friends help you move. Real friends help you move bodies.
Magnific!
Lottery: A tax on people who are bad at math.
What is a free gift ? Aren't all gifts free?
Good job!
What is a free gift ? Aren't all gifts free?
Suicidal twin kills sister by mistake!
Wonderful blog.
Please write anything else!
All generalizations are false, including this one.
Thanks to author.
Post a Comment
<< Home