doctest with variable return value

3

3KWA

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.

"""

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
 
R

Rob Sinclar

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.

"""

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
 
P

Peter Otten

3KWA said:
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.

"""

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

This will at least make sure that get_rate() does not throw an exception.
You can also spoonfeed it with handcrafted data...
... from cStringIO import StringIO
... return StringIO("yadda,0.1234")
... 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
 
3

3KWA

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

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
 
D

Duncan Booth

3KWA said:
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:

That way if your code starts accidentally returning EURAUD you should catch
it in the test, but minor shifts shouldn't matter.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top