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 31st, 2009 | 2 Comments
2 Responses to “Unit tests for private methods are a code smell” Leave your Comment
  1. Simon Harris says:

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

    Couldn’t agree more!

  2. Code Buddy says:

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

Leave a reply

We love to hear your views.