Jeremy D. Miller -- The Shade Tree Developer

Sponsors

The Lounge

Syndication

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
More testing code than production code is typical, but don't let that stop you

Ian Cooper recently wrote about the Ratio of test code to production code.  Ian is saying that he's routinely seeing a 1:1 or even 1.5:1 ratio of test code to production code.  From my experience with TDD, that's about normal.  Some kinds of code will go even higher.  For example, any kind of significant recursive functionality will make the ratio go throught the roof (a Visitor/Composite pattern combo is central to StoryTeller.  That jacks up the test code LOC relative to the real code LOC significantly).

All I want to add to Ian's post is that that number should not discourage newcomers to TDD.  All those lines of code are not created equal.  Unit test code does not take anywhere near the same amount of concentration or care as production code.  I say that mainly because unit test code is composed almost entirely of stand alone methods.  They don't interrelate or interract with each other.  The responsibility of a unit test is almost always the same:  setup data, do something, check the results.  The hardest thing about coding is deciding what to code, not the mechanical act of typing.  The hard part of writing a unit test is deciding what the real code is supposed to be doing , then codifying that specification in the unit test.  Since you'd be doing that specification work in your own head or a document anyway, all you've added is the mechanical work to type it out in a unit test method.

TDD is cost effective when and if the effort you put into writing the tests is less than the corresponding effort would have been for the manual unit testing and additional debugging that you dodge by having comprehensive unit test coverage.  Add in the hard to quantify advantage from the qualities of orthogonality that TDD all but requires (testability design is mostly about creating orthogonality between the pieces).  Plus, if you're good, time spent TDD is also time spent doing design in the small and hopefully creating a useful and human readable set of documentation about the fine grained details of the code.
 

And while I'm thinking about it, the whole "Who tests the tests" issue?

A.)  Keep your unit tests as simple, concise, and readable as possible to help catch problems by inspection

B.)  Just pay attention to what you're doing

C.)  What Phil Haack said in the link above about the code testing the tests and vice versa.  I find most unit test bugs when the test fails and the code seems perfectly right.


Posted Thu, Mar 22 2007 3:45 PM by Jeremy D. Miller

[Advertisement]

Comments

Ayende @ Rahien wrote Test Responsabilities
on Thu, Mar 22 2007 5:26 PM

Test Responsabilities

Dim Blog As New ThoughtStream(me) wrote Unit Test Lines-Of-Code to Production Lines-Of-Code ratio
on Thu, Mar 22 2007 5:37 PM

Jeremy Miller has posted Another Gold Nugget of "Intro To TDD" info, concerning the Unit Test LOC to...

DotNetKicks.com wrote More testing code than production code is typical, but fine
on Fri, Mar 23 2007 2:06 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

rams wrote re: More testing code than production code is typical, but don't let that stop you
on Fri, Mar 23 2007 7:27 AM

and remember,

D) Your software is only as good as the tests you write. Better tests, better software.

Martin Jul wrote re: More testing code than production code is typical, but don't let that stop you
on Sun, Mar 25 2007 4:58 AM

I often see twice as much unit test code as production code (this was roughly the ratio for a recent mainframe migration we did). Then add the acceptance tests on top and you have even more test code.

On the other hand, I am currently helping a new team get a handle on their quality and they have much less UT code than production code. Needless to say, the "production code" isn't quite production ready in terms of quality, so I am helping introduce more upfront, automated testing.

One interesting point here is that a lot of the ground work is to introduce Equals methods on the domain objects so that tests will be simple and short, this being a prerequisite for convincing people to actually write the tests. I have also found that creating some proper factory classes to produce semantically meaningful domain object graphs are quite a boon when it comes to producing simple tests. It is so much easier to CompanyFactory.CreateMultiNationalConglomerate() than to add hundreds of lines setup code in each test that make it hard to see the intention of the test.

Jeremy D. Miller wrote re: More testing code than production code is typical, but don't let that stop you
on Sun, Mar 25 2007 7:06 AM

To Martin's point about helper classes to set up domain object graphs -->

http://www.martinfowler.com/bliki/ObjectMother.html

ObjectMother can help bring down the mechanical cost of writing some unit tests and improve the signal to noise ratio of your unit tests.

Jay R. Wren - lazy dawg evarlast » Blog Archive » its a week for great quotes wrote Jay R. Wren - lazy dawg evarlast » Blog Archive » its a week for great quotes
on Thu, Jun 28 2007 8:06 PM

Pingback from  Jay R. Wren - lazy dawg evarlast  » Blog Archive   » its a week for great quotes

gifts wrote re: More testing code than production code is typical, but don't let that stop you
on Mon, Oct 15 2007 7:35 AM

This is probably an issue with all different types of coding / languages, and is probably not specific to a certain type. While some people may swear blindly that their preferred language results in less test code, I don't think that's entirely fair – especially since some languages are made for only basic things, are are being used as such. If you had to use such a language to do something else, you would probably run into serious problems. The secret, for me, is the right tool for the right job. In other words, code in the right language for the specific job that the language caters for. It makes perfect sense to me. The whole debate of 'which language is better' is, in my opinion, a whole lot of pointless drivel.

photo oil painting wrote re: More testing code than production code is typical, but don't let that stop you
on Mon, Mar 24 2008 4:35 AM

Thank you for sharing this information to us.  I know most coders have experienced this but only very few have put their thoughts into writing.

http://139641.subversion.codebetter.com/blogs/jeremy.miller/archive/2007/03/22/More-testing-code-than-production-code-is-typical_2C00_-but-don_2700_t-let-that-stop-you.aspx wrote http://139641.subversion.codebetter.com/blogs/jeremy.miller/archive/2007/03/22/More-testing-code-than-production-code-is-typical_2C00_-but-don_2700_t-let-that-stop-you.aspx
on Wed, Mar 26 2008 4:52 AM
Xoobie online auctions wrote re: More testing code than production code is typical, but don't let that stop you
on Mon, Jun 23 2008 9:16 AM

Three words:

testing, testing, testing

Wellbutrin sr. wrote Wellbutrin.
on Sat, Jul 12 2008 3:53 PM

Drug prices wellbutrin.

Lexapro. wrote Lexapro.
on Sun, Jul 13 2008 3:41 PM

Lexapro.

rüya tabiri wrote re: More testing code than production code is typical, but don't let that stop you
on Tue, Nov 25 2008 6:11 AM

thank You.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?