Unit tests for private methods are a code smell

This week I attended a talk where some people were discussing techniques for unit testing private methods — they were going on about problems they had getting something called Private Accessors to work with ReSharper.

The tools they mentioned were completely foreign to me, and I wondered why I’d never heard of them. I think the reason is because I never bothered trying to test a private method before.

I take the approach that, if you have a private method worthy of having its own tests, then it is worthy of being extracted to a public method on a new class. Tests for private methods are a big smell you have an SRP violation somewhere. And they will be brittle anyway so just don’t do it!

October 31, 2009

2 Comments

Simon Harris on October 31, 2009 at 8:03 pm.

Reminds me of something I wrote back in 2004: http://www.harukizaemon.com/2004/02/don-touch-my-privates.html

Couldn’t agree more!

Code Buddy on November 2, 2009 at 7:42 pm.

Some interesting discussion on this over at stackoverflow: http://stackoverflow.com/questions/250692/how-do-you-unit-test-private-methods

Leave Your Comment

Your email will not be published or shared. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>