39
39
import _io
40
40
import sys
41
41
42
-
43
42
os = sys .modules .get ("posix" , sys .modules .get ("nt" , None ))
44
43
if os is None :
45
44
raise ImportError ("posix or nt module is required in builtin modules" )
46
45
47
-
48
46
FAIL = '\033 [91m'
49
47
ENDC = '\033 [0m'
50
48
BOLD = '\033 [1m'
@@ -57,12 +55,13 @@ class SkipTest(BaseException):
57
55
58
56
59
57
class TestCase (object ):
58
+
60
59
def __init__ (self ):
61
60
self .exceptions = []
62
61
self .passed = 0
63
62
self .failed = 0
64
63
65
- def run_safely (self , func ):
64
+ def run_safely (self , func , print_immediately = False ):
66
65
if verbose :
67
66
print (u"\n \t \u21B3 " , func .__name__ , " " , end = "" )
68
67
try :
@@ -71,6 +70,8 @@ def run_safely(self, func):
71
70
if isinstance (e , SkipTest ):
72
71
print ("Skipped: %s" % e )
73
72
else :
73
+ if print_immediately :
74
+ print ("Exception during setup occurred: %s\n " % e )
74
75
code = func .__code__
75
76
self .exceptions .append (
76
77
("%s:%d (%s)" % (code .co_filename , code .co_firstlineno , func ), e )
@@ -95,7 +96,7 @@ def success(self):
95
96
96
97
def failure (self ):
97
98
self .failed += 1
98
- fail_msg = FAIL + BOLD + "F" + ENDC if verbose else "F"
99
+ fail_msg = FAIL + BOLD + "F" + ENDC if verbose else "F"
99
100
print (fail_msg , end = "" , flush = True )
100
101
101
102
def assertTrue (self , value , msg = "" ):
@@ -141,6 +142,7 @@ def assertSequenceEqual(self, expected, actual, msg=None):
141
142
assert expected_value == next (actual_iter ), msg
142
143
143
144
class assertRaises ():
145
+
144
146
def __init__ (self , exc_type , function = None , * args , ** kwargs ):
145
147
if function is None :
146
148
self .exc_type = exc_type
@@ -177,9 +179,9 @@ def run(cls, items=None):
177
179
break
178
180
items += typ .__dict__ .items ()
179
181
if hasattr (instance , "setUp" ):
180
- if not instance .run_safely (instance .setUp ):
182
+ if not instance .run_safely (instance .setUp , print_immediately = True ):
181
183
return instance
182
- for k ,v in items :
184
+ for k , v in items :
183
185
if k .startswith ("test" ):
184
186
if patterns :
185
187
if not any (p in k for p in patterns ):
@@ -198,6 +200,7 @@ class ThisTestCase(cls, TestCase): pass
198
200
199
201
200
202
class TestRunner (object ):
203
+
201
204
def __init__ (self , paths ):
202
205
self .testfiles = TestRunner .find_testfiles (paths )
203
206
self .exceptions = []
@@ -255,7 +258,7 @@ def run(self):
255
258
print (u"\n \u25B9 " , module .__name__ , end = "" )
256
259
# some tests can modify the global scope leading to a RuntimeError: test_scope.test_nesting_plus_free_ref_to_global
257
260
module_dict = dict (module .__dict__ )
258
- for k ,v in module_dict .items ():
261
+ for k , v in module_dict .items ():
259
262
if (k .startswith ("Test" ) or k .endswith ("Test" ) or k .endswith ("Tests" )) and isinstance (v , type ):
260
263
testcase = TestCase .runClass (v )
261
264
else :
0 commit comments