unittest and private methods

Y

Yin

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.

Thanks,
Yin
 
B

Ben Finney

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?
 
J

John Roth

Yin said:
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
 
Y

Yin

Ben Finney said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top