blob: 40837e481e5e84c927301e675a90a38ccb0ac30f [file] [log] [blame]
Guido van Rossum54a069f2001-05-23 13:24:30 +00001 Writing Python Regression Tests
2 -------------------------------
3 Skip Montanaro
4 (skip@mojam.com)
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +00005
6
7Introduction
Skip Montanaro47c60ec2000-06-30 06:08:35 +00008
9If you add a new module to Python or modify the functionality of an existing
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +000010module, you should write one or more test cases to exercise that new
Fred Drakea6daad22001-05-23 04:57:49 +000011functionality. There are different ways to do this within the regression
12testing facility provided with Python; any particular test should use only
13one of these options. Each option requires writing a test module using the
14conventions of the the selected option:
Skip Montanaro47c60ec2000-06-30 06:08:35 +000015
Fred Drakea6daad22001-05-23 04:57:49 +000016 - PyUnit based tests
17 - doctest based tests
18 - "traditional" Python test modules
19
20Regardless of the mechanics of the testing approach you choose,
21you will be writing unit tests (isolated tests of functions and objects
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +000022defined by the module) using white box techniques. Unlike black box
23testing, where you only have the external interfaces to guide your test case
24writing, in white box testing you can see the code being tested and tailor
25your test cases to exercise it more completely. In particular, you will be
26able to refer to the C and Python code in the CVS repository when writing
27your regression test cases.
Skip Montanaro47c60ec2000-06-30 06:08:35 +000028
Tim Petersf5f6c432001-05-23 07:46:36 +000029
Fred Drakea6daad22001-05-23 04:57:49 +000030PyUnit based tests
31
32The PyUnit framework is based on the ideas of unit testing as espoused
33by Kent Beck and the Extreme Programming (XP) movement. The specific
34interface provided by the framework is tightly based on the JUnit
35Java implementation of Beck's original SmallTalk test framework. Please
36see the documentation of the unittest module for detailed information on
37the interface and general guidelines on writing PyUnit based tests.
38
Fred Drakeb2ad1c82001-09-28 20:05:25 +000039The test_support helper module provides a two functions for use by
Fred Drakea6daad22001-05-23 04:57:49 +000040PyUnit based tests in the Python regression testing framework:
41run_unittest() takes a unittest.TestCase derived class as a parameter
Fred Drakeb2ad1c82001-09-28 20:05:25 +000042and runs the tests defined in that class, and run_suite() takes a
Barry Warsaw5ca53742002-04-23 21:39:00 +000043populated TestSuite instance and runs the tests. run_suite() is
44preferred because unittest files typically grow multiple test classes,
45and you might as well be prepared.
46
47All test methods in the Python regression framework have names that
48start with "test_" and use lower-case names with words separated with
49underscores.
Fred Drakeb2ad1c82001-09-28 20:05:25 +000050
Guido van Rossum1c486542002-08-22 20:08:14 +000051Test methods should *not* have docstrings! The unittest module prints
52the docstring if there is one, but otherwise prints the function name
53and the full class name. When there's a problem with a test, the
54latter information makes it easier to find the source for the test
55than the docstring.
56
Fred Drakeb2ad1c82001-09-28 20:05:25 +000057All PyUnit-based tests in the Python test suite use boilerplate that
Guido van Rossum1c486542002-08-22 20:08:14 +000058looks like this (with minor variations):
Fred Drakeb2ad1c82001-09-28 20:05:25 +000059
60 import unittest
Barry Warsaw04f357c2002-07-23 19:04:11 +000061 from test import test_support
Fred Drakeb2ad1c82001-09-28 20:05:25 +000062
Barry Warsaw5ca53742002-04-23 21:39:00 +000063 class MyTestCase1(unittest.TestCase):
Guido van Rossum1c486542002-08-22 20:08:14 +000064
65 # Define setUp and tearDown only if needed
66
67 def setUp(self):
68 unittest.TestCase.setUp(self)
69 ... additional initialization...
70
71 def tearDown(self):
72 ... additional finalization...
73 unittest.TestCase.tearDown(self)
74
75 def test_feature_one(self):
76 # Testing feature one
77 ...unit test for feature one...
78
79 def test_feature_two(self):
80 # Testing feature two
81 ...unit test for feature two...
82
83 ...etc...
Fred Drakeb2ad1c82001-09-28 20:05:25 +000084
Barry Warsaw5ca53742002-04-23 21:39:00 +000085 class MyTestCase2(unittest.TestCase):
Guido van Rossum1c486542002-08-22 20:08:14 +000086 ...same structure as MyTestCase1...
87
88 ...etc...
Barry Warsaw5ca53742002-04-23 21:39:00 +000089
Fred Drakeb2ad1c82001-09-28 20:05:25 +000090 def test_main():
Guido van Rossum1c486542002-08-22 20:08:14 +000091 suite = unittest.TestSuite()
92 suite.addTest(unittest.makeSuite(MyTestCase1))
93 suite.addTest(unittest.makeSuite(MyTestCase2))
94 ...add more suites...
Barry Warsaw5ca53742002-04-23 21:39:00 +000095 test_support.run_suite(suite)
Fred Drakeb2ad1c82001-09-28 20:05:25 +000096
97 if __name__ == "__main__":
98 test_main()
99
100This has the advantage that it allows the unittest module to be used
101as a script to run individual tests as well as working well with the
102regrtest framework.
Fred Drakea6daad22001-05-23 04:57:49 +0000103
Tim Petersf5f6c432001-05-23 07:46:36 +0000104
Fred Drakea6daad22001-05-23 04:57:49 +0000105doctest based tests
106
107Tests written to use doctest are actually part of the docstrings for
108the module being tested. Each test is written as a display of an
109interactive session, including the Python prompts, statements that would
110be typed by the user, and the output of those statements (including
Tim Petersf5f6c432001-05-23 07:46:36 +0000111tracebacks, although only the exception msg needs to be retained then).
112The module in the test package is simply a wrapper that causes doctest
113to run over the tests in the module. The test for the difflib module
114provides a convenient example:
Fred Drakea6daad22001-05-23 04:57:49 +0000115
Barry Warsaw1bfab7b2002-07-23 19:13:45 +0000116 import difflib
117 from test import test_support
Tim Petersa0a62222001-09-09 06:12:01 +0000118 test_support.run_doctest(difflib)
Tim Petersf5f6c432001-05-23 07:46:36 +0000119
120If the test is successful, nothing is written to stdout (so you should not
121create a corresponding output/test_difflib file), but running regrtest
Tim Petersa0a62222001-09-09 06:12:01 +0000122with -v will give a detailed report, the same as if passing -v to doctest.
123
124A second argument can be passed to run_doctest to tell doctest to search
125sys.argv for -v instead of using test_support's idea of verbosity. This
126is useful for writing doctest-based tests that aren't simply running a
127doctest'ed Lib module, but contain the doctests themselves. Then at
128times you may want to run such a test directly as a doctest, independent
129of the regrtest framework. The tail end of test_descrtut.py is a good
130example:
131
132 def test_main(verbose=None):
Barry Warsaw1bfab7b2002-07-23 19:13:45 +0000133 from test import test_support, test_descrtut
134 test_support.run_doctest(test_descrtut, verbose)
Tim Petersa0a62222001-09-09 06:12:01 +0000135
136 if __name__ == "__main__":
137 test_main(1)
138
139If run via regrtest, test_main() is called (by regrtest) without specifying
Tim Petersbea3fb82001-09-10 01:39:21 +0000140verbose, and then test_support's idea of verbosity is used. But when
Tim Petersa0a62222001-09-09 06:12:01 +0000141run directly, test_main(1) is called, and then doctest's idea of verbosity
142is used.
Fred Drakea6daad22001-05-23 04:57:49 +0000143
144See the documentation for the doctest module for information on
145writing tests using the doctest framework.
146
Tim Petersf5f6c432001-05-23 07:46:36 +0000147
Fred Drakea6daad22001-05-23 04:57:49 +0000148"traditional" Python test modules
149
150The mechanics of how the "traditional" test system operates are fairly
151straightforward. When a test case is run, the output is compared with the
152expected output that is stored in .../Lib/test/output. If the test runs to
153completion and the actual and expected outputs match, the test succeeds, if
154not, it fails. If an ImportError or test_support.TestSkipped error is
155raised, the test is not run.
156
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000157
158Executing Test Cases
159
160If you are writing test cases for module spam, you need to create a file
Tim Petersf5f6c432001-05-23 07:46:36 +0000161in .../Lib/test named test_spam.py. In addition, if the tests are expected
162to write to stdout during a successful run, you also need to create an
163expected output file in .../Lib/test/output named test_spam ("..."
164represents the top-level directory in the Python source tree, the directory
165containing the configure script). If needed, generate the initial version
166of the test output file by executing:
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000167
168 ./python Lib/test/regrtest.py -g test_spam.py
169
Tim Petersf5f6c432001-05-23 07:46:36 +0000170from the top-level directory.
Fred Drakea6daad22001-05-23 04:57:49 +0000171
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000172Any time you modify test_spam.py you need to generate a new expected
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000173output file. Don't forget to desk check the generated output to make sure
Tim Petersf5f6c432001-05-23 07:46:36 +0000174it's really what you expected to find! All in all it's usually better
175not to have an expected-out file (note that doctest- and unittest-based
176tests do not).
177
178To run a single test after modifying a module, simply run regrtest.py
179without the -g flag:
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000180
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000181 ./python Lib/test/regrtest.py test_spam.py
182
183While debugging a regression test, you can of course execute it
184independently of the regression testing framework and see what it prints:
185
186 ./python Lib/test/test_spam.py
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000187
Tim Petersf5f6c432001-05-23 07:46:36 +0000188To run the entire test suite:
189
190[UNIX, + other platforms where "make" works] Make the "test" target at the
191top level:
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000192
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000193 make test
194
Barry Warsaw5ca53742002-04-23 21:39:00 +0000195[WINDOWS] Run rt.bat from your PCBuild directory. Read the comments at
Tim Petersf5f6c432001-05-23 07:46:36 +0000196the top of rt.bat for the use of special -d, -O and -q options processed
197by rt.bat.
198
199[OTHER] You can simply execute the two runs of regrtest (optimized and
200non-optimized) directly:
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000201
202 ./python Lib/test/regrtest.py
203 ./python -O Lib/test/regrtest.py
204
Tim Petersf5f6c432001-05-23 07:46:36 +0000205But note that this way picks up whatever .pyc and .pyo files happen to be
206around. The makefile and rt.bat ways run the tests twice, the first time
207removing all .pyc and .pyo files from the subtree rooted at Lib/.
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000208
209Test cases generate output based upon values computed by the test code.
210When executed, regrtest.py compares the actual output generated by executing
211the test case with the expected output and reports success or failure. It
212stands to reason that if the actual and expected outputs are to match, they
213must not contain any machine dependencies. This means your test cases
214should not print out absolute machine addresses (e.g. the return value of
215the id() builtin function) or floating point numbers with large numbers of
216significant digits (unless you understand what you are doing!).
217
218
219Test Case Writing Tips
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000220
221Writing good test cases is a skilled task and is too complex to discuss in
222detail in this short document. Many books have been written on the subject.
223I'll show my age by suggesting that Glenford Myers' "The Art of Software
224Testing", published in 1979, is still the best introduction to the subject
225available. It is short (177 pages), easy to read, and discusses the major
226elements of software testing, though its publication predates the
227object-oriented software revolution, so doesn't cover that subject at all.
228Unfortunately, it is very expensive (about $100 new). If you can borrow it
229or find it used (around $20), I strongly urge you to pick up a copy.
230
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000231The most important goal when writing test cases is to break things. A test
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000232case that doesn't uncover a bug is much less valuable than one that does.
233In designing test cases you should pay attention to the following:
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000234
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000235 * Your test cases should exercise all the functions and objects defined
236 in the module, not just the ones meant to be called by users of your
237 module. This may require you to write test code that uses the module
238 in ways you don't expect (explicitly calling internal functions, for
239 example - see test_atexit.py).
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000240
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000241 * You should consider any boundary values that may tickle exceptional
242 conditions (e.g. if you were writing regression tests for division,
243 you might well want to generate tests with numerators and denominators
244 at the limits of floating point and integer numbers on the machine
245 performing the tests as well as a denominator of zero).
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000246
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000247 * You should exercise as many paths through the code as possible. This
248 may not always be possible, but is a goal to strive for. In
249 particular, when considering if statements (or their equivalent), you
250 want to create test cases that exercise both the true and false
251 branches. For loops, you should create test cases that exercise the
252 loop zero, one and multiple times.
253
254 * You should test with obviously invalid input. If you know that a
255 function requires an integer input, try calling it with other types of
256 objects to see how it responds.
257
258 * You should test with obviously out-of-range input. If the domain of a
259 function is only defined for positive integers, try calling it with a
260 negative integer.
261
262 * If you are going to fix a bug that wasn't uncovered by an existing
263 test, try to write a test case that exposes the bug (preferably before
264 fixing it).
265
Fred Drake44b6bd22000-10-23 16:37:14 +0000266 * If you need to create a temporary file, you can use the filename in
267 test_support.TESTFN to do so. It is important to remove the file
268 when done; other tests should be able to use the name without cleaning
269 up after your test.
270
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000271
272Regression Test Writing Rules
273
274Each test case is different. There is no "standard" form for a Python
Tim Petersf5f6c432001-05-23 07:46:36 +0000275regression test case, though there are some general rules (note that
276these mostly apply only to the "classic" tests; unittest- and doctest-
277based tests should follow the conventions natural to those frameworks):
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000278
279 * If your test case detects a failure, raise TestFailed (found in
Barry Warsaw04f357c2002-07-23 19:04:11 +0000280 test.test_support).
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000281
282 * Import everything you'll need as early as possible.
283
284 * If you'll be importing objects from a module that is at least
285 partially platform-dependent, only import those objects you need for
286 the current test case to avoid spurious ImportError exceptions that
287 prevent the test from running to completion.
288
289 * Print all your test case results using the print statement. For
290 non-fatal errors, print an error message (or omit a successful
291 completion print) to indicate the failure, but proceed instead of
292 raising TestFailed.
293
Tim Petersa48b5262000-08-23 05:28:45 +0000294 * Use "assert" sparingly, if at all. It's usually better to just print
295 what you got, and rely on regrtest's got-vs-expected comparison to
296 catch deviations from what you expect. assert statements aren't
297 executed at all when regrtest is run in -O mode; and, because they
298 cause the test to stop immediately, can lead to a long & tedious
299 test-fix, test-fix, test-fix, ... cycle when things are badly broken
300 (and note that "badly broken" often includes running the test suite
301 for the first time on new platforms or under new implementations of
302 the language).
303
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000304
305Miscellaneous
306
Barry Warsaw1bfab7b2002-07-23 19:13:45 +0000307There is a test_support module in the test package you can import for
Barry Warsaw04f357c2002-07-23 19:04:11 +0000308your test case. Import this module using either
309
310 import test.test_support
311
312or
313
314 from test import test_support
315
316test_support provides the following useful objects:
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000317
318 * TestFailed - raise this exception when your regression test detects a
319 failure.
320
Fred Drake62c53dd2000-08-21 16:55:57 +0000321 * TestSkipped - raise this if the test could not be run because the
322 platform doesn't offer all the required facilities (like large
323 file support), even if all the required modules are available.
324
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000325 * verbose - you can use this variable to control print output. Many
326 modules use it. Search for "verbose" in the test_*.py files to see
327 lots of examples.
328
Tim Petersf5f6c432001-05-23 07:46:36 +0000329 * verify(condition, reason='test failed'). Use this instead of
330
331 assert condition[, reason]
332
333 verify() has two advantages over assert: it works even in -O mode,
334 and it raises TestFailed on failure instead of AssertionError.
335
336 * TESTFN - a string that should always be used as the filename when you
337 need to create a temp file. Also use try/finally to ensure that your
338 temp files are deleted before your test completes. Note that you
339 cannot unlink an open file on all operating systems, so also be sure
340 to close temp files before trying to unlink them.
341
342 * sortdict(dict) - acts like repr(dict.items()), but sorts the items
343 first. This is important when printing a dict value, because the
344 order of items produced by dict.items() is not defined by the
345 language.
346
347 * findfile(file) - you can call this function to locate a file somewhere
348 along sys.path or in the Lib/test tree - see test_linuxaudiodev.py for
349 an example of its use.
350
Tim Petersa48b5262000-08-23 05:28:45 +0000351 * use_large_resources - true iff tests requiring large time or space
352 should be run.
353
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000354 * fcmp(x,y) - you can call this function to compare two floating point
355 numbers when you expect them to only be approximately equal withing a
356 fuzz factor (test_support.FUZZ, which defaults to 1e-6).
357
Tim Petersa48b5262000-08-23 05:28:45 +0000358
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000359Python and C statement coverage results are currently available at
360
361 http://www.musi-cal.com/~skip/python/Python/dist/src/
362
363As of this writing (July, 2000) these results are being generated nightly.
364You can refer to the summaries and the test coverage output files to see
365where coverage is adequate or lacking and write test cases to beef up the
366coverage.
Tim Petersf5f6c432001-05-23 07:46:36 +0000367
368
369Some Non-Obvious regrtest Features
370
371 * Automagic test detection: When you create a new test file
372 test_spam.py, you do not need to modify regrtest (or anything else)
373 to advertise its existence. regrtest searches for and runs all
374 modules in the test directory with names of the form test_xxx.py.
375
376 * Miranda output: If, when running test_spam.py, regrtest does not
377 find an expected-output file test/output/test_spam, regrtest
378 pretends that it did find one, containing the single line
379
380 test_spam
381
382 This allows new tests that don't expect to print anything to stdout
383 to not bother creating expected-output files.
384
385 * Two-stage testing: To run test_spam.py, regrtest imports test_spam
386 as a module. Most tests run to completion as a side-effect of
387 getting imported. After importing test_spam, regrtest also executes
388 test_spam.test_main(), if test_spam has a "test_main" attribute.
Fred Drakeb2ad1c82001-09-28 20:05:25 +0000389 This is rarely required with the "traditional" Python tests, and
390 you shouldn't create a module global with name test_main unless
391 you're specifically exploiting this gimmick. This usage does
392 prove useful with PyUnit-based tests as well, however; defining
393 a test_main() which is run by regrtest and a script-stub in the
394 test module ("if __name__ == '__main__': test_main()") allows
395 the test to be used like any other Python test and also work
396 with the unittest.py-as-a-script approach, allowing a developer
397 to run specific tests from the command line.