ten small Python programs
Steven Bethard
steven.bethard at gmail.com
Sun May 27 14:59:01 EDT 2007
Steve Howell wrote:
> --- Steven Bethard <steven.bethard at 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
More information about the Python-list
mailing list