verify the return value of a function

Discussion in 'Python' started by Jabba Laci, Jan 19, 2012.

  1. Jabba Laci

    Jabba Laci Guest

    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
    Jabba Laci, Jan 19, 2012
    #1
    1. Advertising

  2. 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
    Ulrich Eckhardt, Jan 20, 2012
    #2
    1. Advertising

  3. Jabba Laci

    Roy Smith Guest

    In article <>,
    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?


    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.
    Roy Smith, Jan 20, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Seong-Kook Shin
    Replies:
    1
    Views:
    473
    Richard Bos
    Jun 18, 2004
  2. Greenhorn
    Replies:
    15
    Views:
    784
    Keith Thompson
    Mar 6, 2005
  3. roberta

    Verify a collection value

    roberta, May 8, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    276
    Mr. Arnold
    May 9, 2010
  4. Jean-Michel Pichavant

    Re: verify the return value of a function

    Jean-Michel Pichavant, Jan 20, 2012, in forum: Python
    Replies:
    3
    Views:
    143
    Terry Reedy
    Jan 20, 2012
  5. jdog

    Verify current interval value?

    jdog, Oct 5, 2004, in forum: Javascript
    Replies:
    1
    Views:
    63
    Michael Winter
    Oct 7, 2004
Loading...

Share This Page