@@ -43,24 +43,29 @@ def __exit__(self, exc_type, exc_value, tb):
43
43
44
44
45
45
class SubtestContext :
46
+ def __init__ (self , msg = None , params = None ):
47
+ self .msg = msg
48
+ self .params = params
49
+
46
50
def __enter__ (self ):
47
51
pass
48
52
49
53
def __exit__ (self , * exc_info ):
50
54
if exc_info [0 ] is not None :
51
55
# Exception raised
52
56
global __test_result__ , __current_test__
53
- handle_test_exception (
54
- __current_test__ ,
55
- __test_result__ ,
56
- exc_info
57
- )
57
+ test_details = __current_test__
58
+ if self .msg :
59
+ test_details += (f" [{ self .msg } ]" ,)
60
+ if self .params :
61
+ detail = ", " .join (f"{ k } ={ v } " for k , v in self .params .items ())
62
+ test_details += (f" ({ detail } )" ,)
63
+
64
+ handle_test_exception (test_details , __test_result__ , exc_info , False )
58
65
# Suppress the exception as we've captured it above
59
66
return True
60
67
61
68
62
-
63
-
64
69
class NullContext :
65
70
def __enter__ (self ):
66
71
pass
@@ -287,6 +292,7 @@ def __init__(self):
287
292
self .testsRun = 0
288
293
self .errors = []
289
294
self .failures = []
295
+ self .newFailures = 0
290
296
291
297
def wasSuccessful (self ):
292
298
return self .errorsNum == 0 and self .failuresNum == 0
@@ -299,8 +305,9 @@ def printErrors(self):
299
305
def printErrorList (self , lst ):
300
306
sep = "----------------------------------------------------------------------"
301
307
for c , e in lst :
308
+ detail = " " .join ((str (i ) for i in c ))
302
309
print ("======================================================================" )
303
- print (c )
310
+ print (f"FAIL: { detail } " )
304
311
print (sep )
305
312
print (e )
306
313
@@ -331,18 +338,23 @@ def capture_exc(exc, traceback):
331
338
return buf .getvalue ()
332
339
333
340
334
- def handle_test_exception (current_test : tuple , test_result : TestResult , exc_info : tuple ):
341
+ def handle_test_exception (
342
+ current_test : tuple , test_result : TestResult , exc_info : tuple , verbose = True
343
+ ):
335
344
exc = exc_info [1 ]
336
- traceback = exc_info [2 ]
345
+ traceback = exc_info [2 ]
337
346
ex_str = capture_exc (exc , traceback )
338
347
if isinstance (exc , AssertionError ):
339
348
test_result .failuresNum += 1
340
349
test_result .failures .append ((current_test , ex_str ))
341
- print (" FAIL" )
350
+ if verbose :
351
+ print (" FAIL" )
342
352
else :
343
353
test_result .errorsNum += 1
344
354
test_result .errors .append ((current_test , ex_str ))
345
- print (" ERROR" )
355
+ if verbose :
356
+ print (" ERROR" )
357
+ test_result .newFailures += 1
346
358
347
359
348
360
def run_suite (c , test_result , suite_name = "" ):
@@ -370,20 +382,22 @@ def run_one(test_function):
370
382
test_container = f"({ suite_name } )"
371
383
__current_test__ = (name , test_container )
372
384
try :
385
+ test_result .newFailures = 0
373
386
test_result .testsRun += 1
374
387
test_globals = dict (** globals ())
375
388
test_globals ["test_function" ] = test_function
376
389
exec ("test_function()" , test_globals , test_globals )
377
390
# No exception occurred, test passed
378
- print (" ok" )
391
+ if test_result .newFailures :
392
+ print (" FAIL" )
393
+ else :
394
+ print (" ok" )
379
395
except SkipTest as e :
380
396
print (" skipped:" , e .args [0 ])
381
397
test_result .skippedNum += 1
382
398
except Exception as ex :
383
399
handle_test_exception (
384
- current_test = (name , c ),
385
- test_result = test_result ,
386
- exc_info = sys .exc_info ()
400
+ current_test = (name , c ), test_result = test_result , exc_info = sys .exc_info ()
387
401
)
388
402
# Uncomment to investigate failure in detail
389
403
# raise
0 commit comments