unittest and private methods

Ben Finney bignose-hates-spam at and-benfinney-does-too.id.au
Tue Feb 3 07:47:26 CET 2004

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

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?

 \        "Laurie got offended that I used the word 'puke.' But to me, |
  `\              that's what her dinner tasted like."  -- Jack Handey |
_o__)                                                                  |
Ben Finney <http://bignose.squidly.org/>

More information about the Python-list mailing list