development

Private Methods - test, or no test?

March 28, 2003 15:23:32.216

The Fishbowl writes;

Peter Ghali asks about testing private methods:

So how do people test private methods....my opinion is that you should test private methods at all. Adequate test coverage should cover these methods through testing of the public and protected methods that use the private methods. However....

I don't test private methods. Being able to quickly create, move around, and change the functionality of private methods is vital to remaining agile while developing.

There is a significant cost involved in changing the behaviour of your public interface: you have to make sure each caller can cope with the change. Sometimes you even have to write new tests for each caller because you have introduced some new edge-case that wasn't there before. Private methods, on the other hand, can always be tested by running the tests for the enclosing class. If you've introduced a new edge-case, you only ever have to write new tests for that one class. It's all self-contained.

This sounds reasonable. The only problem I have with it is that too often, the public interface for something is either under-specified or simply inadequate. I think the best you can do is call this a goal, not an iron clad law of development...

 Share Tweet This
-->