SimplePrograms challenge

Pete Forman pete.forman at westerngeco.com
Wed Jun 20 08:54:13 EDT 2007


Steve Howell <showell30 at yahoo.com> writes:

>> 2) assert is not the simplest example of doctest. 
>> The style should be
>> 
>>     >>> add_money([0.13, 0.02])
>>     0.15
>>     >>> add_money([100.01, 99.99])
>>     200.0
>>     >>> add_money([0, -13.00, 13.00])
>>     0.0
>> 
>
 > That's not clear cut to me.  I think vertical
 > conciseness has an advantage for readability, as it
 > means you get to keep more "real" code on the screen.

What I meant was that doctest should be "type this into the
interpreter and you should see that".  A doctest is not a unit test,
though it may form a subset of the tests.  There should only be enough
doctests to enclue a human reader.  Comprehensive testing should use a
larger framework.  Doctests in separate files can do this but I would
use py.test, or alternatives like nose or Testoob.

    >>> 2 + 2
    4

"assert 2 + 2 == 4" is a concise way of writing a unit test but it is
not the best way to use doctest IMHO.

>> 3) which fails :-(  So both the unittest and doctest
>> examples ought to
>>    be redone to emphasize what they are doing
>> without getting bogged
>>    down by issues of floating point representations.
>> 
>
 > I was the one who originally posted the floating point
 > example (with yet another style of unit testing, BTW),
 > and I agree that the subtleties of floating point do
 > kind of cloud the issue.  I welcome a better example. 
 > What I didn't realize is that there's an actual error.
 >  Are you saying the program fails?  On which test?

Python 2.5.1 on XP:

Failed example:
    add_money([0.13, 0.02])
Expected:
    0.15
Got:
    0.14999999999999999


-- 
Pete Forman                -./\.-  Disclaimer: This post is originated
WesternGeco                  -./\.-   by myself and does not represent
pete.forman at westerngeco.com    -./\.-   the opinion of Schlumberger or
http://petef.port5.com           -./\.-   WesternGeco.



More information about the Python-list mailing list