Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 1 | """Supporting definitions for the Python regression test.""" |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 2 | |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 3 | import sys |
| 4 | |
Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 5 | |
| 6 | class Error(Exception): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 7 | """Base class for regression test exceptions.""" |
Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 8 | |
| 9 | class TestFailed(Error): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 10 | """Test failed.""" |
Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 11 | |
| 12 | class TestSkipped(Error): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 13 | """Test skipped. |
Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 14 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 15 | 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 Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 20 | """ |
Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 21 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 22 | verbose = 1 # Flag set to 0 by regrtest.py |
Fred Drake | 1790dd4 | 2000-07-24 06:55:00 +0000 | [diff] [blame] | 23 | use_large_resources = 1 # Flag set to 0 by regrtest.py |
Guido van Rossum | 531661c | 1996-12-20 02:58:22 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 25 | def unload(name): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 26 | try: |
| 27 | del sys.modules[name] |
| 28 | except KeyError: |
| 29 | pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 30 | |
| 31 | def forget(modname): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 32 | unload(modname) |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 33 | import os |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 34 | for dirname in sys.path: |
| 35 | try: |
| 36 | os.unlink(os.path.join(dirname, modname + '.pyc')) |
| 37 | except os.error: |
| 38 | pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 40 | FUZZ = 1e-6 |
| 41 | |
| 42 | def fcmp(x, y): # fuzzy comparison function |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 43 | 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 Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 54 | if outcome != 0: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 55 | return outcome |
| 56 | return cmp(len(x), len(y)) |
| 57 | return cmp(x, y) |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | a8f7e59 | 2001-03-13 09:31:07 +0000 | [diff] [blame] | 59 | import os |
Barry Warsaw | 559f668 | 2001-03-23 18:04:02 +0000 | [diff] [blame] | 60 | # Filename used for testing |
| 61 | if os.name == 'java': |
| 62 | # Jython disallows @ in module names |
| 63 | TESTFN = '$test' |
| 64 | elif os.name != 'riscos': |
| 65 | TESTFN = '@test' |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 66 | # 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 Rossum | a8f7e59 | 2001-03-13 09:31:07 +0000 | [diff] [blame] | 70 | else: |
Barry Warsaw | 559f668 | 2001-03-23 18:04:02 +0000 | [diff] [blame] | 71 | TESTFN = 'test' |
Guido van Rossum | a8f7e59 | 2001-03-13 09:31:07 +0000 | [diff] [blame] | 72 | del os |
| 73 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 74 | from os import unlink |
Guido van Rossum | e26132c | 1998-04-23 20:13:30 +0000 | [diff] [blame] | 75 | |
| 76 | def findfile(file, here=__file__): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 77 | import os |
| 78 | if os.path.isabs(file): |
| 79 | return file |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 80 | 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é Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 86 | |
| 87 | def verify(condition, reason='test failed'): |
Guido van Rossum | a1374e4 | 2001-01-19 19:01:56 +0000 | [diff] [blame] | 88 | """Verify that condition is true. If not, raise TestFailed. |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 89 | |
Skip Montanaro | c955f89 | 2001-01-20 19:12:54 +0000 | [diff] [blame] | 90 | The optional argument reason can be given to provide |
Tim Peters | 983874d | 2001-01-19 05:59:21 +0000 | [diff] [blame] | 91 | a better error text. |
Tim Peters | d2bf3b7 | 2001-01-18 02:22:22 +0000 | [diff] [blame] | 92 | """ |
Tim Peters | 983874d | 2001-01-19 05:59:21 +0000 | [diff] [blame] | 93 | |
Tim Peters | d2bf3b7 | 2001-01-18 02:22:22 +0000 | [diff] [blame] | 94 | if not condition: |
Guido van Rossum | a1374e4 | 2001-01-19 19:01:56 +0000 | [diff] [blame] | 95 | raise TestFailed(reason) |
Jeremy Hylton | 4779399 | 2001-02-19 15:35:26 +0000 | [diff] [blame] | 96 | |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 97 | def 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 Hylton | 4779399 | 2001-02-19 15:35:26 +0000 | [diff] [blame] | 105 | def check_syntax(statement): |
| 106 | try: |
| 107 | compile(statement, '<string>', 'exec') |
| 108 | except SyntaxError: |
| 109 | pass |
| 110 | else: |
| 111 | print 'Missing SyntaxError: "%s"' % statement |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 112 | |
| 113 | |
| 114 | |
| 115 | #======================================================================= |
| 116 | # Preliminary PyUNIT integration. |
| 117 | |
| 118 | import unittest |
| 119 | |
| 120 | |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 121 | class BasicTestRunner: |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 122 | def run(self, test): |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 123 | result = unittest.TestResult() |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 124 | test(result) |
| 125 | return result |
| 126 | |
| 127 | |
| 128 | def run_unittest(testclass): |
| 129 | """Run tests from a unittest.TestCase-derived class.""" |
| 130 | if verbose: |
Fred Drake | 84a5934 | 2001-03-23 04:21:17 +0000 | [diff] [blame] | 131 | runner = unittest.TextTestRunner(sys.stdout, verbosity=2) |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 132 | else: |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 133 | runner = BasicTestRunner() |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 134 | |
| 135 | suite = unittest.makeSuite(testclass) |
Steve Purcell | 5ddd1a8 | 2001-03-22 08:45:36 +0000 | [diff] [blame] | 136 | result = runner.run(suite) |
| 137 | if not result.wasSuccessful(): |
Fred Drake | cd1b1dd | 2001-03-21 18:26:33 +0000 | [diff] [blame] | 138 | raise TestFailed("errors occurred in %s.%s" |
| 139 | % (testclass.__module__, testclass.__name__)) |