My Adventures in Coding

March 5, 2008

Unit Testing Private Methods?

Filed under: Testing — Brian @ 8:05 pm
Tags: ,

There was a section of a lecture I attended by Robert C. Martin devoted to unit testing and the topic of writing unit tests for private methods was mentioned. So the question was “Should you write unit tests for private methods” and the answer was NO.

A class has an interface of public methods. Those public methods use private methods. You write test cases for the public methods to ensure all functionality the class provides is covered by tests. If you need to write unit tests specifically for the private methods, then your test cases for the public methods are probably not covering all cases, and if they do, then the tests for the private methods are just duplicates of the public method test cases. One of the main problems with unit tests on private methods is that they make it difficult to refactor the inner workings of a class.

A developer should have the freedom to refactor the code within a class without needing to re-write existing unit tests. We should be working at removing obstacles to refactoring and creating meaningful interfaces to our code that are both straight forward for development and testing. If you absolutely feel a private method in a class is complicated enough that it warrants being unit tested on it’s own, then perhaps that is functionality that should be broken out into it’s own class.

The public methods of a class are an interface. One job of unit tests is to explain the correct usage of a class to the next developer who uses it. Trying to unit test the private methods of a class will just make it more difficult for other developers to understand.



  1. How about static methods and final class ?

    Comment by — October 13, 2009 @ 9:58 am | Reply

    • Hi,

      Whether the method is an instance method or static method does not make a difference. The main point is the visibility of the method, whether it is Public or Private. A public method is part of the interface through which all other classes interact with that class. If you feel the need to unit test a private method, then either you are missing test cases around the public methods from which that private method is being used or you are writing tests for logic already covered by your public method tests.

      Besides just testing, another purpose of unit tests is to help describe to another developer the intended usage of a class. If all tests are around only the public methods, this is much more clear than if these tests are a mixture of tests around public and private methods with public methods that are not always fully tested. Also, often we want to refactor the code inside a class, but not the interface. When doing this it is a great deal easier and safer if the tests are around the public methods of the class so you can refactor the code inside the class without having to move tests in the process and also worry if there is proper test coverage around all public methods.

      Thanks for the comment!

      Comment by Brian — October 17, 2009 @ 5:54 pm | Reply

  2. […] In order to verify my understanding, I did a quick Google search and found that Brian, a software developer, had mentioned the same in his blog. […]

    Pingback by Should You Unit Test Private Methods Of A Class? - Maria Daniel Deepak — July 21, 2016 @ 11:11 pm | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at

%d bloggers like this: