Wednesday, May 18, 2005

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.

101 Comments:

Anonymous Jeffrey Palermo said...

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.

3:45 PM, May 18, 2005  
Anonymous Anonymous said...

Nice site!
[url=http://rxzgqjng.com/esvd/pdep.html]My homepage[/url] | [url=http://rdamksvz.com/qiht/gpat.html]Cool site[/url]

11:39 PM, September 20, 2006  
Anonymous Anonymous said...

Great work!
My homepage | Please visit

11:43 PM, September 20, 2006  
Anonymous Anonymous said...

Good design!
http://rxzgqjng.com/esvd/pdep.html | http://ptnncgbj.com/txfi/pdgn.html

11:43 PM, September 20, 2006  
Anonymous Frederic Torres said...

What about user interface ?
Can we talk about Dialog Unit Test ?

10:32 PM, February 20, 2007  
Anonymous Buy Levitra said...

Great article! Thanks.

6:31 PM, August 18, 2007  
Anonymous Phentermine said...

Thanks for interesting article.

11:54 PM, August 18, 2007  
Anonymous Anonimous said...

Nice! Nice site! Good resources here. I will bookmark!

6:54 PM, September 09, 2007  
Anonymous Maxwells said...

I see first time your site guys. I like you :)

4:40 AM, September 10, 2007  
Anonymous Anonimous said...

Excellent website. Good work. Very useful. I will bookmark!

4:14 PM, September 10, 2007  
Anonymous PaydayLoans said...

UmbzAi You have a talant! Write more!

11:47 AM, October 30, 2007  
Anonymous Auto insurance company said...

Bu9Izp Hello all!

9:32 PM, October 30, 2007  
Anonymous Hydrocodone said...

3OLaCE The best blog you have!

9:35 PM, November 01, 2007  
Anonymous apply for college loan said...

bPpWkk Magnific!

12:23 PM, November 02, 2007  
Anonymous almeria tour operators said...

Good job!

1:16 PM, November 02, 2007  
Anonymous buy fioricet online save $19.95 said...

Good job!

2:02 PM, November 02, 2007  
Anonymous 100 celebrex mg said...

Magnific!

2:58 PM, November 02, 2007  
Anonymous insurance companies that specialize in churches said...

Please write anything else!

4:02 PM, November 02, 2007  
Anonymous get phentermine without a prescription said...

Hello all!

5:34 AM, November 03, 2007  
Anonymous buy vicodin online said...

Please write anything else!

10:11 AM, November 03, 2007  
Anonymous i had withdrawal tramadol said...

Thanks to author.

11:14 AM, November 03, 2007  
Anonymous xenical stories of side effe said...

Please write anything else!

12:17 PM, November 03, 2007  
Anonymous cheap phentermine buy pharmacy online now said...

Wonderful blog.

1:05 PM, November 03, 2007  
Anonymous JohnBraun said...

J7nhka write more, thanks.

1:22 AM, November 04, 2007  
Anonymous couple sex pics said...

actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

9:36 PM, November 04, 2007  
Anonymous during pregnancy sex said...

Nice Article.

10:11 PM, November 04, 2007  
Anonymous animal toons sex said...

Good job!

12:03 AM, November 05, 2007  
Anonymous lebian sex stories said...

Good job!

12:35 AM, November 05, 2007  
Anonymous latina anal said...

Hello all!

1:12 AM, November 05, 2007  
Anonymous serena spice sex said...

Wonderful blog.

1:42 AM, November 05, 2007  
Anonymous ebony gallery sex said...

actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

2:09 AM, November 05, 2007  
Anonymous oral sex galery said...

Thanks to author.

2:43 AM, November 05, 2007  
Anonymous beast stories sex said...

Nice Article.

3:13 AM, November 05, 2007  
Anonymous bart sex milhouse said...

Magnific!

3:55 AM, November 05, 2007  
Anonymous craigslist albuquerque sex said...

Wonderful blog.

4:39 AM, November 05, 2007  
Anonymous hot sex play said...

Please write anything else!

5:12 AM, November 05, 2007  
Anonymous prostatectomy and sex said...

Thanks to author.

5:42 AM, November 05, 2007  
Anonymous male beastly sex said...

What is a free gift ? Aren't all gifts free?

6:14 AM, November 05, 2007  
Anonymous ny wife sex said...

Build a watch in 179 easy steps - by C. Forsberg.

6:53 AM, November 05, 2007  
Anonymous doctor teens sex said...

