blob: 3d5c783644f68a8de89e92ed9409be814307d928 [file] [log] [blame]
Fred Drake1790dd42000-07-24 06:55:00 +00001"""Supporting definitions for the Python regression test."""
Guido van Rossum3bead091992-01-27 17:00:37 +00002
Fred Drakecd1b1dd2001-03-21 18:26:33 +00003import sys
4
Fred Drake1790dd42000-07-24 06:55:00 +00005
6class Error(Exception):
Fred Drake004d5e62000-10-23 17:22:08 +00007 """Base class for regression test exceptions."""
Fred Drake1790dd42000-07-24 06:55:00 +00008
9class TestFailed(Error):
Fred Drake004d5e62000-10-23 17:22:08 +000010 """Test failed."""
Fred Drake1790dd42000-07-24 06:55:00 +000011
12class TestSkipped(Error):
Fred Drake004d5e62000-10-23 17:22:08 +000013 """Test skipped.
Fred Drake1790dd42000-07-24 06:55:00 +000014
Fred Drake004d5e62000-10-23 17:22:08 +000015 This can be raised to indicate that a test was deliberatly
16 skipped, but not because a feature wasn't available. For
17 example, if some resource can't be used, such as the network
18 appears to be unavailable, this should be raised instead of
19 TestFailed.
Fred Drake004d5e62000-10-23 17:22:08 +000020 """
Fred Drake1790dd42000-07-24 06:55:00 +000021
Fred Drake004d5e62000-10-23 17:22:08 +000022verbose = 1 # Flag set to 0 by regrtest.py
Fred Drake1790dd42000-07-24 06:55:00 +000023use_large_resources = 1 # Flag set to 0 by regrtest.py
Guido van Rossum531661c1996-12-20 02:58:22 +000024
Guido van Rossum3bead091992-01-27 17:00:37 +000025def unload(name):
Fred Drake004d5e62000-10-23 17:22:08 +000026 try:
27 del sys.modules[name]
28 except KeyError:
29 pass
Guido van Rossum3bead091992-01-27 17:00:37 +000030
31def forget(modname):
Fred Drake004d5e62000-10-23 17:22:08 +000032 unload(modname)
Fred Drakecd1b1dd2001-03-21 18:26:33 +000033 import os
Fred Drake004d5e62000-10-23 17:22:08 +000034 for dirname in sys.path:
35 try:
36 os.unlink(os.path.join(dirname, modname + '.pyc'))
37 except os.error:
38 pass
Guido van Rossum3bead091992-01-27 17:00:37 +000039
Guido van Rossum35fb82a1993-01-26 13:04:43 +000040FUZZ = 1e-6
41
42def fcmp(x, y): # fuzzy comparison function
Fred Drake004d5e62000-10-23 17:22:08 +000043 if type(x) == type(0.0) or type(y) == type(0.0):
44 try:
45 x, y = coerce(x, y)
46 fuzz = (abs(x) + abs(y)) * FUZZ
47 if abs(x-y) <= fuzz:
48 return 0
49 except:
50 pass
51 elif type(x) == type(y) and type(x) in (type(()), type([])):
52 for i in range(min(len(x), len(y))):
53 outcome = fcmp(x[i], y[i])
Fred Drake132dce22000-12-12 23:11:42 +000054 if outcome != 0:
Fred Drake004d5e62000-10-23 17:22:08 +000055 return outcome
56 return cmp(len(x), len(y))
57 return cmp(x, y)
Guido van Rossum35fb82a1993-01-26 13:04:43 +000058
Guido van Rossuma8f7e592001-03-13 09:31:07 +000059import os
Barry Warsaw559f6682001-03-23 18:04:02 +000060# Filename used for testing
61if os.name == 'java':
62 # Jython disallows @ in module names
63 TESTFN = '$test'
64elif os.name != 'riscos':
65 TESTFN = '@test'
Mark Hammondef8b6542001-05-13 08:04:26 +000066 # Unicode name only used if TEST_FN_ENCODING exists for the platform.
67 TESTFN_UNICODE=u"@test-\xe0\xf2" # 2 latin characters.
68 if os.name=="nt":
69 TESTFN_ENCODING="mbcs"
Guido van Rossuma8f7e592001-03-13 09:31:07 +000070else:
Barry Warsaw559f6682001-03-23 18:04:02 +000071 TESTFN = 'test'
Guido van Rossuma8f7e592001-03-13 09:31:07 +000072del os
73
Guido van Rossum3bead091992-01-27 17:00:37 +000074from os import unlink
Guido van Rossume26132c1998-04-23 20:13:30 +000075
76def findfile(file, here=__file__):
Fred Drake004d5e62000-10-23 17:22:08 +000077 import os
78 if os.path.isabs(file):
79 return file
Fred Drake004d5e62000-10-23 17:22:08 +000080 path = sys.path
81 path = [os.path.dirname(here)] + path
82 for dn in path:
83 fn = os.path.join(dn, file)
84 if os.path.exists(fn): return fn
85 return file
Marc-André Lemburg36619082001-01-17 19:11:13 +000086
87def verify(condition, reason='test failed'):
Guido van Rossuma1374e42001-01-19 19:01:56 +000088 """Verify that condition is true. If not, raise TestFailed.
Marc-André Lemburg36619082001-01-17 19:11:13 +000089
Skip Montanaroc955f892001-01-20 19:12:54 +000090 The optional argument reason can be given to provide
Tim Peters983874d2001-01-19 05:59:21 +000091 a better error text.
Tim Petersd2bf3b72001-01-18 02:22:22 +000092 """
Tim Peters983874d2001-01-19 05:59:21 +000093
Tim Petersd2bf3b72001-01-18 02:22:22 +000094 if not condition:
Guido van Rossuma1374e42001-01-19 19:01:56 +000095 raise TestFailed(reason)
Jeremy Hylton47793992001-02-19 15:35:26 +000096
Tim Peters2f228e72001-05-13 00:19:31 +000097def sortdict(dict):
98 "Like repr(dict), but in sorted order."
99 items = dict.items()
100 items.sort()
101 reprpairs = ["%r: %r" % pair for pair in items]
102 withcommas = ", ".join(reprpairs)
103 return "{%s}" % withcommas
104
Jeremy Hylton47793992001-02-19 15:35:26 +0000105def check_syntax(statement):
106 try:
107 compile(statement, '<string>', 'exec')
108 except SyntaxError:
109 pass
110 else:
111 print 'Missing SyntaxError: "%s"' % statement
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000112
113
114
115#=======================================================================
116# Preliminary PyUNIT integration.
117
118import unittest
119
120
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000121class BasicTestRunner:
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000122 def run(self, test):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000123 result = unittest.TestResult()
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000124 test(result)
125 return result
126
127
128def run_unittest(testclass):
129 """Run tests from a unittest.TestCase-derived class."""
130 if verbose:
Fred Drake84a59342001-03-23 04:21:17 +0000131 runner = unittest.TextTestRunner(sys.stdout, verbosity=2)
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000132 else:
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000133 runner = BasicTestRunner()
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000134
135 suite = unittest.makeSuite(testclass)
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000136 result = runner.run(suite)
137 if not result.wasSuccessful():
Fred Drake14f6c182001-07-16 18:51:32 +0000138 if len(result.errors) == 1 and not result.failures:
139 err = result.errors[0][1]
140 elif len(result.failures) == 1 and not result.errors:
141 err = result.failures[0][1]
142 else:
143 raise TestFailed("errors occurred in %s.%s"
144 % (testclass.__module__, testclass.__name__))
145 if err[0] is AssertionError:
146 raise TestFailed(str(err[1]))
147 else:
148 raise TestFailed("%s: %s" % err[:2])