Skip to content

Commit e582666

Browse files
pfalconpi-anl
authored andcommitted
unittest: Properly handle failures vs errors.
Also, rework result printing to be more compatible with CPython. Signed-off-by: Paul Sokolovsky <[email protected]>
1 parent 1b46612 commit e582666

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

python-stdlib/unittest/unittest.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def addTest(self, cls):
208208

209209
def run(self, result):
210210
for c in self._tests:
211-
result.exceptions.extend(run_suite(c, result))
211+
run_suite(c, result)
212212
return result
213213

214214

@@ -217,6 +217,8 @@ def run(self, suite):
217217
res = TestResult()
218218
suite.run(res)
219219

220+
res.printErrors()
221+
print("----------------------------------------------------------------------")
220222
print("Ran %d tests\n" % res.testsRun)
221223
if res.failuresNum > 0 or res.errorsNum > 0:
222224
print("FAILED (failures=%d, errors=%d)" % (res.failuresNum, res.errorsNum))
@@ -235,11 +237,33 @@ def __init__(self):
235237
self.failuresNum = 0
236238
self.skippedNum = 0
237239
self.testsRun = 0
238-
self.exceptions = []
240+
self.errors = []
241+
self.failures = []
239242

240243
def wasSuccessful(self):
241244
return self.errorsNum == 0 and self.failuresNum == 0
242245

246+
def printErrors(self):
247+
print()
248+
self.printErrorList(self.errors)
249+
self.printErrorList(self.failures)
250+
251+
def printErrorList(self, lst):
252+
sep = "----------------------------------------------------------------------"
253+
for c, e in lst:
254+
print("======================================================================")
255+
print(c)
256+
print(sep)
257+
print(e)
258+
259+
def __repr__(self):
260+
# Format is compatible with CPython.
261+
return "<unittest.result.TestResult run=%d errors=%d failures=%d>" % (
262+
self.testsRun,
263+
self.errorsNum,
264+
self.failuresNum,
265+
)
266+
243267

244268
def capture_exc(e):
245269
buf = io.StringIO()
@@ -274,9 +298,15 @@ def run_suite(c, test_result):
274298
print(" skipped:", e.args[0])
275299
test_result.skippedNum += 1
276300
except Exception as ex:
277-
exceptions.append(capture_exc(ex))
278-
print(" FAIL")
279-
test_result.failuresNum += 1
301+
ex_str = capture_exc(ex)
302+
if isinstance(ex, AssertionError):
303+
test_result.failuresNum += 1
304+
test_result.failures.append(((name, c), ex_str))
305+
print(" FAIL")
306+
else:
307+
test_result.errorsNum += 1
308+
test_result.errors.append(((name, c), ex_str))
309+
print(" ERROR")
280310
# Uncomment to investigate failure in detail
281311
# raise
282312
continue
@@ -299,9 +329,5 @@ def test_cases(m):
299329
suite.addTest(c)
300330
runner = TestRunner()
301331
result = runner.run(suite)
302-
if result.exceptions:
303-
sep = "\n----------------------------------------------------------------------\n"
304-
print(sep)
305-
print(sep.join(result.exceptions))
306332
# Terminate with non zero return code in case of failures
307-
sys.exit(result.failuresNum > 0)
333+
sys.exit(result.failuresNum or result.errorsNum)

0 commit comments

Comments
 (0)