mk wrote:
Jean-Paul Calderone wrote:
<snip>
The most significant thing missing from this code is unit tests. Developing automated tests for your code will help you learn a lot.

However, unit tests are a tougher cookie, do you have any resource that's truly worth recommending for learning unit tests? The only resource I found that was talking at some length about it was "Dive into Python" and I was wondering if there's anything better out there...

Do the "Dive In" thing.  You'll quickly develop instincts.
Start with the simplest possible test of your function,
run it, and fix your code until it passes.  Add another test,
and loop.  The simplest possible test will unfortunately need
a bit of bolerplate, but adding tests is easy.


to start:

    import unittest
    import moving_average as ma

    class MovingAverageTest(unittest.TestCase):
        def test_simplest(self):
            'Check the mechanics of getting a simple result'
            self.assertEqual(ma.moving_average(3, []), [])


    if __name__ == '__main__':
        unittest.main()

Once that works, add another test:
        def test_simple(self):
            self.assertAlmostEqual(ma.moving_average(3, [1, 2, 3]), [2])

Once that works, check something a bit easier to get wrong:
        def test_floating(self):
            'Check we have reasonable results'
            self.assertAlmostEqual(ma.moving_average(3, [1, 1, 3])[0],
                                   1.6666667)

and, as they so often say, "lather, rinse, repeat."

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to