unittest and private methods

John Roth newsgroups at jhrothjr.com
Tue Feb 3 06:26:08 EST 2004


"Yin" <yin_12180 at yahoo.com> wrote in message
news:88caaeda.0402022158.3a7923ac at posting.google.com...
> I am an avid user of unittest and write tests for everything. Every so
> often, I write a class that has some private methods that I only want
> to be called from within the class. I usually write unittests for
> these private methods using the mangled form of the call from a test
> script file. It's not very elegant, and I wanted to get some feedback
> on how others write unit tests for private methods.
>
> I suppose that I could just not use private methods, and have them all
> be public, but it's a lot cleaner for me to hide all the other
> extraneous methods rather than expose them to the public.

Technically, the __ syntax that does name mangling is supposed
to be for private methods, and the _ syntax is supposed to be for
protected methods and attributes. However, I find that the
distinction is not worth all that much, and use the single underscore
for both. Possibly that's because I don't use subclassing all that
much anyway so I don't find 'protected' to be a useful distinction.

John Roth
>
> Thanks,
> Yin





More information about the Python-list mailing list