By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

unittest and private methods

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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?

--
\ "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/>
Jul 18 '05 #2

P: n/a
"Yin" <yi*******@yahoo.com> wrote in message
news:88**************************@posting.google.c om...
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

Jul 18 '05 #3

P: n/a
Yin
Ben Finney <bi****************@and-benfinney-does-too.id.au> wrote in message news:<sl*******************************@rose.local domain.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
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.