Re: Why does python not have a mechanism for data hiding?



On Jun 4, 4:29 am, "Russ P." <Russ.Paie...@xxxxxxxxx> wrote:
If you think that private data and methods should not be allowed
because they complicate unit testing, then I suggest you take a look
at how unit testing is done is C++, Java, and Ada. They seem to do
just fine.

Nice to put the burden of evidence back onto everyone else, but doing
a bit of searching I found the following "answers" to the question of
unit-testing private functions & methods:

I suggest that tests should be written only for the public methods.

You can use a debugger, probably Carbide. That way you can see
all the variables. Otherwise, write the values to a log or EMCT.
You can make the logging only happen for debug builds if you don't
want the logging in the production code. If you really need to
see the private variables from your code, declare them public in
debug builds.

Problem is testing private functions. Some can be fixed by
promoting private to protected, inheriting the class adding
testing in the class. Others get refactored out the classes
they reside in and get put into their own functor classes[...]

So the basic answers I'm seeing that "do just fine" are:

1. Don't test private functions.
2. Add functionality _to_ the private functions for testing.
3. Change the interface for the purpose of testing.

All of which seem exceptionally inefficient and run counter to the
whole purpose of unit testing.

But I think there is a more fundamental issue here. You complain about
problems with software that uses data encapsulation. So two
possibilities exist here: either the designers of the code were not
smart enough to understand what data or methods the client would need,
or the client is not smart enough to understand what they need. Maybe
the solution is smarter programmers and clients rather than a dumber
language.

This is the most ludicrous argument I've ever heard. Of _course_ we
can't predict every possible usage of our code that others might want
it for. If someone can easily extend code that I've written to improve
or increase its functionality, why would I want to prevent them from
doing so?

Then again, I tend to think of other programmers as "peers" rather
than clients. YMMV.
.



Relevant Pages

  • Re: OO Unit Test Question
    ... [Stubbing B::doB in the test harness to return an appropriate value for the test case is just a form of self-delusion; one is just testing the test harness, not the contract between A and its client. ... When unit testing A::doA, all one has to demonstrate is that B::doB was called with the correct 'x'. ... Pathfinder Solutions -- Put MDA to Work ...
    (comp.object)
  • Re: When static typing is worth it
    ... I wrote 50kLOC of custom object marshalling code in OCaml and the ... client insisted on unit testing it. ... caught many bugs and 24hrs of running unit tests caught precisely zero ... It gives you (or your client) a warm and fuzzy feeling when the ...
    (comp.lang.lisp)
  • Re: Binary message creation in BizTalk Server
    ... In my CreateMessage method, I need to pass a XLangMessage variable. ... How will I pass this and do a unit testing with a client application? ... I could not find much info in MSDN also ...
    (microsoft.public.biztalk.general)