Friday, October 27, 2006

I have started using Team System at work and I stumbled upon the built in functionality to generate a unit test for a private method. At first I thought it was sweet that you could test private methods but then after going for a walk, I have come to the conclusion that this is not a good feature.

Someone has probably complained about this before but is there a situation where you would use a private method that is not eventually called by a public or protected method that is itself testable? Shouldn't you test the public or protected method to see if your private method is called and returns what you are expecting? I am going to avoid using this functionality until I find a valid reason to use it.

Friday, October 27, 2006 6:42:55 AM (GMT Standard Time, UTC+00:00)  #    Comments [2]  | 
Friday, October 27, 2006 1:17:15 PM (GMT Standard Time, UTC+00:00)
This is something that I've thought about a lot, and you're right when you say that your private method will be tested when you call the public method. My argument against this (watch the Agilists line up to flame me now!) is that testing private methods provides you with a much more granular test that only testing public methods. This is only the case though, if your public methods have complexity to them. If all the public method does is call one private method, then you're essentially testing the same thing. If your public method calls 4 private methods, or even one private method that uses two other private methods, how many tests will you be writing to ensure good coverage of the private methods' functionality? Writing tests for permutations against one function sucks and eventually gets too complicated leading to missed testing scenarios. Testing of the private methods, however you wish to do this, allows you to write much more discrete and readable tests.

Now that I'm done, the Agilists and TDDers can say how I should refactor that public method so it's less complicated and easier to test.
Friday, October 27, 2006 3:28:34 PM (GMT Standard Time, UTC+00:00)
you will find lots of debate on this subject.
The people who are pro of testing private methods said that the benefits that TDD brings to model design on the class level, can be acheived as well on the method level. Beside gives more control, if you refactor later and something brake.
and the people who are opposing that said that TDD is for the class behavior and interaction with the outside world, so how the class is doing inside is irrelevant, beside as you mentioned, all private methods should be called by testing public methods.
Myself, I am not doing unit test for private methods, and just keep it for public
Ghassan
Comments are closed.

Theme design by Jelle Druyts

Pick a theme: