Message from discussion
ten small Python programs
Path: g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail
NNTP-Posting-Date: Sun, 27 May 2007 13:59:04 -0500
Date: Sun, 27 May 2007 12:59:01 -0600
From: Steven Bethard <steven.beth...@gmail.com>
User-Agent: Thunderbird 2.0.0.0 (Windows/20070326)
MIME-Version: 1.0
Newsgroups: comp.lang.python
Subject: Re: ten small Python programs
References: <mailman.8268.1180289324.32031.python-list@python.org>
In-Reply-To: <mailman.8268.1180289324.32031.python-list@python.org>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <FsadnQRcQORlScTbnZ2dnUVZ_jGdnZ2d@comcast.com>
Lines: 49
NNTP-Posting-Host: 67.166.43.236
X-Trace: sv3-bM61QNuqKX6bQPVUihZqMvUbq8OrVnh7DTRpgruHuQHsI+/djyWGOAr8sg1wk8vbz4oAhu6xenl86yt!pyEwSyHGzTQvs4Y2MfB+37XpcHLRFgyqgnHMKefVJOQDWfhU4EJVMLFssSZhHainrnHAs+BOJEog!axPwOyiH7+WRJfB139hSFU5LF1zsmA==
X-Complaints-To: abuse@comcast.net
X-DMCA-Complaints-To: d...@comcast.net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.34
Steve Howell wrote:
> --- Steven Bethard <steven.beth...@gmail.com> wrote:
>
>> I think I would rewrite the current unit-testing
>> example to use the
>> standard library unittest module::
>>
>> # Let's write reusable code, and unit test it.
>> def add_money(amounts):
>> # do arithmetic in pennies so as not to
>> accumulate float errors
>> pennies = sum([round(int(amount * 100)) for
>> amount in amounts])
>> return float(pennies / 100.0)
>> import unittest
>> class TestAddMoney(unittest.TestCase):
>> def test_float_errors(self):
>> self.failUnlessEqual(add_money([0.13,
>> 0.02]), 0.15)
>> self.failUnlessEqual(add_money([100.01,
>> 99.99]), 200)
>> self.failUnlessEqual(add_money([0,
>> -13.00, 13.00]), 0)
>> if __name__ == '__main__':
>> unittest.main()
>>
>> I believe I've still kept it to 13 lines.
>>
>
> I approve this change, although in a sense, it's
> harder for a Python newbie, because it introduces
> inheritance a little earlier than I would have liked.
>
> FWIW I'm in the minority (I think) of people that
> prefer roll-your-own testing, but I don't want to
> argue that, because I think it mostly comes down to
> personal preference.
Have you tried py.test?
http://codespeak.net/py/dist/test.html
I've heard good things about it, but haven't gotten around to trying it
yet. Here's a two-line test suite from the page above:
def test_answer():
assert 42 == 43
STeVe