Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   verify the return value of a function (http://www.velocityreviews.com/forums/t808213-verify-the-return-value-of-a-function.html)

Jabba Laci 01-19-2012 08:45 PM

verify the return value of a function
 
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

Ulrich Eckhardt 01-20-2012 09:15 AM

Re: verify the return value of a function
 
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
constructed.

Anyhow:

>>> x = 1
>>> type(x)

<type 'int'>
>>> type(x) is int

True

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
> discouraged.


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.

Uli

Roy Smith 01-20-2012 02:24 PM

Re: verify the return value of a function
 
In article <mailman.4872.1327005963.27778.python-list@python.org>,
Jabba Laci <jabba.laci@gmail.com> 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?


jar = my_function_being_tested()
self.assertIsInstance(jar, cookielib.LWPCookieJar)

That works in 2.7. If you're using something older than 2.7, you'll
need to do:

self.assertTrue(isinstance(jar, cookielib.LWPCookieJar)

Alternatively, just download the 2.7 version of unittest and use that
(it works fine with 2.6, not sure about earlier than that).

> 3) isinstance(return_value, cookielib.LWPCookieJar) seems to be the
> best way, however somewhere I read that using isinstance is
> discouraged


Where did you read that, and in what context?

Compared to type(), isinstance() is an improvement because it correctly
handles subclasses. If you want a LWPCookieJar, you should be happy to
have somebody give you a subclass of LWPCookieJar (assuming they
correctly implemented the interface). Thus says the Church of Most
Corpulent Staticness and Type Bondage.

On the other hand, there are some (adherents of the Most Holy and
Loquacious Church of Duck Typing) who would say that testing for class
at all is a sin, and what you want to do is test that the object being
tested has the methods and attributes you expect.

Me, I'm somewhere in between. I believe that pinching it and seeing
what the quack sounds like is usually the right thing to do. On the
other hand, if you want to demand to see its Certificate of Duckiness,
you have a right to do that too.


All times are GMT. The time now is 04:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57