473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

doctest with variable return value

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

Jul 25 '06 #1
4 1847
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
Jul 25 '06 #2
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
Jul 25 '06 #3

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

Jul 26 '06 #4
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.
Jul 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Pierre Rouleau | last post by:
I have a problem writing self-testable modules using doctest when these modules have internationalized strings using gettext _('...'). - The main module of an application (say app.py) calls...
2
by: Alan G Isaac | last post by:
> python doctest.py -v Running doctest.__doc__ Trying: .remove(42) Expecting: Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: list.remove(x): x not in list ok...
0
by: bearophile | last post by:
doctest module is quite useful; and I've also tried the simpler docex: http://aima.cs.berkeley.edu/python/docex.html http://aima.cs.berkeley.edu/python/docex.py (docex isn't perfect, there are...
1
by: David MacKay | last post by:
Hello, I'm a python-list newbie. I've got a question about doctest; perhaps a bug report. I really like doctest, but sometimes doctest gives a failure when the output looks absolutely fine to me...
0
by: Steven Bethard | last post by:
I have an optparse-like module and though I have a full unittest-style suite of tests for it, I'd also like to be able to run doctest on the documentation to make sure my examples all work. ...
12
by: thomas.guest | last post by:
I'm not making progress with the following and would appreciate any help. Here's an interpreted Python session. .... Traceback (most recent call last): File "<stdin>", line 1, in <module>...
0
by: Edward Loper | last post by:
Anyone testing on xemacs? I tried it, and C-c C-c sent xemacs into an It works fine for me in XEmacs 21.4 (patch 17) (i386-debian-linux, Mule). If you could answer a few questions, it might...
6
by: Bzyczek | last post by:
Hello, I have problems with running doctests if I use czech national characters in UTF-8 encoding. I have Python script, which begin with encoding definition: # -*- coding: utf-8 -*- I...
0
by: dj | last post by:
Hello, I have just started working with minimock in doctest. I want to create a mock pyodbc object which returns a string value when the method execute is called. Here is my doctest: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.