doctest with variable return value

Duncan Booth duncan.booth at invalid.invalid
Wed Jul 26 03:42:41 EDT 2006


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.



More information about the Python-list mailing list