verify the return value of a function

J

Jabba Laci

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
 
U

Ulrich Eckhardt

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:
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
 
R

Roy Smith

Jabba Laci said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top