blob: 2cf7736f618f2da6e0f7b5633033d3154d64263e [file] [log] [blame]
Skip Montanaro47c60ec2000-06-30 06:08:35 +00001 Writing Python Test Cases
2 -------------------------
3 Skip Montanaro
4
5If you add a new module to Python or modify the functionality of an existing
6module, it is your responsibility to write one or more test cases to test
7that new functionality. The mechanics of the test system are fairly
8straightforward. If you are writing test cases for module zyzzyx, you need
9to create a file in .../Lib/test named test_zyzzyx.py and an expected output
10file in .../Lib/test/output named test_zyzzyx ("..." represents the
11top-level directory in the Python source tree, the directory containing the
12configure script). Generate the initial version of the test output file by
13executing:
14
15 cd .../Lib/test
16 python regrtest.py -g test_zyzzyx.py
17
18Any time you modify test_zyzzyx.py you need to generate a new expected
19output file. Don't forget to desk check the generated output to make sure
20it's really what you expected to find! To run a single test after modifying
21a module, simply run regrtest.py without the -g flag:
22
23 cd .../Lib/test
24 python regrtest.py test_zyzzyx.py
25
26To run the entire test suite, make the "test" target at the top level:
27
28 cd ...
29 make test
30
31Test cases generate output based upon computed values and branches taken in
32the code. When executed, regrtest.py compares the actual output generated
33by executing the test case with the expected output and reports success or
34failure. It stands to reason that if the actual and expected outputs are to
35match, they must not contain any machine dependencies. This means
36your test cases should not print out absolute machine addresses or floating
37point numbers with large numbers of significant digits.
38
39Writing good test cases is a skilled task and is too complex to discuss in
40detail in this short document. Many books have been written on the subject.
41I'll show my age by suggesting that Glenford Myers' "The Art of Software
42Testing", published in 1979, is still the best introduction to the subject
43available. It is short (177 pages), easy to read, and discusses the major
44elements of software testing, though its publication predates the
45object-oriented software revolution, so doesn't cover that subject at all.
46Unfortunately, it is very expensive (about $100 new). If you can borrow it
47or find it used (around $20), I strongly urge you to pick up a copy.
48
49As an author of at least part of a module, you will be writing unit tests
50(isolated tests of functions and objects defined by the module) using white
51box techniques. (Unlike black box testing, where you only have the external
52interfaces to guide your test case writing, in white box testing you can see
53the code being tested and tailor your test cases to exercise it more
54completely).
55
56The most important goal when writing test cases is to break things. A test
57case that doesn't uncover a bug is less valuable than one that does. In
58designing test cases you should pay attention to the following:
59
60 1. Your test cases should exercise all the functions and objects defined
61 in the module, not just the ones meant to be called by users of your
62 module. This may require you to write test code that uses the module
63 in ways you don't expect (explicitly calling internal functions, for
64 example - see test_atexit.py).
65
66 2. You should consider any boundary values that may tickle exceptional
67 conditions (e.g. if you were testing a division module you might well
68 want to generate tests with numerators and denominators at the limits
69 of floating point and integer numbers on the machine performing the
70 tests as well as a denominator of zero).
71
72 3. You should exercise as many paths through the code as possible. This
73 may not always be possible, but is a goal to strive for. In
74 particular, when considering if statements (or their equivalent), you
75 want to create test cases that exercise both the true and false
76 branches. For while and for statements, you should create test cases
77 that exercise the loop zero, one and multiple times.