isA function?

Roy Smith roy at panix.com
Sun Jul 14 10:03:15 EDT 2002


I wrote:
>> We're writing some unit tests, and one of the tests is "foo() returns 
>> something which isA (bar)".

"Afonso Fernandez Nogueira" <spam at aturuxo.net> wrote:
> "isinstance() considered harmful":
> http://www.mail-archive.com/kragen-tol@canonical.org/msg00007.html

A very interesting article; thanks for the URL.

Still, I think it's a reasonable thing to do in a unit test environment.  
I suppose "unit test" can mean different things to different people 
(just like the article says about "object oriented").  The way we're 
doing it, every time we say "foo() does blah", we write a test case to 
make sure foo() really does blah.  No, we're not doing XP, but this 
seems like one of the good ideas that can be adopted from XP.

In this case, we said, "foo() returns a list blahs".  It's actually the 
case that it'll always be a subclass of blah; exactly which subclass 
depends on the input.  We started out with:

assertEquals (foo.__class__.__name__, "blah_1")

then I realized that was dumb and changed it to:

assertEquals (foo.__class__, myModule.blah_1)

which is a little more pythonic, but still a special case.  It may still 
be worth writing that test case, but

assert_ (isinstance (foo, myModule.blah))

is really what I want for the unit test.



More information about the Python-list mailing list