Message from discussion
ten small Python programs
Path: g2news1.google.com!news4.google.com!news.germany.com!multikabel.net!feed20.multikabel.net!news2.euro.net!transit.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path: <showel...@yahoo.com>
X-Original-To: python-l...@python.org
Delivered-To: python-l...@bag.python.org
X-Spam-Status: OK 0.014
X-YMail-OSG: bqDgyM8VM1ktnLCToryq62ffw0mc0Aj5Lan1ngDCDe5dwk.j2ef9x_OFkdALfpWohEJex5izHDbJKoD70tA7JmGtFo7TPWVwbWCDBOXIE1JY6FwmNpk-
Date: Sun, 27 May 2007 12:00:16 -0700 (PDT)
From: Steve Howell <showel...@yahoo.com>
Subject: Re: ten small Python programs
To: Steven Bethard <steven.beth...@gmail.com>, python-l...@python.org
In-Reply-To: <VcCdnUfV8Yj8TcTbnZ2dnUVZ_h7inZ2d@comcast.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
X-BeenThere: python-l...@python.org
X-Mailman-Version: 2.1.9
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <http://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-requ...@python.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/python-list>
List-Post: <mailto:python-l...@python.org>
List-Help: <mailto:python-list-requ...@python.org?subject=help>
List-Subscribe: <http://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-requ...@python.org?subject=subscribe>
Newsgroups: comp.lang.python
Message-ID: <mailman.8272.1180292448.32031.python-list@python.org>
Lines: 65
NNTP-Posting-Host: 194.109.207.14
X-Trace: 1180292448 news.xs4all.nl 337 [::ffff:194.109.207.14]:60804
X-Complaints-To: abuse@xs4all.nl
--- 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?
I am in 100% agreement with you that most unit tests
would be completely outside the module, although I
often follow the practice that my modules have a
little "if __main__" section that runs a few simple
unit tests, as sort of a bit of self-documentation.
____________________________________________________________________________________Building a website is a piece of cake. Yahoo! Small Business gives you all the tools to get online.
http://smallbusiness.yahoo.com/webhosting