1 """Supporting definitions for the Python regression test."""
3 if __name__
!= 'test.test_support':
4 raise ImportError, 'test_support must be imported from the test package'
8 class Error(Exception):
9 """Base class for regression test exceptions."""
11 class TestFailed(Error
):
14 class TestSkipped(Error
):
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
24 verbose
= 1 # Flag set to 0 by regrtest.py
25 use_resources
= None # Flag set to [] by regrtest.py
27 # _original_stdout is meant to hold stdout at the time regrtest began.
28 # This may be "the real" stdout, or IDLE's emulation of stdout, or whatever.
29 # The point is to have some flavor of stdout the user can actually see.
30 _original_stdout
= None
31 def record_original_stdout(stdout
):
32 global _original_stdout
33 _original_stdout
= stdout
35 def get_original_stdout():
36 return _original_stdout
or sys
.stdout
47 for dirname
in sys
.path
:
49 os
.unlink(os
.path
.join(dirname
, modname
+ '.pyc'))
53 def requires(resource
, msg
=None):
54 if use_resources
is not None and resource
not in use_resources
:
56 msg
= "Use of the `%s' resource not enabled" % resource
57 raise TestSkipped(msg
)
61 def fcmp(x
, y
): # fuzzy comparison function
62 if type(x
) == type(0.0) or type(y
) == type(0.0):
65 fuzz
= (abs(x
) + abs(y
)) * FUZZ
70 elif type(x
) == type(y
) and type(x
) in (type(()), type([])):
71 for i
in range(min(len(x
), len(y
))):
72 outcome
= fcmp(x
[i
], y
[i
])
75 return cmp(len(x
), len(y
))
85 # Filename used for testing
87 # Jython disallows @ in module names
89 elif os
.name
!= 'riscos':
91 # Unicode name only used if TEST_FN_ENCODING exists for the platform.
93 TESTFN_UNICODE
=unicode("@test-\xe0\xf2", "latin-1") # 2 latin characters.
95 TESTFN_ENCODING
="mbcs"
100 from os
import unlink
102 def findfile(file, here
=__file__
):
104 if os
.path
.isabs(file):
107 path
= [os
.path
.dirname(here
)] + path
109 fn
= os
.path
.join(dn
, file)
110 if os
.path
.exists(fn
): return fn
113 def verify(condition
, reason
='test failed'):
114 """Verify that condition is true. If not, raise TestFailed.
116 The optional argument reason can be given to provide
121 raise TestFailed(reason
)
124 """Raise TestFailed if a == b is false.
126 This is better than verify(a == b) because, in case of failure, the
127 error message incorporates repr(a) and repr(b) so you can see the
130 Note that "not (a == b)" isn't necessarily the same as "a != b"; the
135 raise TestFailed
, "%r == %r" % (a
, b
)
138 "Like repr(dict), but in sorted order."
141 reprpairs
= ["%r: %r" % pair
for pair
in items
]
142 withcommas
= ", ".join(reprpairs
)
143 return "{%s}" % withcommas
145 def check_syntax(statement
):
147 compile(statement
, '<string>', 'exec')
151 print 'Missing SyntaxError: "%s"' % statement
155 #=======================================================================
156 # Preliminary PyUNIT integration.
161 class BasicTestRunner
:
163 result
= unittest
.TestResult()
168 def run_suite(suite
, testclass
=None):
169 """Run tests from a unittest.TestSuite-derived class."""
171 runner
= unittest
.TextTestRunner(sys
.stdout
, verbosity
=2)
173 runner
= BasicTestRunner()
175 result
= runner
.run(suite
)
176 if not result
.wasSuccessful():
177 if len(result
.errors
) == 1 and not result
.failures
:
178 err
= result
.errors
[0][1]
179 elif len(result
.failures
) == 1 and not result
.errors
:
180 err
= result
.failures
[0][1]
182 if testclass
is None:
183 msg
= "errors occurred; run in verbose mode for details"
185 msg
= "errors occurred in %s.%s" \
186 % (testclass
.__module
__, testclass
.__name
__)
187 raise TestFailed(msg
)
188 raise TestFailed(err
)
191 def run_unittest(testclass
):
192 """Run tests from a unittest.TestCase-derived class."""
193 run_suite(unittest
.makeSuite(testclass
), testclass
)
196 #=======================================================================
199 def run_doctest(module
, verbosity
=None):
200 """Run doctest on the given module. Return (#failures, #tests).
202 If optional argument verbosity is not specified (or is None), pass
203 test_support's belief about verbosity on to doctest. Else doctest's
204 usual behavior is used (it searches sys.argv for -v).
209 if verbosity
is None:
214 # Direct doctest output (normally just errors) to real stdout; doctest
215 # output shouldn't be compared by regrtest.
216 save_stdout
= sys
.stdout
217 sys
.stdout
= get_original_stdout()
219 f
, t
= doctest
.testmod(module
, verbose
=verbosity
)
221 raise TestFailed("%d of %d doctests failed" % (f
, t
))
224 sys
.stdout
= save_stdout