verify the return value of a function

Jean-Michel Pichavant jeanmichel at sequans.com
Fri Jan 20 05:13:59 EST 2012


Jabba Laci wrote:
> Hi,
>
> In a unit test, I want to verify that a function returns a
> cookielib.LWPCookieJar object. What is the correct way of doing that?
>
> 1) First I tried to figure out its type with type(return_value) but it
> is <type 'instance'>
>
> 2) return_value.__class__ .__name__ gives 'LWPCookieJar', which is bettter
>
> 3) isinstance(return_value, cookielib.LWPCookieJar) seems to be the
> best way, however somewhere I read that using isinstance is
> discouraged
>
> Thanks,
>
> Laszlo
>   
isinstance is fine, if you could find the source where it is 
discouraged... Could be a consequence of some specific context.
However, checking types in OOP is in general a failure. Unitary tests 
are possibly an exception.

JM



More information about the Python-list mailing list