verify the return value of a function
ulrich.eckhardt at dominolaser.com
Fri Jan 20 04:15:54 EST 2012
Am 19.01.2012 21:45, schrieb Jabba Laci:
> 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'>
I'm not sure where the problem here is and where exactly you are seeing
this. This might even indicate a problem with how the returned type is
>>> x = 1
>>> type(x) is int
So checking for an exact type should work using type().
> 2) return_value.__class__ .__name__ gives 'LWPCookieJar', which is bettter
It doesn't cover namespaces though. Also, you should compare that to
cookielib.LWPCookieJar.__name__, not 'LWPCookieJar'. What is the "LWP", btw?
> 3) isinstance(return_value, cookielib.LWPCookieJar) seems to be the
> best way, however somewhere I read that using isinstance is
Never trust any such claim that doesn't give a justification. In your
case, that would be the right thing to do, IMHO. Promising to return an
LWPCookieJar is fulfilled when the returnvalue is of that type or a
class derived from that, which variant 1 doesn't cover.
More information about the Python-list