Where did you learn to unit test? How did you learn?

Skip Montanaro skip at pobox.com
Thu May 1 15:44:03 EDT 2003


    * What exactly is a unit test?

A set of unit tests is an executable specification of the functional
requirements for a piece of software.  A single unit test associates a
particular input with an expected output.  For each test you are saying, "If
I execute X, the result should be Y."  If that's not the case, the test
fails.  (Of course, if it succeeds there is still the possibility that you
wrote the test incorrectly. ;-)

    * How does the investment payoff?

When you make a change to your software and all the unit tests still pass
(including the new test(s) you wrote which specifically exercise the
changes), you have greater confidence that you haven't broken something than
if you have no tests.  When you write a unit test that fails, then modify
your code so the test succeeds, you have greater confidence that you
actually fixed the bug than if you didn't have any tests.

    * Where can the newer programmers find good examples of unit tests?

The Python source distribution has a lot of unit tests in Lib/test.  Also,
check out the PyUnit web site at <http://pyunit.sf.net/> and the various
open source software packages which are mentioned (Cheetah, MoinMoin,
BicycleRepairMan, etc).

Here are some other more general links:

    http://starship.python.net/crew/tbryan/UnitTestTalk/slide1.html
    http://xp.c2.com/TestDrivenProgramming.html
    http://www.extremeprogramming.org/

There was another thing I came across a few months ago (maybe an ebook in
PDF form on XP or test-driven programming) which I thought was excellent.
It worked through a complete example, starting from square one, including
all the detail.  Unfortunately, I can't find it now.

Skip





More information about the Python-list mailing list