Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19138 > unrolled thread

verify the return value of a function

Started byJabba Laci <jabba.laci@gmail.com>
First post2012-01-19 21:45 +0100
Last post2012-01-20 09:24 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  verify the return value of a function Jabba Laci <jabba.laci@gmail.com> - 2012-01-19 21:45 +0100
    Re: verify the return value of a function Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-01-20 10:15 +0100
    Re: verify the return value of a function Roy Smith <roy@panix.com> - 2012-01-20 09:24 -0500

#19138 — verify the return value of a function

FromJabba Laci <jabba.laci@gmail.com>
Date2012-01-19 21:45 +0100
Subjectverify the return value of a function
Message-ID<mailman.4872.1327005963.27778.python-list@python.org>
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

[toc] | [next] | [standalone]


#19143

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2012-01-20 10:15 +0100
Message-ID<bvnnu8-7nb.ln1@satorlaser.homedns.org>
In reply to#19138
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

[toc] | [prev] | [next] | [standalone]


#19146

FromRoy Smith <roy@panix.com>
Date2012-01-20 09:24 -0500
Message-ID<roy-A00D31.09240420012012@news.panix.com>
In reply to#19138
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web