[Tutor] unittest and private methods

lemeia lemeia at bigpond.com
Fri Jan 20 08:43:01 CET 2006

I currently have a class where I have quite a few private methods. One of these methods is a small general utility that extracts characters from provided text up until it reaches a delimiter. Something like extractTo(self, text, delimiter).

It's not a relevant interface to the class, just a utility for the class' public methods, which is why I've made it private. That seems to be a logical decision to me.

However, the public methods within the class that call it currently only do so to extract numeric events from the text. extractTo is a little more generalised than that and so I decided that I would use unittest to build test cases for the extractTo method and exercise it to its fullest in case the class requires its extended functionality in the future.

You'll probably see what my problem is. I can't exercise this method directly from an external test case as the method is not part of the class' public interface. If I call a method in the interface that currently utilises this private method I'm not testing the method as fully as I would like.

Now I can build arbitrary interfaces into my class just for testing this method but that sounds like a messy way to maintain a program. Additionally, I could make this method public, but that also seems like an unsatisfactory solution.

The example isn't so important to my question, but I wanted to give some context before asking......

Is there a way to specifically target and unit test private methods in a class via unittest without testing them through the class' public interface?

I hope I've made some sense there.


More information about the Tutor mailing list