Hello all!

7:28 AM, November 05, 2007  
Anonymous free no dwonload porn movies said...

All generalizations are false, including this one.

7:54 AM, November 05, 2007  
Anonymous boy boy sex said...

Oops. My brain just hit a bad sector.

8:23 AM, November 05, 2007  
Anonymous boy boy sex said...

Give me ambiguity or give me something else.

8:59 AM, November 05, 2007  
Anonymous adult newsletter sex said...

Clap on! , Clap off! clap@#&$NO CARRIER

9:31 AM, November 05, 2007  
Anonymous islam premarital sex said...

Change is inevitable, except from a vending machine.

10:04 AM, November 05, 2007  
Anonymous hyperammonemia steroids said...

Energizer Bunny Arrested! Charged with battery.

10:35 AM, November 05, 2007  
Anonymous generic cialis pills stories said...

Give me ambiguity or give me something else.

11:05 AM, November 05, 2007  
Anonymous phenterm said...

What is a free gift ? Aren't all gifts free?

11:38 AM, November 05, 2007  
Anonymous islam premarital sex said...

What is a free gift ? Aren't all gifts free?

12:14 PM, November 05, 2007  
Anonymous belgium teen sex said...

Save the whales, collect the whole set

1:01 PM, November 05, 2007  
Anonymous geocities celebrity sex said...

Nice Article.

1:39 PM, November 05, 2007  
Anonymous asian bed sex said...

When there's a will, I want to be in it.

2:10 PM, November 05, 2007  
Anonymous amatuer phone sex said...

A flashlight is a case for holding dead batteries.

2:46 PM, November 05, 2007  
Anonymous sex blowjobs xxx said...

Energizer Bunny Arrested! Charged with battery.

3:17 PM, November 05, 2007  
Anonymous devil sex art said...

Friends help you move. Real friends help you move bodies

3:41 PM, November 05, 2007  
Anonymous old ladies sex said...

Clap on! , Clap off! clap@#&$NO CARRIER

4:08 PM, November 05, 2007  
Anonymous cocaine heroin effects said...

Give me ambiguity or give me something else.

4:40 PM, November 05, 2007  
Anonymous name said...

What is a free gift ? Aren't all gifts free?

5:08 PM, November 05, 2007  
Anonymous familt guy porn lois said...

Hello all!

5:48 PM, November 05, 2007  
Anonymous rape porn websites said...

Energizer Bunny Arrested! Charged with battery.

6:16 PM, November 05, 2007  
Anonymous adrianne curry porn said...

When there's a will, I want to be in it.

6:57 PM, November 05, 2007  
Anonymous lesbian sex videos said...

The gene pool could use a little chlorine.

7:28 PM, November 05, 2007  
Anonymous ass sex picture said...

Magnific!

7:59 PM, November 05, 2007  
Anonymous free schoolgirl porn naked nude bab said...

Please write anything else!

8:30 PM, November 05, 2007  
Anonymous teen porn fetish said...

A lot of people mistake a short memory for a clear conscience.

9:06 PM, November 05, 2007  
Anonymous girl fucked by dog free preview beastal said...

Friends help you move. Real friends help you move bodies.

9:52 PM, November 05, 2007  
Anonymous anal sex women forum hood said...

A flashlight is a case for holding dead batteries.

10:29 PM, November 05, 2007  
Anonymous free turkish sex said...

Good job!

11:04 PM, November 05, 2007  
Anonymous porn norway said...

Oops. My brain just hit a bad sector.

11:46 PM, November 05, 2007  
Anonymous hector dog sex said...

Change is inevitable, except from a vending machine.

12:15 AM, November 06, 2007  
Anonymous illegal us said...

Good job!

12:51 AM, November 06, 2007  
Anonymous chabert lacey sex said...

Good job!

1:39 AM, November 06, 2007  
Anonymous pic sex transgender said...

Please write anything else!

2:16 AM, November 06, 2007  
Anonymous caleb sex tape said...

Oops. My brain just hit a bad sector.

3:06 AM, November 06, 2007  
Anonymous cannes sex club said...

Beam me aboard, Scotty..... Sure. Will a 2x10 do?

3:49 AM, November 06, 2007  
Anonymous free messy sex said...

All generalizations are false, including this one.

4:28 AM, November 06, 2007  
Anonymous how to find reliable free porn said...

Build a watch in 179 easy steps - by C. Forsberg.

5:12 AM, November 06, 2007  
Anonymous install sex swing said...

Save the whales, collect the whole set

6:09 AM, November 06, 2007  
Anonymous xxx porn passwords pics teens said...

If ignorance is bliss, you must be orgasmic.

6:58 AM, November 06, 2007  
Anonymous xxx porn passwords pics teens said...

If ignorance is bliss, you must be orgasmic.

6:59 AM, November 06, 2007  
Anonymous katies diary sex said...

Friends help you move. Real friends help you move bodies.

7:48 AM, November 06, 2007  
Anonymous predict baby sex said...

Magnific!

8:23 AM, November 06, 2007  
Anonymous ass fucking porn videos said...

Lottery: A tax on people who are bad at math.

9:02 AM, November 06, 2007  
Anonymous teennap sex fantasy said...

What is a free gift ? Aren't all gifts free?

9:41 AM, November 06, 2007  
Anonymous cell phone porn said...

Good job!

10:12 AM, November 06, 2007  
Anonymous 1oo free sex said...

What is a free gift ? Aren't all gifts free?

10:57 AM, November 06, 2007  
Anonymous passionate couples sex said...

Suicidal twin kills sister by mistake!

11:42 AM, November 06, 2007  
Anonymous medlineplus drug information phentermine said...

Wonderful blog.

12:20 PM, November 06, 2007  
Anonymous buy herbal phentermine cheap prices said...

Please write anything else!

1:08 PM, November 06, 2007  
Anonymous phentermine overnight no prescription said...

All generalizations are false, including this one.

2:00 PM, November 06, 2007  
Anonymous il sex offender said...

Thanks to author.

2:37 PM, November 06, 2007  
Anonymous Anonymous said...

ebook high performance sql server dba http://audiobookscollection.co.uk/it/Scienze-Ambientali/c1778/?page=5 sell ebook marketing [url=http://audiobookscollection.co.uk/es/Robin-Walshaw/m110138/]clickbank ebook clickbank health[/url] ez ebook templates package 6

1:51 AM, February 09, 2013  
Anonymous Anonymous said...

the premature ejaculation manual ebook http://audiobookscollection.co.uk/Takashi-Washio/m122837/ scott turow ebook pdf [url=http://audiobookscollection.co.uk/es/Computational-Biochemistry-and-Biophysics/p200891/]la vita nuova ebook[/url] zahn ebook

9:06 PM, February 12, 2013  
Anonymous Anonymous said...

free ebook downlaod theories of personality http://audiobookscollection.co.uk/it/Lone-Gammeltoft/m77439/ freda ebook reader [url=http://audiobookscollection.co.uk/it/Rotational-spectroscopy-of-diatomic-molecules/p100056/]ebook java[/url] deborah christian ebook

2:37 PM, February 14, 2013  
Anonymous Anonymous said...

the skiff ebook http://audiobooksplanet.co.uk/Writing-on-Drawing-Essays-on-Drawing-Practice-and-Research/p171379/ we russian novel 1921 ebook free [url=http://audiobooksplanet.co.uk/Wachter-A/m130582/]sprzedaz ebook[/url] engineering problem solving with c ebook

3:02 PM, February 16, 2013  
Anonymous Anonymous said...

