blob: fdc847c785c13bea544638090c360e24314858ba [file] [log] [blame]
Neal Norwitzdeaba572002-11-27 15:47:10 +00001+++++++++++++++++++++++++++++++
2Writing Python Regression Tests
3+++++++++++++++++++++++++++++++
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +00004
Neal Norwitzdeaba572002-11-27 15:47:10 +00005:Author: Skip Montanaro
Christian Heimes895627f2007-12-08 17:28:33 +00006:Contact: skip@pobox.com
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +00007
8Introduction
Neal Norwitzdeaba572002-11-27 15:47:10 +00009============
Skip Montanaro47c60ec2000-06-30 06:08:35 +000010
11If you add a new module to Python or modify the functionality of an existing
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +000012module, you should write one or more test cases to exercise that new
Fred Drakea6daad22001-05-23 04:57:49 +000013functionality. There are different ways to do this within the regression
14testing facility provided with Python; any particular test should use only
15one of these options. Each option requires writing a test module using the
Walter Dörwald88af4df2003-02-03 20:22:27 +000016conventions of the selected option:
Skip Montanaro47c60ec2000-06-30 06:08:35 +000017
Guido van Rossumd8faa362007-04-27 19:54:29 +000018 - unittest_ based tests
Neal Norwitzdeaba572002-11-27 15:47:10 +000019 - doctest_ based tests
Fred Drakea6daad22001-05-23 04:57:49 +000020 - "traditional" Python test modules
21
22Regardless of the mechanics of the testing approach you choose,
23you will be writing unit tests (isolated tests of functions and objects
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +000024defined by the module) using white box techniques. Unlike black box
25testing, where you only have the external interfaces to guide your test case
26writing, in white box testing you can see the code being tested and tailor
27your test cases to exercise it more completely. In particular, you will be
28able to refer to the C and Python code in the CVS repository when writing
29your regression test cases.
Skip Montanaro47c60ec2000-06-30 06:08:35 +000030
Neal Norwitzdeaba572002-11-27 15:47:10 +000031.. _unittest: http://www.python.org/doc/current/lib/module-unittest.html
32.. _doctest: http://www.python.org/doc/current/lib/module-doctest.html
Tim Petersf5f6c432001-05-23 07:46:36 +000033
Guido van Rossumd8faa362007-04-27 19:54:29 +000034unittest-based tests
Neal Norwitzdeaba572002-11-27 15:47:10 +000035------------------
Guido van Rossumd8faa362007-04-27 19:54:29 +000036The unittest_ framework is based on the ideas of unit testing as espoused
Neal Norwitzdeaba572002-11-27 15:47:10 +000037by Kent Beck and the `Extreme Programming`_ (XP) movement. The specific
38interface provided by the framework is tightly based on the JUnit_
Fred Drakea6daad22001-05-23 04:57:49 +000039Java implementation of Beck's original SmallTalk test framework. Please
Neal Norwitzdeaba572002-11-27 15:47:10 +000040see the documentation of the unittest_ module for detailed information on
Guido van Rossumd8faa362007-04-27 19:54:29 +000041the interface and general guidelines on writing unittest-based tests.
Fred Drakea6daad22001-05-23 04:57:49 +000042
Guido van Rossumd8faa362007-04-27 19:54:29 +000043The test_support helper module provides a function for use by
44unittest-based tests in the Python regression testing framework,
45``run_unittest()``. This is the primary way of running tests in the
46standard library. You can pass it any number of the following:
Neal Norwitzdeaba572002-11-27 15:47:10 +000047
Guido van Rossumd8faa362007-04-27 19:54:29 +000048- classes derived from or instances of ``unittest.TestCase`` or
49 ``unittest.TestSuite``. These will be handed off to unittest for
50 converting into a proper TestSuite instance.
51
52- a string; this must be a key in sys.modules. The module associated with
53 that string will be scanned by ``unittest.TestLoader.loadTestsFromModule``.
54 This is usually seen as ``test_support.run_unittest(__name__)`` in a test
55 module's ``test_main()`` function. This has the advantage of picking up
56 new tests automatically, without you having to add each new test case
57 manually.
Neal Norwitzdeaba572002-11-27 15:47:10 +000058
Barry Warsaw5ca53742002-04-23 21:39:00 +000059All test methods in the Python regression framework have names that
Neal Norwitzdeaba572002-11-27 15:47:10 +000060start with "``test_``" and use lower-case names with words separated with
Barry Warsaw5ca53742002-04-23 21:39:00 +000061underscores.
Fred Drakeb2ad1c82001-09-28 20:05:25 +000062
Guido van Rossum1c486542002-08-22 20:08:14 +000063Test methods should *not* have docstrings! The unittest module prints
64the docstring if there is one, but otherwise prints the function name
65and the full class name. When there's a problem with a test, the
66latter information makes it easier to find the source for the test
67than the docstring.
68
Guido van Rossumd8faa362007-04-27 19:54:29 +000069All unittest-based tests in the Python test suite use boilerplate that
Neal Norwitzdeaba572002-11-27 15:47:10 +000070looks like this (with minor variations)::
Fred Drakeb2ad1c82001-09-28 20:05:25 +000071
72 import unittest
Barry Warsaw04f357c2002-07-23 19:04:11 +000073 from test import test_support
Fred Drakeb2ad1c82001-09-28 20:05:25 +000074
Barry Warsaw5ca53742002-04-23 21:39:00 +000075 class MyTestCase1(unittest.TestCase):
Guido van Rossum1c486542002-08-22 20:08:14 +000076
77 # Define setUp and tearDown only if needed
78
79 def setUp(self):
80 unittest.TestCase.setUp(self)
81 ... additional initialization...
82
83 def tearDown(self):
84 ... additional finalization...
85 unittest.TestCase.tearDown(self)
86
87 def test_feature_one(self):
88 # Testing feature one
89 ...unit test for feature one...
90
91 def test_feature_two(self):
92 # Testing feature two
93 ...unit test for feature two...
94
95 ...etc...
Fred Drakeb2ad1c82001-09-28 20:05:25 +000096
Barry Warsaw5ca53742002-04-23 21:39:00 +000097 class MyTestCase2(unittest.TestCase):
Guido van Rossum1c486542002-08-22 20:08:14 +000098 ...same structure as MyTestCase1...
99
100 ...etc...
Barry Warsaw5ca53742002-04-23 21:39:00 +0000101
Fred Drakeb2ad1c82001-09-28 20:05:25 +0000102 def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +0000103 test_support.run_unittest(__name__)
Fred Drakeb2ad1c82001-09-28 20:05:25 +0000104
105 if __name__ == "__main__":
106 test_main()
107
108This has the advantage that it allows the unittest module to be used
109as a script to run individual tests as well as working well with the
110regrtest framework.
Fred Drakea6daad22001-05-23 04:57:49 +0000111
Neal Norwitzdeaba572002-11-27 15:47:10 +0000112.. _Extreme Programming: http://www.extremeprogramming.org/
113.. _JUnit: http://www.junit.org/
Tim Petersf5f6c432001-05-23 07:46:36 +0000114
Fred Drakea6daad22001-05-23 04:57:49 +0000115doctest based tests
Neal Norwitzdeaba572002-11-27 15:47:10 +0000116-------------------
117Tests written to use doctest_ are actually part of the docstrings for
Fred Drakea6daad22001-05-23 04:57:49 +0000118the module being tested. Each test is written as a display of an
119interactive session, including the Python prompts, statements that would
120be typed by the user, and the output of those statements (including
Tim Petersf5f6c432001-05-23 07:46:36 +0000121tracebacks, although only the exception msg needs to be retained then).
122The module in the test package is simply a wrapper that causes doctest
123to run over the tests in the module. The test for the difflib module
Neal Norwitzdeaba572002-11-27 15:47:10 +0000124provides a convenient example::
Fred Drakea6daad22001-05-23 04:57:49 +0000125
Barry Warsaw1bfab7b2002-07-23 19:13:45 +0000126 import difflib
127 from test import test_support
Tim Petersa0a62222001-09-09 06:12:01 +0000128 test_support.run_doctest(difflib)
Tim Petersf5f6c432001-05-23 07:46:36 +0000129
130If the test is successful, nothing is written to stdout (so you should not
131create a corresponding output/test_difflib file), but running regrtest
Tim Petersa0a62222001-09-09 06:12:01 +0000132with -v will give a detailed report, the same as if passing -v to doctest.
133
134A second argument can be passed to run_doctest to tell doctest to search
Neal Norwitzdeaba572002-11-27 15:47:10 +0000135``sys.argv`` for -v instead of using test_support's idea of verbosity. This
Tim Petersa0a62222001-09-09 06:12:01 +0000136is useful for writing doctest-based tests that aren't simply running a
137doctest'ed Lib module, but contain the doctests themselves. Then at
138times you may want to run such a test directly as a doctest, independent
139of the regrtest framework. The tail end of test_descrtut.py is a good
Neal Norwitzdeaba572002-11-27 15:47:10 +0000140example::
Tim Petersa0a62222001-09-09 06:12:01 +0000141
142 def test_main(verbose=None):
Barry Warsaw1bfab7b2002-07-23 19:13:45 +0000143 from test import test_support, test_descrtut
144 test_support.run_doctest(test_descrtut, verbose)
Tim Petersa0a62222001-09-09 06:12:01 +0000145
146 if __name__ == "__main__":
147 test_main(1)
148
Neal Norwitzdeaba572002-11-27 15:47:10 +0000149If run via regrtest, ``test_main()`` is called (by regrtest) without
150specifying verbose, and then test_support's idea of verbosity is used. But
151when run directly, ``test_main(1)`` is called, and then doctest's idea of
152verbosity is used.
Fred Drakea6daad22001-05-23 04:57:49 +0000153
154See the documentation for the doctest module for information on
155writing tests using the doctest framework.
156
157"traditional" Python test modules
Neal Norwitzdeaba572002-11-27 15:47:10 +0000158---------------------------------
Fred Drakea6daad22001-05-23 04:57:49 +0000159The mechanics of how the "traditional" test system operates are fairly
160straightforward. When a test case is run, the output is compared with the
161expected output that is stored in .../Lib/test/output. If the test runs to
162completion and the actual and expected outputs match, the test succeeds, if
Neal Norwitzdeaba572002-11-27 15:47:10 +0000163not, it fails. If an ``ImportError`` or ``test_support.TestSkipped`` error
164is raised, the test is not run.
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000165
166Executing Test Cases
Neal Norwitzdeaba572002-11-27 15:47:10 +0000167====================
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000168If you are writing test cases for module spam, you need to create a file
Tim Petersf5f6c432001-05-23 07:46:36 +0000169in .../Lib/test named test_spam.py. In addition, if the tests are expected
170to write to stdout during a successful run, you also need to create an
171expected output file in .../Lib/test/output named test_spam ("..."
172represents the top-level directory in the Python source tree, the directory
173containing the configure script). If needed, generate the initial version
Neal Norwitzdeaba572002-11-27 15:47:10 +0000174of the test output file by executing::
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000175
176 ./python Lib/test/regrtest.py -g test_spam.py
177
Tim Petersf5f6c432001-05-23 07:46:36 +0000178from the top-level directory.
Fred Drakea6daad22001-05-23 04:57:49 +0000179
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000180Any time you modify test_spam.py you need to generate a new expected
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000181output file. Don't forget to desk check the generated output to make sure
Tim Petersf5f6c432001-05-23 07:46:36 +0000182it's really what you expected to find! All in all it's usually better
183not to have an expected-out file (note that doctest- and unittest-based
184tests do not).
185
186To run a single test after modifying a module, simply run regrtest.py
Neal Norwitzdeaba572002-11-27 15:47:10 +0000187without the -g flag::
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000188
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000189 ./python Lib/test/regrtest.py test_spam.py
190
191While debugging a regression test, you can of course execute it
Neal Norwitzdeaba572002-11-27 15:47:10 +0000192independently of the regression testing framework and see what it prints::
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000193
194 ./python Lib/test/test_spam.py
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000195
Tim Petersf5f6c432001-05-23 07:46:36 +0000196To run the entire test suite:
197
Neal Norwitzdeaba572002-11-27 15:47:10 +0000198- [UNIX, + other platforms where "make" works] Make the "test" target at the
199 top level::
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000200
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000201 make test
202
Neal Norwitzdeaba572002-11-27 15:47:10 +0000203- [WINDOWS] Run rt.bat from your PCBuild directory. Read the comments at
204 the top of rt.bat for the use of special -d, -O and -q options processed
205 by rt.bat.
Tim Petersf5f6c432001-05-23 07:46:36 +0000206
Neal Norwitzdeaba572002-11-27 15:47:10 +0000207- [OTHER] You can simply execute the two runs of regrtest (optimized and
208 non-optimized) directly::
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000209
210 ./python Lib/test/regrtest.py
211 ./python -O Lib/test/regrtest.py
212
Tim Petersf5f6c432001-05-23 07:46:36 +0000213But note that this way picks up whatever .pyc and .pyo files happen to be
214around. The makefile and rt.bat ways run the tests twice, the first time
215removing all .pyc and .pyo files from the subtree rooted at Lib/.
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000216
217Test cases generate output based upon values computed by the test code.
218When executed, regrtest.py compares the actual output generated by executing
219the test case with the expected output and reports success or failure. It
220stands to reason that if the actual and expected outputs are to match, they
221must not contain any machine dependencies. This means your test cases
222should not print out absolute machine addresses (e.g. the return value of
223the id() builtin function) or floating point numbers with large numbers of
224significant digits (unless you understand what you are doing!).
225
226
227Test Case Writing Tips
Neal Norwitzdeaba572002-11-27 15:47:10 +0000228======================
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000229Writing good test cases is a skilled task and is too complex to discuss in
230detail in this short document. Many books have been written on the subject.
Neal Norwitzdeaba572002-11-27 15:47:10 +0000231I'll show my age by suggesting that Glenford Myers' `"The Art of Software
232Testing"`_, published in 1979, is still the best introduction to the subject
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000233available. It is short (177 pages), easy to read, and discusses the major
234elements of software testing, though its publication predates the
235object-oriented software revolution, so doesn't cover that subject at all.
236Unfortunately, it is very expensive (about $100 new). If you can borrow it
237or find it used (around $20), I strongly urge you to pick up a copy.
238
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000239The most important goal when writing test cases is to break things. A test
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000240case that doesn't uncover a bug is much less valuable than one that does.
241In designing test cases you should pay attention to the following:
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000242
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000243 * Your test cases should exercise all the functions and objects defined
244 in the module, not just the ones meant to be called by users of your
245 module. This may require you to write test code that uses the module
246 in ways you don't expect (explicitly calling internal functions, for
247 example - see test_atexit.py).
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000248
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000249 * You should consider any boundary values that may tickle exceptional
250 conditions (e.g. if you were writing regression tests for division,
251 you might well want to generate tests with numerators and denominators
252 at the limits of floating point and integer numbers on the machine
253 performing the tests as well as a denominator of zero).
Skip Montanaro47c60ec2000-06-30 06:08:35 +0000254
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000255 * You should exercise as many paths through the code as possible. This
256 may not always be possible, but is a goal to strive for. In
257 particular, when considering if statements (or their equivalent), you
258 want to create test cases that exercise both the true and false
259 branches. For loops, you should create test cases that exercise the
260 loop zero, one and multiple times.
261
262 * You should test with obviously invalid input. If you know that a
263 function requires an integer input, try calling it with other types of
264 objects to see how it responds.
265
266 * You should test with obviously out-of-range input. If the domain of a
267 function is only defined for positive integers, try calling it with a
268 negative integer.
269
270 * If you are going to fix a bug that wasn't uncovered by an existing
271 test, try to write a test case that exposes the bug (preferably before
272 fixing it).
273
Fred Drake44b6bd22000-10-23 16:37:14 +0000274 * If you need to create a temporary file, you can use the filename in
Neal Norwitzdeaba572002-11-27 15:47:10 +0000275 ``test_support.TESTFN`` to do so. It is important to remove the file
Fred Drake44b6bd22000-10-23 16:37:14 +0000276 when done; other tests should be able to use the name without cleaning
277 up after your test.
278
Neal Norwitzdeaba572002-11-27 15:47:10 +0000279.. _"The Art of Software Testing":
280 http://www.amazon.com/exec/obidos/ISBN=0471043281
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000281
282Regression Test Writing Rules
Neal Norwitzdeaba572002-11-27 15:47:10 +0000283=============================
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000284Each test case is different. There is no "standard" form for a Python
Tim Petersf5f6c432001-05-23 07:46:36 +0000285regression test case, though there are some general rules (note that
Neal Norwitzdeaba572002-11-27 15:47:10 +0000286these mostly apply only to the "classic" tests; unittest_- and doctest_-
287based tests should follow the conventions natural to those frameworks)::
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000288
Neal Norwitzdeaba572002-11-27 15:47:10 +0000289 * If your test case detects a failure, raise ``TestFailed`` (found in
290 ``test.test_support``).
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000291
292 * Import everything you'll need as early as possible.
293
294 * If you'll be importing objects from a module that is at least
295 partially platform-dependent, only import those objects you need for
Neal Norwitzdeaba572002-11-27 15:47:10 +0000296 the current test case to avoid spurious ``ImportError`` exceptions
297 that prevent the test from running to completion.
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000298
Neal Norwitzdeaba572002-11-27 15:47:10 +0000299 * Print all your test case results using the ``print`` statement. For
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000300 non-fatal errors, print an error message (or omit a successful
301 completion print) to indicate the failure, but proceed instead of
Neal Norwitzdeaba572002-11-27 15:47:10 +0000302 raising ``TestFailed``.
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000303
Neal Norwitzdeaba572002-11-27 15:47:10 +0000304 * Use ``assert`` sparingly, if at all. It's usually better to just print
Tim Petersa48b5262000-08-23 05:28:45 +0000305 what you got, and rely on regrtest's got-vs-expected comparison to
Neal Norwitzdeaba572002-11-27 15:47:10 +0000306 catch deviations from what you expect. ``assert`` statements aren't
Tim Petersa48b5262000-08-23 05:28:45 +0000307 executed at all when regrtest is run in -O mode; and, because they
308 cause the test to stop immediately, can lead to a long & tedious
309 test-fix, test-fix, test-fix, ... cycle when things are badly broken
310 (and note that "badly broken" often includes running the test suite
311 for the first time on new platforms or under new implementations of
312 the language).
313
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000314Miscellaneous
Neal Norwitzdeaba572002-11-27 15:47:10 +0000315=============
Barry Warsaw1bfab7b2002-07-23 19:13:45 +0000316There is a test_support module in the test package you can import for
Neal Norwitzdeaba572002-11-27 15:47:10 +0000317your test case. Import this module using either::
Barry Warsaw04f357c2002-07-23 19:04:11 +0000318
319 import test.test_support
320
Neal Norwitzdeaba572002-11-27 15:47:10 +0000321or::
Barry Warsaw04f357c2002-07-23 19:04:11 +0000322
323 from test import test_support
324
325test_support provides the following useful objects:
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000326
Neal Norwitzdeaba572002-11-27 15:47:10 +0000327 * ``TestFailed`` - raise this exception when your regression test detects
328 a failure.
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000329
Neal Norwitzdeaba572002-11-27 15:47:10 +0000330 * ``TestSkipped`` - raise this if the test could not be run because the
Fred Drake62c53dd2000-08-21 16:55:57 +0000331 platform doesn't offer all the required facilities (like large
332 file support), even if all the required modules are available.
333
Brett Cannone6b70332003-04-30 01:42:35 +0000334 * ``ResourceDenied`` - this is raised when a test requires a resource that
335 is not available. Primarily used by 'requires'.
336
Neal Norwitzdeaba572002-11-27 15:47:10 +0000337 * ``verbose`` - you can use this variable to control print output. Many
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000338 modules use it. Search for "verbose" in the test_*.py files to see
339 lots of examples.
340
Brett Cannone6b70332003-04-30 01:42:35 +0000341 * ``forget(module_name)`` - attempts to cause Python to "forget" that it
342 loaded a module and erase any PYC files.
343
344 * ``is_resource_enabled(resource)`` - Returns a boolean based on whether
345 the resource is enabled or not.
346
347 * ``requires(resource [, msg])`` - if the required resource is not
348 available the ResourceDenied exception is raised.
349
Neal Norwitzdeaba572002-11-27 15:47:10 +0000350 * ``verify(condition, reason='test failed')``. Use this instead of::
Tim Petersf5f6c432001-05-23 07:46:36 +0000351
352 assert condition[, reason]
353
Neal Norwitzdeaba572002-11-27 15:47:10 +0000354 ``verify()`` has two advantages over ``assert``: it works even in -O
355 mode, and it raises ``TestFailed`` on failure instead of
356 ``AssertionError``.
Tim Petersf5f6c432001-05-23 07:46:36 +0000357
Brett Cannone6b70332003-04-30 01:42:35 +0000358 * ``have_unicode`` - true if Unicode is available, false otherwise.
359
360 * ``is_jython`` - true if the interpreter is Jython, false otherwise.
361
Neal Norwitzdeaba572002-11-27 15:47:10 +0000362 * ``TESTFN`` - a string that should always be used as the filename when
363 you need to create a temp file. Also use ``try``/``finally`` to
364 ensure that your temp files are deleted before your test completes.
365 Note that you cannot unlink an open file on all operating systems, so
366 also be sure to close temp files before trying to unlink them.
Tim Petersf5f6c432001-05-23 07:46:36 +0000367
Neal Norwitzdeaba572002-11-27 15:47:10 +0000368 * ``sortdict(dict)`` - acts like ``repr(dict.items())``, but sorts the
369 items first. This is important when printing a dict value, because
370 the order of items produced by ``dict.items()`` is not defined by the
Tim Petersf5f6c432001-05-23 07:46:36 +0000371 language.
372
Neal Norwitzdeaba572002-11-27 15:47:10 +0000373 * ``findfile(file)`` - you can call this function to locate a file
374 somewhere along sys.path or in the Lib/test tree - see
Guido van Rossum33d26892007-08-05 15:29:28 +0000375 test_ossaudiodev.py for an example of its use.
Tim Petersf5f6c432001-05-23 07:46:36 +0000376
Neal Norwitzdeaba572002-11-27 15:47:10 +0000377 * ``fcmp(x,y)`` - you can call this function to compare two floating
378 point numbers when you expect them to only be approximately equal
379 withing a fuzz factor (``test_support.FUZZ``, which defaults to 1e-6).
Skip Montanaroe9e5dcd2000-07-19 17:19:49 +0000380
Thomas Wouters89f507f2006-12-13 04:49:30 +0000381 * ``check_syntax_error(testcase, statement)`` - make sure that the
382 statement is *not* correct Python syntax.
Brett Cannone6b70332003-04-30 01:42:35 +0000383
Tim Petersa48b5262000-08-23 05:28:45 +0000384
Tim Petersf5f6c432001-05-23 07:46:36 +0000385Some Non-Obvious regrtest Features
Neal Norwitzdeaba572002-11-27 15:47:10 +0000386==================================
Tim Petersf5f6c432001-05-23 07:46:36 +0000387 * Automagic test detection: When you create a new test file
388 test_spam.py, you do not need to modify regrtest (or anything else)
389 to advertise its existence. regrtest searches for and runs all
390 modules in the test directory with names of the form test_xxx.py.
391
392 * Miranda output: If, when running test_spam.py, regrtest does not
393 find an expected-output file test/output/test_spam, regrtest
394 pretends that it did find one, containing the single line
395
396 test_spam
397
398 This allows new tests that don't expect to print anything to stdout
399 to not bother creating expected-output files.
400
401 * Two-stage testing: To run test_spam.py, regrtest imports test_spam
402 as a module. Most tests run to completion as a side-effect of
403 getting imported. After importing test_spam, regrtest also executes
Neal Norwitzdeaba572002-11-27 15:47:10 +0000404 ``test_spam.test_main()``, if test_spam has a ``test_main`` attribute.
Fred Drakeb2ad1c82001-09-28 20:05:25 +0000405 This is rarely required with the "traditional" Python tests, and
406 you shouldn't create a module global with name test_main unless
407 you're specifically exploiting this gimmick. This usage does
Guido van Rossumd8faa362007-04-27 19:54:29 +0000408 prove useful with unittest-based tests as well, however; defining
Neal Norwitzdeaba572002-11-27 15:47:10 +0000409 a ``test_main()`` which is run by regrtest and a script-stub in the
410 test module ("``if __name__ == '__main__': test_main()``") allows
Fred Drakeb2ad1c82001-09-28 20:05:25 +0000411 the test to be used like any other Python test and also work
412 with the unittest.py-as-a-script approach, allowing a developer
413 to run specific tests from the command line.