doctest with variable return value

Discussion in 'Python' started by 3KWA, Jul 25, 2006.

  1. 3KWA

    3KWA Guest

    Hi all,

    I am wondering what is the standard doctest (test) practice for
    functions who's returned value change all the time e.g. forex rate:

    import urllib

    def get_rate(symbol):
    """get_rate(symbol) connects to yahoo finance to return the rate of
    symbol.

    >>>get_rate('AUDEUR')


    """

    url=
    "http://finance.yahoo.com/d/quotes.csv?s=%s=X&f=sl1d1t1c1ohgv&e=.csv" %
    \
    symbol
    f=urllib.urlopen(url)
    return float(f.readline().split(',')[1])

    As you can guess I am very new to unittest and doctest in general ...

    Thanks for your help,

    EuGeNe
     
    3KWA, Jul 25, 2006
    #1
    1. Advertising

  2. 3KWA

    Rob Sinclar Guest

    On Tuesday 25 July 2006 09:53, 3KWA wrote:
    > Hi all,
    >
    > I am wondering what is the standard doctest (test) practice for
    > functions who's returned value change all the time e.g. forex rate:
    >
    > import urllib
    >
    > def get_rate(symbol):
    > """get_rate(symbol) connects to yahoo finance to return the rate of
    > symbol.
    >
    > >>>get_rate('AUDEUR')

    >
    > """
    >
    > url=
    > "http://finance.yahoo.com/d/quotes.csv?s=%s=X&f=sl1d1t1c1ohgv&e=.csv" %
    > \
    > symbol
    > f=urllib.urlopen(url)
    > return float(f.readline().split(',')[1])
    >
    > As you can guess I am very new to unittest and doctest in general ...
    >
    > Thanks for your help,
    >
    > EuGeNe


    Hi EuGeNe,
    Pass it through a variable before returning a value.
    Here's how I would do it:

    import urllib2
    def get_rate(symbol):

    URL='http://finance.yahoo.com/d/quotes.csv?s=AUDEUR=X&f=sl1d1t1c1ohgv&e=.csv'
    request_headers = { 'User-Agent': 'Linuxinclar/0.1' }
    request = urllib2.Request(URL, None, request_headers)
    response = urllib2.urlopen(request)
    STR = response.read()
    return STR.split(',')[1].strip()

    SYMB='AUDEUR'
    print SYMB,'=',get_rate(SYMB)

    Python rocks.
    That's be nice to indicate hour though (4th array element)...

    Best Regards,
    Rob Sinclar
     
    Rob Sinclar, Jul 25, 2006
    #2
    1. Advertising

  3. 3KWA

    Peter Otten Guest

    3KWA wrote:

    > I am wondering what is the standard doctest (test) practice for
    > functions who's returned value change all the time e.g. forex rate:
    >
    > import urllib
    >
    > def get_rate(symbol):
    > """get_rate(symbol) connects to yahoo finance to return the rate of
    > symbol.
    >
    > >>>get_rate('AUDEUR')

    >
    > """
    >
    > url=
    > "http://finance.yahoo.com/d/quotes.csv?s=%s=X&f=sl1d1t1c1ohgv&e=.csv" %
    > \
    > symbol
    > f=urllib.urlopen(url)
    > return float(f.readline().split(',')[1])


    You cannot test for an unknown value, but you can do some sanity checks:

    >>> rate = get_rate('AUDEUR')
    >>> rate > 0

    True
    >>> isinstance(rate, float)

    True

    This will at least make sure that get_rate() does not throw an exception.
    You can also spoonfeed it with handcrafted data...

    >>> def mock_urlopen(url):

    ... from cStringIO import StringIO
    ... return StringIO("yadda,0.1234")
    ...
    >>> urllib.urlopen = mock_urlopen
    >>> get_rate("AUDEUR")

    0.1234

    but this has the disadvantage that the test has to know about the actual
    implementation of the function about to be tested.

    Peter
     
    Peter Otten, Jul 25, 2006
    #3
  4. 3KWA

    3KWA Guest

    Peter Otten wrote:

    > You cannot test for an unknown value, but you can do some sanity checks:
    >
    > >>> rate = get_rate('AUDEUR')
    > >>> rate > 0

    > True
    > >>> isinstance(rate, float)

    > True
    >
    > This will at least make sure that get_rate() does not throw an exception.


    Thanks a lot ... sanity checks ... it makes a lot of sense to me!

    At EuroPython I attended a talk where someone said that untested code
    is nothing ... so I am trying to write something instead of nothing ...
    on the other hand can code ever be over tested?

    EuGeNe
     
    3KWA, Jul 26, 2006
    #4
  5. 3KWA

    Duncan Booth Guest

    3KWA wrote:

    >
    > Peter Otten wrote:
    >
    >> You cannot test for an unknown value, but you can do some sanity
    >> checks:
    >>
    >> >>> rate = get_rate('AUDEUR')
    >> >>> rate > 0

    >> True
    >> >>> isinstance(rate, float)

    >> True
    >>
    >> This will at least make sure that get_rate() does not throw an
    >> exception.

    >
    > Thanks a lot ... sanity checks ... it makes a lot of sense to me!
    >
    > At EuroPython I attended a talk where someone said that untested code
    > is nothing ... so I am trying to write something instead of nothing
    > ... on the other hand can code ever be over tested?
    >

    Yes, code can be over tested: tests require maintenance just like any other
    code, so you should avoid having tests which just duplicate other tests and
    don't add any value. e.g. If you know the rate code works for a few
    currencies it probably also works for most others, so you don't need to
    exhaustively test all possible currency pairs.

    For your unknown value, I would choose a range which you might expect to
    hold true for a reasonable period of time and check the value is inside
    that range. If the currencies shift massively you might need to update the
    test, but with luck that won't happen:

    >>> 0.4 <= get_rate('AUDEUR') <= 0.8


    That way if your code starts accidentally returning EURAUD you should catch
    it in the test, but minor shifts shouldn't matter.
     
    Duncan Booth, Jul 26, 2006
    #5
    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. PvdK
    Replies:
    0
    Views:
    2,981
  2. Seong-Kook Shin
    Replies:
    1
    Views:
    495
    Richard Bos
    Jun 18, 2004
  3. mitchell
    Replies:
    1
    Views:
    267
    Fredrik Lundh
    May 26, 2005
  4. Steven Bethard
    Replies:
    0
    Views:
    393
    Steven Bethard
    Jul 1, 2006
  5. Greenhorn
    Replies:
    15
    Views:
    831
    Keith Thompson
    Mar 6, 2005
Loading...

Share This Page