unittest and private methods

Yin yin_12180 at yahoo.com
Tue Feb 3 11:18:27 EST 2004


Ben Finney <bignose-hates-spam at and-benfinney-does-too.id.au> wrote in message news:<slrnc1ui5e.151.bignose-hates-spam at rose.localdomain.fake>...
> On 2 Feb 2004 21:58:13 -0800, Yin wrote:
> > 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.
> 
> If a method is private, why is it being tested from outside?  Unit
> testing is supposed to test that the module *behaves* as expected.  If a
> method is private, it's not part of the external behaviour of the
> module.
> 
> Possibly you want to re-think what it is you're testing.  Write a public
> method that *does something visible*, and test that.  If you find that
> there's nothing the private method is doing that you want to test from
> outside, then why are you trying to test it?

That's a good point. The primary reason is that I'd like to be able to
pinpoint errors quicker through unittesting.  If I just provided a
call to an exposed function, and an error occurred, it would still
take some work to go in the class and pinpoint where exactly the
problem is.  By writing some unittests for the class internals, I may
save myself some time in the future.

Thanks for your thoughts!
Yin



More information about the Python-list mailing list