A Portion of Buff

Everybody else had one, so...

How big are your privates?

There has recently been, and will continue to be, increased noise about Team System and its assorted components.  The most widely discussed seems to be the unit testing piece, which is encouraging on one level because it means people are interested in unit testing.  I have yet to see anyone ask the question "why?", however, as we already have several perfectly good testing frameworks (one of which VSTS blatently clones), with perfectly good (better than VSTS, even) integration with Visual Studio.

Lacking, also, is much contrarian opinion on the "Create Tests" feature in VSTS.  I'll state my bias up-front - I think it's a Bad Thing - but I think if Microsoft are going to offer the feature, they should also provide guidance on how they think it should be used.  I have several objections to this feature.  First, it assumes that there is a one-to-one mapping between methods and tests (even down to the semi-configurable test-name generation).  Second, it promotes a test-last culture.  Third, it makes it super-easy to test private methods.

I'll expand on those points in reverse.  Whether to test private methods is not one of those issues which has an easy, widely-accepted answer, but there are other significant questions which Microsoft should address before jumping off the fence:

* Is unit-testing about testing features or implementation?

* Should every piece of code within a class be testable via its public interface?

* Does coupling your tests to private implementation hinder or facilitate refactoring?

* Does the implementation of the xxxAccessor objects hinder or facilitate refactoring?

* Are private methods a code smell?

* Do framework developers have different API concerns to closed-audience developers?

I think providing an easy way to test private methods solves the wrong problem.  Before reaching for the reflection, ask yourself why you can't test that method via the public methods which use it, and consider changing your class-under-test to be more easily testable.

The second point I consider the most important.  Visual Studio (even 2005) already offers very little to the test driven developer, and I see "Create Tests" as a backward step.  How about a "Create Code" feature instead?  Whidbey offers "Generate Method Stub".  Would it kill them to add Generate Class, Interface, Field, Local?  And the expansions don't count - go and look at IntelliJ, or Eclipse, or even ReSharper.  By making it so hard to drive development from your unit tests, it's no wonder that some developers view TDD, and programming by intention in general, with suspicion.  Much easier to hack away inside a class and add tests later, that way the "flow" isn't interrupted with the constant flipping between unit tests, code-under-test, and the Solution Explorer.  I was at a developer lab for VSTS recently and, I'm not kidding, the presenter offered Generate Method Stub as Visual Studio's support for TDD.  He even dedicated a slide to it.  I work with guys who would have laughed out loud at that.

Finally, the idea that each method on a class only needs one test method (one called 'SomeMethodTest' at that) should seem weird to any unit-tester, even those not doing TDD.  What are you meant to do with the generated test methods?  Put all your assertions about each method in one test?  Add more tests as you go, thus instantly screwing up the naming conventions?  What about tests which involve more than one method on the same class?  What if you want to name your test methods after the behaviour they test, rather than the methods they exercise? 

It just hasn't been thought through.  If it had, they might have realised that providing lower level features to support code generation from the editor would have provided a far more flexible, and less dangerous, solution.

Comments

TrackBack said:

# September 16, 2004 3:51 AM

TrackBack said:

# September 16, 2004 10:53 PM

Owen Rogers said:

great post jim. i couldn't agree with you more.
# September 17, 2004 5:38 AM

How big are your privates? said:

Pingback from  How big are your privates?

# November 28, 2007 1:01 AM

PorterChristie25 said:

The articles about this topic are offered by the term paper writing service thus, some people will <a href="http://www.gogetessays.com">buy custom essay papers</a> or term paper about it.

# December 9, 2010 7:55 PM

essays online said:

I want to tell that I have great experience of outline essays purchasing. I tell that the essay writing services will be a good helper in the papers completing case.  

# January 6, 2011 8:12 PM

Home Security Monitoring systems said:

Nice blog here! Also your site loads up very fast! What web host are you using' Can I get your affiliate link to your host' I wish my site loaded up as quickly as yours lol

<b><a href="supremearticle.com/.../ideas-for-choosing-a-home-security-camera.html

">Home Security Monitoring

<a/><b/>

# April 14, 2011 12:30 PM

Best forum hosting said:

Really this is great think and good information about this article..you have described everything is very clearly..i got a lot from this article

<a href="www.hosting-forum.in/" rel="dofollow">best forum hosting</a>

# December 12, 2011 5:03 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)