Testing private methods - Shortcuts will cut you

Anders dissects the testing features of Team System, and seems quite pleased with what he finds.

The subject of testing private methods became quite a hot potatoe at James Newkirks BoF at TechEd this year. For me the whole point of writing tests is to verify the behaviour of the public interface of your class. Private methods are simply a implementation detail. If you're applying TDD, testing private methods is simply meaningless and would create unnecissary restrictions on how to implement your class. Say you've implemented a class based on a well defined interface, but then your underlying structure changes and you'd want to refactor your class. If only the public interface was tested, you could perform refactoring, and run the existing tests to assure that the interface was not broken.

Testing private methods is bad, and this feature adds itself to the list of shortcuts Microsoft seems to like to put into Visual Studio. Experienced programmers know to avoid using these features, but then you've got that bunch who just like to make use of these shortcuts. At some point in time, a good programmer is going to do maintenance work on this code and curse the cuts made by these features, and Microsoft who allowed the shortcuts to RTM.

They should rather prioritize features that promote good design, like contract-first development.

No Comments