Brett Cannon | f1cfb62 | 2003-05-04 21:15:27 +0000 | [diff] [blame] | 1 | """Supporting definitions for the Python regression tests.""" |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 2 | |
Barry Warsaw | 408b6d3 | 2002-07-30 23:27:12 +0000 | [diff] [blame] | 3 | if __name__ != 'test.test_support': |
| 4 | raise ImportError, 'test_support must be imported from the test package' |
| 5 | |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 6 | import sys |
| 7 | |
Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 8 | class Error(Exception): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 9 | """Base class for regression test exceptions.""" |
Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 10 | |
| 11 | class TestFailed(Error): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 12 | """Test failed.""" |
Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 13 | |
| 14 | class TestSkipped(Error): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 15 | """Test skipped. |
Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 16 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 17 | This can be raised to indicate that a test was deliberatly |
| 18 | skipped, but not because a feature wasn't available. For |
| 19 | example, if some resource can't be used, such as the network |
| 20 | appears to be unavailable, this should be raised instead of |
| 21 | TestFailed. |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 22 | """ |
Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 23 | |
Fred Drake | 9a0db07 | 2003-02-03 15:19:30 +0000 | [diff] [blame] | 24 | class ResourceDenied(TestSkipped): |
| 25 | """Test skipped because it requested a disallowed resource. |
| 26 | |
| 27 | This is raised when a test calls requires() for a resource that |
| 28 | has not be enabled. It is used to distinguish between expected |
| 29 | and unexpected skips. |
| 30 | """ |
| 31 | |
Barry Warsaw | c0fb605 | 2001-08-20 22:29:23 +0000 | [diff] [blame] | 32 | verbose = 1 # Flag set to 0 by regrtest.py |
Guido van Rossum | fe3f696 | 2001-09-06 16:09:41 +0000 | [diff] [blame] | 33 | use_resources = None # Flag set to [] by regrtest.py |
Guido van Rossum | 531661c | 1996-12-20 02:58:22 +0000 | [diff] [blame] | 34 | |
Tim Peters | 8dee809 | 2001-09-25 20:05:11 +0000 | [diff] [blame] | 35 | # _original_stdout is meant to hold stdout at the time regrtest began. |
| 36 | # This may be "the real" stdout, or IDLE's emulation of stdout, or whatever. |
| 37 | # The point is to have some flavor of stdout the user can actually see. |
| 38 | _original_stdout = None |
| 39 | def record_original_stdout(stdout): |
| 40 | global _original_stdout |
| 41 | _original_stdout = stdout |
| 42 | |
| 43 | def get_original_stdout(): |
| 44 | return _original_stdout or sys.stdout |
| 45 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 46 | def unload(name): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 47 | try: |
| 48 | del sys.modules[name] |
| 49 | except KeyError: |
| 50 | pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 51 | |
Neal Norwitz | 0e17f8c | 2006-01-23 07:51:27 +0000 | [diff] [blame] | 52 | def unlink(filename): |
| 53 | import os |
| 54 | try: |
| 55 | os.unlink(filename) |
| 56 | except OSError: |
| 57 | pass |
| 58 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 59 | def forget(modname): |
Brett Cannon | f1cfb62 | 2003-05-04 21:15:27 +0000 | [diff] [blame] | 60 | '''"Forget" a module was ever imported by removing it from sys.modules and |
| 61 | deleting any .pyc and .pyo files.''' |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 62 | unload(modname) |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 63 | import os |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 64 | for dirname in sys.path: |
Neal Norwitz | 0e17f8c | 2006-01-23 07:51:27 +0000 | [diff] [blame] | 65 | unlink(os.path.join(dirname, modname + os.extsep + 'pyc')) |
Brett Cannon | f1cfb62 | 2003-05-04 21:15:27 +0000 | [diff] [blame] | 66 | # Deleting the .pyo file cannot be within the 'try' for the .pyc since |
| 67 | # the chance exists that there is no .pyc (and thus the 'try' statement |
| 68 | # is exited) but there is a .pyo file. |
Neal Norwitz | 0e17f8c | 2006-01-23 07:51:27 +0000 | [diff] [blame] | 69 | unlink(os.path.join(dirname, modname + os.extsep + 'pyo')) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 70 | |
Tim Peters | b4ee4eb | 2002-12-04 03:26:57 +0000 | [diff] [blame] | 71 | def is_resource_enabled(resource): |
Brett Cannon | f1cfb62 | 2003-05-04 21:15:27 +0000 | [diff] [blame] | 72 | """Test whether a resource is enabled. Known resources are set by |
| 73 | regrtest.py.""" |
Tim Peters | b4ee4eb | 2002-12-04 03:26:57 +0000 | [diff] [blame] | 74 | return use_resources is not None and resource in use_resources |
| 75 | |
Barry Warsaw | c0fb605 | 2001-08-20 22:29:23 +0000 | [diff] [blame] | 76 | def requires(resource, msg=None): |
Brett Cannon | f1cfb62 | 2003-05-04 21:15:27 +0000 | [diff] [blame] | 77 | """Raise ResourceDenied if the specified resource is not available. |
| 78 | |
| 79 | If the caller's module is __main__ then automatically return True. The |
| 80 | possibility of False being returned occurs when regrtest.py is executing.""" |
Skip Montanaro | d839ecd | 2003-04-24 19:06:57 +0000 | [diff] [blame] | 81 | # see if the caller's module is __main__ - if so, treat as if |
| 82 | # the resource was set |
| 83 | if sys._getframe().f_back.f_globals.get("__name__") == "__main__": |
| 84 | return |
Tim Peters | b4ee4eb | 2002-12-04 03:26:57 +0000 | [diff] [blame] | 85 | if not is_resource_enabled(resource): |
Barry Warsaw | c0fb605 | 2001-08-20 22:29:23 +0000 | [diff] [blame] | 86 | if msg is None: |
| 87 | msg = "Use of the `%s' resource not enabled" % resource |
Fred Drake | 9a0db07 | 2003-02-03 15:19:30 +0000 | [diff] [blame] | 88 | raise ResourceDenied(msg) |
Barry Warsaw | c0fb605 | 2001-08-20 22:29:23 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 90 | FUZZ = 1e-6 |
| 91 | |
| 92 | def fcmp(x, y): # fuzzy comparison function |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 93 | if type(x) == type(0.0) or type(y) == type(0.0): |
| 94 | try: |
| 95 | x, y = coerce(x, y) |
| 96 | fuzz = (abs(x) + abs(y)) * FUZZ |
| 97 | if abs(x-y) <= fuzz: |
| 98 | return 0 |
| 99 | except: |
| 100 | pass |
| 101 | elif type(x) == type(y) and type(x) in (type(()), type([])): |
| 102 | for i in range(min(len(x), len(y))): |
| 103 | outcome = fcmp(x[i], y[i]) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 104 | if outcome != 0: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 105 | return outcome |
| 106 | return cmp(len(x), len(y)) |
| 107 | return cmp(x, y) |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 108 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 109 | try: |
| 110 | unicode |
| 111 | have_unicode = 1 |
| 112 | except NameError: |
| 113 | have_unicode = 0 |
| 114 | |
Finn Bock | 57bc5fa | 2002-11-01 18:02:03 +0000 | [diff] [blame] | 115 | is_jython = sys.platform.startswith('java') |
| 116 | |
Guido van Rossum | a8f7e59 | 2001-03-13 09:31:07 +0000 | [diff] [blame] | 117 | import os |
Barry Warsaw | 559f668 | 2001-03-23 18:04:02 +0000 | [diff] [blame] | 118 | # Filename used for testing |
| 119 | if os.name == 'java': |
| 120 | # Jython disallows @ in module names |
| 121 | TESTFN = '$test' |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 122 | elif os.name == 'riscos': |
| 123 | TESTFN = 'testfile' |
| 124 | else: |
Barry Warsaw | 559f668 | 2001-03-23 18:04:02 +0000 | [diff] [blame] | 125 | TESTFN = '@test' |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 126 | # Unicode name only used if TEST_FN_ENCODING exists for the platform. |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 127 | if have_unicode: |
Mark Hammond | b337dd9 | 2003-12-03 01:27:23 +0000 | [diff] [blame] | 128 | # Assuming sys.getfilesystemencoding()!=sys.getdefaultencoding() |
| 129 | # TESTFN_UNICODE is a filename that can be encoded using the |
| 130 | # file system encoding, but *not* with the default (ascii) encoding |
Martin v. Löwis | 2411a2d | 2002-11-09 19:57:26 +0000 | [diff] [blame] | 131 | if isinstance('', unicode): |
| 132 | # python -U |
| 133 | # XXX perhaps unicode() should accept Unicode strings? |
Tim Peters | c6c5ece | 2003-12-04 05:39:43 +0000 | [diff] [blame] | 134 | TESTFN_UNICODE = "@test-\xe0\xf2" |
Martin v. Löwis | 2411a2d | 2002-11-09 19:57:26 +0000 | [diff] [blame] | 135 | else: |
Tim Peters | c6c5ece | 2003-12-04 05:39:43 +0000 | [diff] [blame] | 136 | # 2 latin characters. |
| 137 | TESTFN_UNICODE = unicode("@test-\xe0\xf2", "latin-1") |
| 138 | TESTFN_ENCODING = sys.getfilesystemencoding() |
| 139 | # TESTFN_UNICODE_UNENCODEABLE is a filename that should *not* be |
Mark Hammond | b337dd9 | 2003-12-03 01:27:23 +0000 | [diff] [blame] | 140 | # able to be encoded by *either* the default or filesystem encoding. |
Tim Peters | c6c5ece | 2003-12-04 05:39:43 +0000 | [diff] [blame] | 141 | # This test really only makes sense on Windows NT platforms |
Mark Hammond | 2e8624c | 2003-12-03 22:16:47 +0000 | [diff] [blame] | 142 | # which have special Unicode support in posixmodule. |
Tim Peters | c6c5ece | 2003-12-04 05:39:43 +0000 | [diff] [blame] | 143 | if (not hasattr(sys, "getwindowsversion") or |
| 144 | sys.getwindowsversion()[3] < 2): # 0=win32s or 1=9x/ME |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 145 | TESTFN_UNICODE_UNENCODEABLE = None |
Mark Hammond | b337dd9 | 2003-12-03 01:27:23 +0000 | [diff] [blame] | 146 | else: |
Mark Hammond | 2e8624c | 2003-12-03 22:16:47 +0000 | [diff] [blame] | 147 | # Japanese characters (I think - from bug 846133) |
Martin v. Löwis | e2713be | 2005-03-08 15:03:08 +0000 | [diff] [blame] | 148 | TESTFN_UNICODE_UNENCODEABLE = eval('u"@test-\u5171\u6709\u3055\u308c\u308b"') |
Mark Hammond | 2e8624c | 2003-12-03 22:16:47 +0000 | [diff] [blame] | 149 | try: |
| 150 | # XXX - Note - should be using TESTFN_ENCODING here - but for |
Tim Peters | c6c5ece | 2003-12-04 05:39:43 +0000 | [diff] [blame] | 151 | # Windows, "mbcs" currently always operates as if in |
Mark Hammond | 2e8624c | 2003-12-03 22:16:47 +0000 | [diff] [blame] | 152 | # errors=ignore' mode - hence we get '?' characters rather than |
| 153 | # the exception. 'Latin1' operates as we expect - ie, fails. |
| 154 | # See [ 850997 ] mbcs encoding ignores errors |
| 155 | TESTFN_UNICODE_UNENCODEABLE.encode("Latin1") |
| 156 | except UnicodeEncodeError: |
| 157 | pass |
| 158 | else: |
| 159 | print \ |
| 160 | 'WARNING: The filename %r CAN be encoded by the filesystem. ' \ |
| 161 | 'Unicode filename tests may not be effective' \ |
| 162 | % TESTFN_UNICODE_UNENCODEABLE |
Neal Norwitz | 26a1eef | 2002-11-03 00:35:53 +0000 | [diff] [blame] | 163 | |
| 164 | # Make sure we can write to TESTFN, try in /tmp if we can't |
| 165 | fp = None |
| 166 | try: |
| 167 | fp = open(TESTFN, 'w+') |
| 168 | except IOError: |
| 169 | TMP_TESTFN = os.path.join('/tmp', TESTFN) |
| 170 | try: |
| 171 | fp = open(TMP_TESTFN, 'w+') |
| 172 | TESTFN = TMP_TESTFN |
| 173 | del TMP_TESTFN |
| 174 | except IOError: |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 175 | print ('WARNING: tests will fail, unable to write to: %s or %s' % |
Neal Norwitz | 26a1eef | 2002-11-03 00:35:53 +0000 | [diff] [blame] | 176 | (TESTFN, TMP_TESTFN)) |
| 177 | if fp is not None: |
| 178 | fp.close() |
Neal Norwitz | 0e17f8c | 2006-01-23 07:51:27 +0000 | [diff] [blame] | 179 | unlink(TESTFN) |
Neal Norwitz | 26a1eef | 2002-11-03 00:35:53 +0000 | [diff] [blame] | 180 | del os, fp |
Guido van Rossum | a8f7e59 | 2001-03-13 09:31:07 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | e26132c | 1998-04-23 20:13:30 +0000 | [diff] [blame] | 182 | def findfile(file, here=__file__): |
Brett Cannon | f1cfb62 | 2003-05-04 21:15:27 +0000 | [diff] [blame] | 183 | """Try to find a file on sys.path and the working directory. If it is not |
| 184 | found the argument passed to the function is returned (this does not |
| 185 | necessarily signal failure; could still be the legitimate path).""" |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 186 | import os |
| 187 | if os.path.isabs(file): |
| 188 | return file |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 189 | path = sys.path |
| 190 | path = [os.path.dirname(here)] + path |
| 191 | for dn in path: |
| 192 | fn = os.path.join(dn, file) |
| 193 | if os.path.exists(fn): return fn |
| 194 | return file |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 195 | |
| 196 | def verify(condition, reason='test failed'): |
Guido van Rossum | a1374e4 | 2001-01-19 19:01:56 +0000 | [diff] [blame] | 197 | """Verify that condition is true. If not, raise TestFailed. |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 198 | |
Skip Montanaro | c955f89 | 2001-01-20 19:12:54 +0000 | [diff] [blame] | 199 | The optional argument reason can be given to provide |
Tim Peters | 983874d | 2001-01-19 05:59:21 +0000 | [diff] [blame] | 200 | a better error text. |
Tim Peters | d2bf3b7 | 2001-01-18 02:22:22 +0000 | [diff] [blame] | 201 | """ |
Tim Peters | 983874d | 2001-01-19 05:59:21 +0000 | [diff] [blame] | 202 | |
Tim Peters | d2bf3b7 | 2001-01-18 02:22:22 +0000 | [diff] [blame] | 203 | if not condition: |
Guido van Rossum | a1374e4 | 2001-01-19 19:01:56 +0000 | [diff] [blame] | 204 | raise TestFailed(reason) |
Jeremy Hylton | 4779399 | 2001-02-19 15:35:26 +0000 | [diff] [blame] | 205 | |
Tim Peters | c2fe618 | 2001-10-30 23:20:46 +0000 | [diff] [blame] | 206 | def vereq(a, b): |
Tim Peters | 7790297 | 2001-12-29 17:34:57 +0000 | [diff] [blame] | 207 | """Raise TestFailed if a == b is false. |
| 208 | |
| 209 | This is better than verify(a == b) because, in case of failure, the |
| 210 | error message incorporates repr(a) and repr(b) so you can see the |
| 211 | inputs. |
| 212 | |
| 213 | Note that "not (a == b)" isn't necessarily the same as "a != b"; the |
| 214 | former is tested. |
| 215 | """ |
| 216 | |
Tim Peters | c2fe618 | 2001-10-30 23:20:46 +0000 | [diff] [blame] | 217 | if not (a == b): |
| 218 | raise TestFailed, "%r == %r" % (a, b) |
| 219 | |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 220 | def sortdict(dict): |
| 221 | "Like repr(dict), but in sorted order." |
| 222 | items = dict.items() |
| 223 | items.sort() |
| 224 | reprpairs = ["%r: %r" % pair for pair in items] |
| 225 | withcommas = ", ".join(reprpairs) |
| 226 | return "{%s}" % withcommas |
| 227 | |
Jeremy Hylton | 4779399 | 2001-02-19 15:35:26 +0000 | [diff] [blame] | 228 | def check_syntax(statement): |
| 229 | try: |
| 230 | compile(statement, '<string>', 'exec') |
| 231 | except SyntaxError: |
| 232 | pass |
| 233 | else: |
| 234 | print 'Missing SyntaxError: "%s"' % statement |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 235 | |
Hye-Shik Chang | aaa2f1d | 2005-12-10 17:44:27 +0000 | [diff] [blame] | 236 | def open_urlresource(url): |
| 237 | import urllib, urlparse |
| 238 | import os.path |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 239 | |
Hye-Shik Chang | aaa2f1d | 2005-12-10 17:44:27 +0000 | [diff] [blame] | 240 | filename = urlparse.urlparse(url)[2].split('/')[-1] # '/': it's URL! |
| 241 | |
| 242 | for path in [os.path.curdir, os.path.pardir]: |
| 243 | fn = os.path.join(path, filename) |
| 244 | if os.path.exists(fn): |
| 245 | return open(fn) |
| 246 | |
| 247 | requires('urlfetch') |
| 248 | print >> get_original_stdout(), '\tfetching %s ...' % url |
| 249 | fn, _ = urllib.urlretrieve(url, filename) |
| 250 | return open(fn) |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 251 | |
| 252 | #======================================================================= |
| 253 | # Preliminary PyUNIT integration. |
| 254 | |
| 255 | import unittest |
| 256 | |
| 257 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 258 | class BasicTestRunner: |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 259 | def run(self, test): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 260 | result = unittest.TestResult() |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 261 | test(result) |
| 262 | return result |
| 263 | |
| 264 | |
Fred Drake | 2664103 | 2001-10-04 19:46:07 +0000 | [diff] [blame] | 265 | def run_suite(suite, testclass=None): |
Barry Warsaw | c88425e | 2001-09-20 06:31:22 +0000 | [diff] [blame] | 266 | """Run tests from a unittest.TestSuite-derived class.""" |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 267 | if verbose: |
Fred Drake | 84a5934 | 2001-03-23 04:21:17 +0000 | [diff] [blame] | 268 | runner = unittest.TextTestRunner(sys.stdout, verbosity=2) |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 269 | else: |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 270 | runner = BasicTestRunner() |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 271 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 272 | result = runner.run(suite) |
| 273 | if not result.wasSuccessful(): |
Fred Drake | 14f6c18 | 2001-07-16 18:51:32 +0000 | [diff] [blame] | 274 | if len(result.errors) == 1 and not result.failures: |
| 275 | err = result.errors[0][1] |
| 276 | elif len(result.failures) == 1 and not result.errors: |
| 277 | err = result.failures[0][1] |
| 278 | else: |
Fred Drake | 2664103 | 2001-10-04 19:46:07 +0000 | [diff] [blame] | 279 | if testclass is None: |
| 280 | msg = "errors occurred; run in verbose mode for details" |
| 281 | else: |
| 282 | msg = "errors occurred in %s.%s" \ |
| 283 | % (testclass.__module__, testclass.__name__) |
| 284 | raise TestFailed(msg) |
Tim Peters | 2d84f2c | 2001-09-08 03:37:56 +0000 | [diff] [blame] | 285 | raise TestFailed(err) |
Tim Peters | a0a6222 | 2001-09-09 06:12:01 +0000 | [diff] [blame] | 286 | |
Barry Warsaw | c10d690 | 2001-09-20 06:30:41 +0000 | [diff] [blame] | 287 | |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 288 | def run_unittest(*classes): |
| 289 | """Run tests from unittest.TestCase-derived classes.""" |
Raymond Hettinger | 9dcbbea | 2003-04-27 07:54:23 +0000 | [diff] [blame] | 290 | suite = unittest.TestSuite() |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 291 | for cls in classes: |
Raymond Hettinger | f359062 | 2003-07-16 04:29:42 +0000 | [diff] [blame] | 292 | if isinstance(cls, (unittest.TestSuite, unittest.TestCase)): |
Raymond Hettinger | 21d9987 | 2003-07-16 02:59:32 +0000 | [diff] [blame] | 293 | suite.addTest(cls) |
| 294 | else: |
| 295 | suite.addTest(unittest.makeSuite(cls)) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 296 | if len(classes)==1: |
| 297 | testclass = classes[0] |
| 298 | else: |
| 299 | testclass = None |
| 300 | run_suite(suite, testclass) |
Raymond Hettinger | 9dcbbea | 2003-04-27 07:54:23 +0000 | [diff] [blame] | 301 | |
Barry Warsaw | c10d690 | 2001-09-20 06:30:41 +0000 | [diff] [blame] | 302 | |
Tim Peters | a0a6222 | 2001-09-09 06:12:01 +0000 | [diff] [blame] | 303 | #======================================================================= |
| 304 | # doctest driver. |
| 305 | |
| 306 | def run_doctest(module, verbosity=None): |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 307 | """Run doctest on the given module. Return (#failures, #tests). |
Tim Peters | a0a6222 | 2001-09-09 06:12:01 +0000 | [diff] [blame] | 308 | |
| 309 | If optional argument verbosity is not specified (or is None), pass |
Tim Peters | bea3fb8 | 2001-09-10 01:39:21 +0000 | [diff] [blame] | 310 | test_support's belief about verbosity on to doctest. Else doctest's |
| 311 | usual behavior is used (it searches sys.argv for -v). |
Tim Peters | a0a6222 | 2001-09-09 06:12:01 +0000 | [diff] [blame] | 312 | """ |
| 313 | |
| 314 | import doctest |
| 315 | |
| 316 | if verbosity is None: |
| 317 | verbosity = verbose |
| 318 | else: |
| 319 | verbosity = None |
| 320 | |
Tim Peters | 342ca75 | 2001-09-25 19:13:20 +0000 | [diff] [blame] | 321 | # Direct doctest output (normally just errors) to real stdout; doctest |
| 322 | # output shouldn't be compared by regrtest. |
| 323 | save_stdout = sys.stdout |
Tim Peters | 8dee809 | 2001-09-25 20:05:11 +0000 | [diff] [blame] | 324 | sys.stdout = get_original_stdout() |
Tim Peters | 342ca75 | 2001-09-25 19:13:20 +0000 | [diff] [blame] | 325 | try: |
| 326 | f, t = doctest.testmod(module, verbose=verbosity) |
| 327 | if f: |
| 328 | raise TestFailed("%d of %d doctests failed" % (f, t)) |
| 329 | finally: |
| 330 | sys.stdout = save_stdout |
Raymond Hettinger | 35b34bd | 2003-05-17 00:58:33 +0000 | [diff] [blame] | 331 | if verbose: |
Raymond Hettinger | 1ba24b4 | 2003-05-17 01:59:57 +0000 | [diff] [blame] | 332 | print 'doctest (%s) ... %d tests with zero failures' % (module.__name__, t) |
Raymond Hettinger | 35b34bd | 2003-05-17 00:58:33 +0000 | [diff] [blame] | 333 | return f, t |