unittest: Run test_* functions as well as TestCase classes.

pull/488/head
Andrew Leech 2022-03-18 14:58:20 +11:00
rodzic c7eb3de858
commit 9d9ca3d59b
1 zmienionych plików z 12 dodań i 2 usunięć

Wyświetl plik

@ -332,7 +332,10 @@ def run_suite(c, test_result, suite_name=""):
# raise # raise
finally: finally:
tear_down() tear_down()
o.doCleanups() try:
o.doCleanups()
except AttributeError:
pass
if hasattr(o, "runTest"): if hasattr(o, "runTest"):
name = str(o) name = str(o)
@ -340,11 +343,16 @@ def run_suite(c, test_result, suite_name=""):
return return
for name in dir(o): for name in dir(o):
if name.startswith("test"): if name.startswith("test_"):
m = getattr(o, name) m = getattr(o, name)
if not callable(m): if not callable(m):
continue continue
run_one(m) run_one(m)
if callable(o):
name = o.__name__
run_one(o)
return exceptions return exceptions
@ -354,6 +362,8 @@ def main(module="__main__"):
c = getattr(m, tn) c = getattr(m, tn)
if isinstance(c, object) and isinstance(c, type) and issubclass(c, TestCase): if isinstance(c, object) and isinstance(c, type) and issubclass(c, TestCase):
yield c yield c
elif tn.startswith("test_") and callable(c):
yield c
m = __import__(module) if isinstance(module, str) else module m = __import__(module) if isinstance(module, str) else module
suite = TestSuite(m.__name__) suite = TestSuite(m.__name__)