1 Writing Python Regression Tests
2 -------------------------------
9 If you add a new module to Python or modify the functionality of an existing
10 module, you should write one or more test cases to exercise that new
11 functionality. There are different ways to do this within the regression
12 testing facility provided with Python; any particular test should use only
13 one of these options. Each option requires writing a test module using the
14 conventions of the the selected option:
18 - "traditional" Python test modules
20 Regardless of the mechanics of the testing approach you choose,
21 you will be writing unit tests (isolated tests of functions and objects
22 defined by the module) using white box techniques. Unlike black box
23 testing, where you only have the external interfaces to guide your test case
24 writing, in white box testing you can see the code being tested and tailor
25 your test cases to exercise it more completely. In particular, you will be
26 able to refer to the C and Python code in the CVS repository when writing
27 your regression test cases.
32 The PyUnit framework is based on the ideas of unit testing as espoused
33 by Kent Beck and the Extreme Programming (XP) movement. The specific
34 interface provided by the framework is tightly based on the JUnit
35 Java implementation of Beck's original SmallTalk test framework. Please
36 see the documentation of the unittest module for detailed information on
37 the interface and general guidelines on writing PyUnit based tests.
39 The test_support helper module provides a two functions for use by
40 PyUnit based tests in the Python regression testing framework:
41 run_unittest() takes a unittest.TestCase derived class as a parameter
42 and runs the tests defined in that class, and run_suite() takes a
43 populated TestSuite instance and runs the tests. run_suite() is
44 preferred because unittest files typically grow multiple test classes,
45 and you might as well be prepared.
47 All test methods in the Python regression framework have names that
48 start with "test_" and use lower-case names with words separated with
51 All PyUnit-based tests in the Python test suite use boilerplate that
57 class MyTestCase1(unittest.TestCase):
58 # define test methods here...
60 class MyTestCase2(unittest.TestCase):
61 # define more test methods here...
64 suite = unittest.TestSuite()
65 suite.addTest(unittest.makeSuite(MyTestCase1))
66 suite.addTest(unittest.makeSuite(MyTestCase2))
67 test_support.run_suite(suite)
69 if __name__ == "__main__":
72 This has the advantage that it allows the unittest module to be used
73 as a script to run individual tests as well as working well with the
79 Tests written to use doctest are actually part of the docstrings for
80 the module being tested. Each test is written as a display of an
81 interactive session, including the Python prompts, statements that would
82 be typed by the user, and the output of those statements (including
83 tracebacks, although only the exception msg needs to be retained then).
84 The module in the test package is simply a wrapper that causes doctest
85 to run over the tests in the module. The test for the difflib module
86 provides a convenient example:
88 import difflib, test_support
89 test_support.run_doctest(difflib)
91 If the test is successful, nothing is written to stdout (so you should not
92 create a corresponding output/test_difflib file), but running regrtest
93 with -v will give a detailed report, the same as if passing -v to doctest.
95 A second argument can be passed to run_doctest to tell doctest to search
96 sys.argv for -v instead of using test_support's idea of verbosity. This
97 is useful for writing doctest-based tests that aren't simply running a
98 doctest'ed Lib module, but contain the doctests themselves. Then at
99 times you may want to run such a test directly as a doctest, independent
100 of the regrtest framework. The tail end of test_descrtut.py is a good
103 def test_main(verbose=None):
104 import test_support, test.test_descrtut
105 test_support.run_doctest(test.test_descrtut, verbose)
107 if __name__ == "__main__":
110 If run via regrtest, test_main() is called (by regrtest) without specifying
111 verbose, and then test_support's idea of verbosity is used. But when
112 run directly, test_main(1) is called, and then doctest's idea of verbosity
115 See the documentation for the doctest module for information on
116 writing tests using the doctest framework.
119 "traditional" Python test modules
121 The mechanics of how the "traditional" test system operates are fairly
122 straightforward. When a test case is run, the output is compared with the
123 expected output that is stored in .../Lib/test/output. If the test runs to
124 completion and the actual and expected outputs match, the test succeeds, if
125 not, it fails. If an ImportError or test_support.TestSkipped error is
126 raised, the test is not run.
131 If you are writing test cases for module spam, you need to create a file
132 in .../Lib/test named test_spam.py. In addition, if the tests are expected
133 to write to stdout during a successful run, you also need to create an
134 expected output file in .../Lib/test/output named test_spam ("..."
135 represents the top-level directory in the Python source tree, the directory
136 containing the configure script). If needed, generate the initial version
137 of the test output file by executing:
139 ./python Lib/test/regrtest.py -g test_spam.py
141 from the top-level directory.
143 Any time you modify test_spam.py you need to generate a new expected
144 output file. Don't forget to desk check the generated output to make sure
145 it's really what you expected to find! All in all it's usually better
146 not to have an expected-out file (note that doctest- and unittest-based
149 To run a single test after modifying a module, simply run regrtest.py
152 ./python Lib/test/regrtest.py test_spam.py
154 While debugging a regression test, you can of course execute it
155 independently of the regression testing framework and see what it prints:
157 ./python Lib/test/test_spam.py
159 To run the entire test suite:
161 [UNIX, + other platforms where "make" works] Make the "test" target at the
166 [WINDOWS] Run rt.bat from your PCBuild directory. Read the comments at
167 the top of rt.bat for the use of special -d, -O and -q options processed
170 [OTHER] You can simply execute the two runs of regrtest (optimized and
171 non-optimized) directly:
173 ./python Lib/test/regrtest.py
174 ./python -O Lib/test/regrtest.py
176 But note that this way picks up whatever .pyc and .pyo files happen to be
177 around. The makefile and rt.bat ways run the tests twice, the first time
178 removing all .pyc and .pyo files from the subtree rooted at Lib/.
180 Test cases generate output based upon values computed by the test code.
181 When executed, regrtest.py compares the actual output generated by executing
182 the test case with the expected output and reports success or failure. It
183 stands to reason that if the actual and expected outputs are to match, they
184 must not contain any machine dependencies. This means your test cases
185 should not print out absolute machine addresses (e.g. the return value of
186 the id() builtin function) or floating point numbers with large numbers of
187 significant digits (unless you understand what you are doing!).
190 Test Case Writing Tips
192 Writing good test cases is a skilled task and is too complex to discuss in
193 detail in this short document. Many books have been written on the subject.
194 I'll show my age by suggesting that Glenford Myers' "The Art of Software
195 Testing", published in 1979, is still the best introduction to the subject
196 available. It is short (177 pages), easy to read, and discusses the major
197 elements of software testing, though its publication predates the
198 object-oriented software revolution, so doesn't cover that subject at all.
199 Unfortunately, it is very expensive (about $100 new). If you can borrow it
200 or find it used (around $20), I strongly urge you to pick up a copy.
202 The most important goal when writing test cases is to break things. A test
203 case that doesn't uncover a bug is much less valuable than one that does.
204 In designing test cases you should pay attention to the following:
206 * Your test cases should exercise all the functions and objects defined
207 in the module, not just the ones meant to be called by users of your
208 module. This may require you to write test code that uses the module
209 in ways you don't expect (explicitly calling internal functions, for
210 example - see test_atexit.py).
212 * You should consider any boundary values that may tickle exceptional
213 conditions (e.g. if you were writing regression tests for division,
214 you might well want to generate tests with numerators and denominators
215 at the limits of floating point and integer numbers on the machine
216 performing the tests as well as a denominator of zero).
218 * You should exercise as many paths through the code as possible. This
219 may not always be possible, but is a goal to strive for. In
220 particular, when considering if statements (or their equivalent), you
221 want to create test cases that exercise both the true and false
222 branches. For loops, you should create test cases that exercise the
223 loop zero, one and multiple times.
225 * You should test with obviously invalid input. If you know that a
226 function requires an integer input, try calling it with other types of
227 objects to see how it responds.
229 * You should test with obviously out-of-range input. If the domain of a
230 function is only defined for positive integers, try calling it with a
233 * If you are going to fix a bug that wasn't uncovered by an existing
234 test, try to write a test case that exposes the bug (preferably before
237 * If you need to create a temporary file, you can use the filename in
238 test_support.TESTFN to do so. It is important to remove the file
239 when done; other tests should be able to use the name without cleaning
243 Regression Test Writing Rules
245 Each test case is different. There is no "standard" form for a Python
246 regression test case, though there are some general rules (note that
247 these mostly apply only to the "classic" tests; unittest- and doctest-
248 based tests should follow the conventions natural to those frameworks):
250 * If your test case detects a failure, raise TestFailed (found in
253 * Import everything you'll need as early as possible.
255 * If you'll be importing objects from a module that is at least
256 partially platform-dependent, only import those objects you need for
257 the current test case to avoid spurious ImportError exceptions that
258 prevent the test from running to completion.
260 * Print all your test case results using the print statement. For
261 non-fatal errors, print an error message (or omit a successful
262 completion print) to indicate the failure, but proceed instead of
265 * Use "assert" sparingly, if at all. It's usually better to just print
266 what you got, and rely on regrtest's got-vs-expected comparison to
267 catch deviations from what you expect. assert statements aren't
268 executed at all when regrtest is run in -O mode; and, because they
269 cause the test to stop immediately, can lead to a long & tedious
270 test-fix, test-fix, test-fix, ... cycle when things are badly broken
271 (and note that "badly broken" often includes running the test suite
272 for the first time on new platforms or under new implementations of
278 There is a test_support module you can import from your test case. It
279 provides the following useful objects:
281 * TestFailed - raise this exception when your regression test detects a
284 * TestSkipped - raise this if the test could not be run because the
285 platform doesn't offer all the required facilities (like large
286 file support), even if all the required modules are available.
288 * verbose - you can use this variable to control print output. Many
289 modules use it. Search for "verbose" in the test_*.py files to see
292 * verify(condition, reason='test failed'). Use this instead of
294 assert condition[, reason]
296 verify() has two advantages over assert: it works even in -O mode,
297 and it raises TestFailed on failure instead of AssertionError.
299 * TESTFN - a string that should always be used as the filename when you
300 need to create a temp file. Also use try/finally to ensure that your
301 temp files are deleted before your test completes. Note that you
302 cannot unlink an open file on all operating systems, so also be sure
303 to close temp files before trying to unlink them.
305 * sortdict(dict) - acts like repr(dict.items()), but sorts the items
306 first. This is important when printing a dict value, because the
307 order of items produced by dict.items() is not defined by the
310 * findfile(file) - you can call this function to locate a file somewhere
311 along sys.path or in the Lib/test tree - see test_linuxaudiodev.py for
312 an example of its use.
314 * use_large_resources - true iff tests requiring large time or space
317 * fcmp(x,y) - you can call this function to compare two floating point
318 numbers when you expect them to only be approximately equal withing a
319 fuzz factor (test_support.FUZZ, which defaults to 1e-6).
321 NOTE: Always import something from test_support like so:
323 from test_support import verbose
328 ... use test_support.verbose in the code ...
330 Never import anything from test_support like this:
332 from test.test_support import verbose
334 "test" is a package already, so can refer to modules it contains without
335 "test." qualification. If you do an explicit "test.xxx" qualification, that
336 can fool Python into believing test.xxx is a module distinct from the xxx
337 in the current package, and you can end up importing two distinct copies of
338 xxx. This is especially bad if xxx=test_support, as regrtest.py can (and
339 routinely does) overwrite its "verbose" and "use_large_resources"
340 attributes: if you get a second copy of test_support loaded, it may not
341 have the same values for those as regrtest intended.
344 Python and C statement coverage results are currently available at
346 http://www.musi-cal.com/~skip/python/Python/dist/src/
348 As of this writing (July, 2000) these results are being generated nightly.
349 You can refer to the summaries and the test coverage output files to see
350 where coverage is adequate or lacking and write test cases to beef up the
354 Some Non-Obvious regrtest Features
356 * Automagic test detection: When you create a new test file
357 test_spam.py, you do not need to modify regrtest (or anything else)
358 to advertise its existence. regrtest searches for and runs all
359 modules in the test directory with names of the form test_xxx.py.
361 * Miranda output: If, when running test_spam.py, regrtest does not
362 find an expected-output file test/output/test_spam, regrtest
363 pretends that it did find one, containing the single line
367 This allows new tests that don't expect to print anything to stdout
368 to not bother creating expected-output files.
370 * Two-stage testing: To run test_spam.py, regrtest imports test_spam
371 as a module. Most tests run to completion as a side-effect of
372 getting imported. After importing test_spam, regrtest also executes
373 test_spam.test_main(), if test_spam has a "test_main" attribute.
374 This is rarely required with the "traditional" Python tests, and
375 you shouldn't create a module global with name test_main unless
376 you're specifically exploiting this gimmick. This usage does
377 prove useful with PyUnit-based tests as well, however; defining
378 a test_main() which is run by regrtest and a script-stub in the
379 test module ("if __name__ == '__main__': test_main()") allows
380 the test to be used like any other Python test and also work
381 with the unittest.py-as-a-script approach, allowing a developer
382 to run specific tests from the command line.