55import warnings
66
77from . import result
8+ from .case import _SubTest
89from .signals import registerResult
910
1011__unittest = True
@@ -40,6 +41,7 @@ def __init__(self, stream, descriptions, verbosity):
4041 self .showAll = verbosity > 1
4142 self .dots = verbosity == 1
4243 self .descriptions = descriptions
44+ self ._newline = True
4345
4446 def getDescription (self , test ):
4547 doc_first_line = test .shortDescription ()
@@ -54,39 +56,64 @@ def startTest(self, test):
5456 self .stream .write (self .getDescription (test ))
5557 self .stream .write (" ... " )
5658 self .stream .flush ()
59+ self ._newline = False
60+
61+ def _write_status (self , test , status ):
62+ is_subtest = isinstance (test , _SubTest )
63+ if is_subtest or self ._newline :
64+ if not self ._newline :
65+ self .stream .writeln ()
66+ if is_subtest :
67+ self .stream .write (" " )
68+ self .stream .write (self .getDescription (test ))
69+ self .stream .write (" ... " )
70+ self .stream .writeln (status )
71+ self .stream .flush ()
72+ self ._newline = True
73+
74+ def addSubTest (self , test , subtest , err ):
75+ if err is not None :
76+ if self .showAll :
77+ if issubclass (err [0 ], subtest .failureException ):
78+ self ._write_status (subtest , "FAIL" )
79+ else :
80+ self ._write_status (subtest , "ERROR" )
81+ elif self .dots :
82+ if issubclass (err [0 ], subtest .failureException ):
83+ self .stream .write ('F' )
84+ else :
85+ self .stream .write ('E' )
86+ self .stream .flush ()
87+ super (TextTestResult , self ).addSubTest (test , subtest , err )
5788
5889 def addSuccess (self , test ):
5990 super (TextTestResult , self ).addSuccess (test )
6091 if self .showAll :
61- self .stream .writeln ("ok" )
62- self .stream .flush ()
92+ self ._write_status (test , "ok" )
6393 elif self .dots :
6494 self .stream .write ('.' )
6595 self .stream .flush ()
6696
6797 def addError (self , test , err ):
6898 super (TextTestResult , self ).addError (test , err )
6999 if self .showAll :
70- self .stream .writeln ("ERROR" )
71- self .stream .flush ()
100+ self ._write_status (test , "ERROR" )
72101 elif self .dots :
73102 self .stream .write ('E' )
74103 self .stream .flush ()
75104
76105 def addFailure (self , test , err ):
77106 super (TextTestResult , self ).addFailure (test , err )
78107 if self .showAll :
79- self .stream .writeln ("FAIL" )
80- self .stream .flush ()
108+ self ._write_status (test , "FAIL" )
81109 elif self .dots :
82110 self .stream .write ('F' )
83111 self .stream .flush ()
84112
85113 def addSkip (self , test , reason ):
86114 super (TextTestResult , self ).addSkip (test , reason )
87115 if self .showAll :
88- self .stream .writeln ("skipped {0!r}" .format (reason ))
89- self .stream .flush ()
116+ self ._write_status (test , "skipped {0!r}" .format (reason ))
90117 elif self .dots :
91118 self .stream .write ("s" )
92119 self .stream .flush ()
@@ -115,6 +142,12 @@ def printErrors(self):
115142 self .stream .flush ()
116143 self .printErrorList ('ERROR' , self .errors )
117144 self .printErrorList ('FAIL' , self .failures )
145+ unexpectedSuccesses = getattr (self , 'unexpectedSuccesses' , ())
146+ if unexpectedSuccesses :
147+ self .stream .writeln (self .separator1 )
148+ for test in unexpectedSuccesses :
149+ self .stream .writeln (f"UNEXPECTED SUCCESS: { self .getDescription (test )} " )
150+ self .stream .flush ()
118151
119152 def printErrorList (self , flavour , errors ):
120153 for test , err in errors :
@@ -167,15 +200,6 @@ def run(self, test):
167200 if self .warnings :
168201 # if self.warnings is set, use it to filter all the warnings
169202 warnings .simplefilter (self .warnings )
170- # if the filter is 'default' or 'always', special-case the
171- # warnings from the deprecated unittest methods to show them
172- # no more than once per module, because they can be fairly
173- # noisy. The -Wd and -Wa flags can be used to bypass this
174- # only when self.warnings is None.
175- if self .warnings in ['default' , 'always' ]:
176- warnings .filterwarnings ('module' ,
177- category = DeprecationWarning ,
178- message = r'Please use assert\w+ instead.' )
179203 startTime = time .perf_counter ()
180204 startTestRun = getattr (result , 'startTestRun' , None )
181205 if startTestRun is not None :
0 commit comments