Efficient python programming...

Jimmy Retzlaff jimmy at retzlaff.com
Fri Jun 7 20:06:47 EDT 2002


Peter Hansen [mailto:peter at engcorp.com] writes:
> I disagree, if the tests are written first (and run, proving that
> they will fail when the code is wrong or missing).

I think the other perspective here is that the writing of the unit tests
is design/coding, and hence subject to the same types of mistakes as the
designing/writing of the code they test. A contrived example:


def addEmUp(x, y):
    return x * y

def testAddEmUp():
    # try some limit cases and something in between
    for testCase in (0, 2, 1.79e+308):
        assert addEmUp(testCase, testCase) == testCase+testCase

if __name__ == '__main__':
    testAddEmUp()
    print 'Success!!!'


That test runs successfully on Python 2.2.1 on Windows despite the
flawed implementation of the function the test is exercising. Having
unit tests may make you more confident in correctness than not having
them, but they are still prone to mistakes themselves. In order for unit
tests to prove the code is correct, you'd need to prove the unit tests
are correct and exhaustive (despite its correctness, the above unit test
is not exhaustive which leads to a false validation).

> The tests are executable requirement specifications.  Running
> them "proves" (yes, not 100%... nothing could) that the code
> meets the specs.  The specs could be wrong of course, but that
> doesn't mean the code is broken...

You can say the tests are your spec and hence your program is correct
with regards to the spec, but that doesn't make the user any happier if
your code costs them time, money, safety, etc. just because the part of
the code you call the spec was also flawed or incomplete.

Unit testing is just one of many important tools. On any significant
project, it is important to have a well-rounded quality strategy that
also employs things like design/code reviews, pair programming, user
testing, etc.

But if you're really after correctness, there may be only one true
measure - let a sales person demo the product! Good old Murphy ensures
that bugs are very good at exposing themselves during important sales
presentations. :)

Jimmy





More information about the Python-list mailing list