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 14:01:56 -0500
Date: Sun, 27 May 2007 13:01:54 -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.8272.1180292448.32031.python-list@python.org>
In-Reply-To: <mailman.8272.1180292448.32031.python-list@python.org>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <FsadnQdcQOQ5SMTbnZ2dnUVZ_jGdnZ2d@comcast.com>
Lines: 54
NNTP-Posting-Host: 67.166.43.236
X-Trace: sv3-f6exoJQlPssvxZNbAElLcrEtfB+ClQ2O9CbfAYcsErF/UkVwey9afRPITlbkvnp2+4CyrT5QE+ia1Rs!bDXt4t7N6+gxnTueohcTwyHC2X0Gqz8RFXPUtRpwLKHw93Q20K8D2BA4VX5PryFZBKRnLCA4m2g/!rrotdXg32B3EjcT/YKnTWZc9gj0kHw==
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:
>
>> 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()
>>>>
>>> Just a minor quibble, but wouldn't you want the
>> import
>>> and test class to only get executed in the
>> ___main__
>>> context?
>> That would be fine too. In the real world, I'd put
>> the tests in a
>> different module.
>>
>
> Maybe this is the first good example that motivates a
> hyperlink to alternatives. Would you accept the idea
> that we keep my original example on the SimplePrograms
> page, but we link to a UnitTestingPhilosophies page,
> and we show your alternative there? Or vice versa,
> show your example on the first page, but then show
> mine on the hyperlinked page?
Sure. Either way is fine.
STeVe