Skip to content

Commit f92833b

Browse files
pfalconpi-anl
authored andcommitted
unittest: Support TestCase subclasses with own runTest() method.
E.g. for doctest. Signed-off-by: Paul Sokolovsky <[email protected]>
1 parent ac282d8 commit f92833b

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

python-stdlib/unittest/unittest.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -302,36 +302,44 @@ def run_suite(c, test_result):
302302
set_up = getattr(o, "setUp", lambda: None)
303303
tear_down = getattr(o, "tearDown", lambda: None)
304304
exceptions = []
305+
306+
def run_one(m):
307+
print("%s (%s) ..." % (name, c.__qualname__), end="")
308+
set_up()
309+
try:
310+
test_result.testsRun += 1
311+
m()
312+
print(" ok")
313+
except SkipTest as e:
314+
print(" skipped:", e.args[0])
315+
test_result.skippedNum += 1
316+
except Exception as ex:
317+
ex_str = capture_exc(ex)
318+
if isinstance(ex, AssertionError):
319+
test_result.failuresNum += 1
320+
test_result.failures.append(((name, c), ex_str))
321+
print(" FAIL")
322+
else:
323+
test_result.errorsNum += 1
324+
test_result.errors.append(((name, c), ex_str))
325+
print(" ERROR")
326+
# Uncomment to investigate failure in detail
327+
# raise
328+
finally:
329+
tear_down()
330+
o.doCleanups()
331+
332+
if hasattr(o, "runTest"):
333+
name = str(o)
334+
run_one(o.runTest)
335+
return
336+
305337
for name in dir(o):
306338
if name.startswith("test"):
307339
m = getattr(o, name)
308340
if not callable(m):
309341
continue
310-
print("%s (%s) ..." % (name, c.__qualname__), end="")
311-
set_up()
312-
try:
313-
test_result.testsRun += 1
314-
m()
315-
print(" ok")
316-
except SkipTest as e:
317-
print(" skipped:", e.args[0])
318-
test_result.skippedNum += 1
319-
except Exception as ex:
320-
ex_str = capture_exc(ex)
321-
if isinstance(ex, AssertionError):
322-
test_result.failuresNum += 1
323-
test_result.failures.append(((name, c), ex_str))
324-
print(" FAIL")
325-
else:
326-
test_result.errorsNum += 1
327-
test_result.errors.append(((name, c), ex_str))
328-
print(" ERROR")
329-
# Uncomment to investigate failure in detail
330-
# raise
331-
continue
332-
finally:
333-
tear_down()
334-
o.doCleanups()
342+
run_one(m)
335343
return exceptions
336344

337345

0 commit comments

Comments
 (0)