geographic information systems software http://buyoemsoftware.co.uk/fr/category-100-102/Affaires?page=2 suunto software download [url=http://buyoemsoftware.co.uk/fr/product-16754/TextSoap-5-7-Mac]simple machinr message board software[/url] bit torremt best software
[url=http://buyoemsoftware.co.uk/fr/product-33767/Autodesk-AutoCAD-2012-x64]Autodesk AutoCAD 2012 x64 - Cheap Legal OEM Software, Software Sale, Download OEM[/url] what can filemaker software do
[url=http://buyoemsoftware.co.uk/fr/product-19983/REGLabs-DVision-3-1][img]http://buyoem.co.uk/image/7.gif[/img][/url]

1:30 PM, February 28, 2013  
Anonymous Anonymous said...

sgh-a877 software http://buyoemsoftware.co.uk/it/category-200-214/Altri gang up software [url=http://buyoemsoftware.co.uk/fr/category-100-114/Autres?page=3]pantech installtion software[/url] pathlore software corporation
[url=http://buyoemsoftware.co.uk/category-100-108/Office-Tools?page=3]Office Tools - Software Store[/url] windows mobile with zune software
[url=http://buyoemsoftware.co.uk/product-12172/Adobe-Creative-Suite-3-Web-Premium-Mac][img]http://buyoem.co.uk/image/5.gif[/img][/url]

3:54 PM, March 02, 2013  
Anonymous Anonymous said...

accumax software http://buysoftwareonline.co.uk/product-34190/Unified-Color-32-Float-1-0-for-Adobe-Photoshop-CS3-x64 technical charting software comparison [url=http://buysoftwareonline.co.uk/fr/product-37188/ActiveState-Tcl-Dev-Kit-4-1-for-Windows]palm os email software[/url] syntrillium software
[url=http://buysoftwareonline.co.uk/product-35833/HiTek-Software-AutoKrypt-9-0]HiTek Software AutoKrypt 9.0 - Software Store[/url] shrink sleeves design software
[url=http://buysoftwareonline.co.uk/it/product-37209/MetaProducts-Flash-and-Media-Capture-1-4][img]http://buyoem.co.uk/image/2.gif[/img][/url]

11:13 PM, March 03, 2013  
Anonymous Anonymous said...

[url=http://certifiedpharmacy.co.uk/products/revia.htm][img]http://onlinemedistore.com/4.jpg[/img][/url]
printable target pharmacy coupons http://certifiedpharmacy.co.uk/products/micardis.htm rite aid pharmacy orem utah [url=http://certifiedpharmacy.co.uk/categories/female-enhancement.htm]cost co pharmacy[/url]
walmary pharmacy discount prescriptions http://certifiedpharmacy.co.uk/products/cardizem.htm discount pharmacy prilosec [url=http://certifiedpharmacy.co.uk/products/sustiva.htm]sustiva[/url]
automated pharmacy technology http://certifiedpharmacy.co.uk/products/diovan.htm online pharmacy viagra cialis [url=http://certifiedpharmacy.co.uk/products/yasmin.htm]lake brandt pharmacy north carolina[/url]
find location for pharmacy technician certification test http://certifiedpharmacy.co.uk/products/exelon.htm online pharmacy rx [url=http://certifiedpharmacy.co.uk/categories/pain-relief.htm]pain relief[/url]

10:04 AM, April 06, 2013  
Anonymous Anonymous said...

[url=http://certifiedpharmacy.co.uk/products/remeron.htm][img]http://onlinemedistore.com/12.jpg[/img][/url]
wal mart pharmacy move prescriptions http://certifiedpharmacy.co.uk/products/remeron.htm foremost clinical pharmacy services [url=http://certifiedpharmacy.co.uk/products/isoptin.htm]elmwood pharmacy canada[/url]
weis pharmacy http://certifiedpharmacy.co.uk/products/kamasutra-intensity-condoms.htm phentermine lighthouse pharmacy [url=http://certifiedpharmacy.co.uk/products/nolvadex.htm]nolvadex[/url]
top 10 pharmacy school http://certifiedpharmacy.co.uk/products/suhagra.htm medicare pharmacy insurances [url=http://certifiedpharmacy.co.uk/products/cok-n--energy-and-sensory-enhancer-.htm]lanes pharmacy[/url]
pharmacy online uk http://certifiedpharmacy.co.uk/products/metformin.htm getting started in pharmacy compounding [url=http://certifiedpharmacy.co.uk/products/cipro.htm]cipro[/url]

3:12 PM, April 07, 2013  
Anonymous Anonymous said...

[url=http://englandpharmacy.co.uk/products/karela.htm][img]http://onlinemedistore.com/10.jpg[/img][/url]
rite aid pharmacy orem utah http://englandpharmacy.co.uk/products/apcalis-sx.htm pharmacy technician jobs in texas [url=http://englandpharmacy.co.uk/categories/anti-fungus.htm]eckerts pharmacy[/url]
liza yuen pharmacy san francisco http://englandpharmacy.co.uk/products/kamasutra-intensity-condoms.htm florida atlantic school of pharmacy [url=http://englandpharmacy.co.uk/products/allopurinol.htm]allopurinol[/url]
pharmacy phd http://englandpharmacy.co.uk/products/zebeta.htm nexium pharmacy [url=http://englandpharmacy.co.uk/products/ampicillin.htm]pharmacy continuing education course in hawaii[/url]
training for pharmacy technician http://englandpharmacy.co.uk/catalogue/m.htm bigalow pharmacy [url=http://englandpharmacy.co.uk/products/female-viagra.htm]female viagra[/url]

5:18 AM, April 13, 2013  

Post a Comment

<< Home