isA function?

Peter Hansen peter at engcorp.com
Mon Jul 15 00:15:33 EDT 2002


Roy Smith wrote:
> 
> "Delaney, Timothy" <tdelaney at avaya.com> wrote:
> > What would be more useful though is to simply try to use the functionality.
> > If it works, why is there a need for it to be a particular type? If an
> > attribute is missing, the unit test will fail. If the semantics are wrong,
> > and it passes, the unit test needs an additional case added.
> 
> But, what you're arguing is that the API is defined wrong, not that the
> unit test is written wrong.  If the API says, "foo() returns an object
> of class bar", then it's the right thing for the unit test to determine
> if, indeed, foo() returns an object of class bar.
> 
> Now, if you want to tell me that I should change the definition of foo()
> in the API to be, "returns something which has these three methods and
> these seven data items", well, OK, that's might be a point worth
> arguing.  But, it's not the job of the unit test writer to decide that
> the API isn't designed right.

Also, since some rely on the unit tests to tell them when code
is broken, if such a test were written "looser" so that it allowed
*any* such object (which supported those three methods) then
it would be possible that code breakage might not be caught
by the unit tests.

For example, a unit test for a factory function might be checking
that instances of several different classes were being returned
depending on the parameters passed in.

If the code were broken so that only a single class was represented
in the returned objects, Timothy's test would continue to pass
(bad!) while yours would fail - just as you'd want - alerting you
to the bug.

Unit testing is *not* normal coding: sometimes you want to do things
in unit testing which you would consider very bad style in regular
code, and yet it's exactly the proper way to proceed in the tests.
This is one reason I find Python *so* effective for test-driven 
development: it easily lets you bend the rules when you want to,
and in unit testing you tend to want to more often than normal.

-Peter



More information about the Python-list mailing list