1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-04 13:38:28 +00:00

test(core): add setUpClass and tearDownClass to core unit tests

[no changelog]
This commit is contained in:
M1nd3r 2024-11-19 12:07:10 +01:00 committed by Petr Sedláček
parent b1f07fec4f
commit 22a7ba7070

View File

@ -236,38 +236,48 @@ generator_type = type((lambda: (yield))())
def run_class(c, test_result): def run_class(c, test_result):
o = c() o = c()
set_up_class = getattr(o, "setUpClass", lambda: None)
tear_down_class = getattr(o, "tearDownClass", lambda: None)
set_up = getattr(o, "setUp", lambda: None) set_up = getattr(o, "setUp", lambda: None)
tear_down = getattr(o, "tearDown", lambda: None) tear_down = getattr(o, "tearDown", lambda: None)
print("class", c.__qualname__) print("class", c.__qualname__)
for name in dir(o): try:
if name.startswith("test"): set_up_class()
print(" ", name, end=" ...") for name in dir(o):
m = getattr(o, name) if name.startswith("test"):
try: run_test_method(o, name, set_up, tear_down, test_result)
try: finally:
set_up() tear_down_class()
test_result.testsRun += 1
retval = m()
if isinstance(retval, generator_type): def run_test_method(o, name, set_up, tear_down, test_result):
raise RuntimeError( print(" ", name, end=" ...")
f"{name} must not be a generator (it is async, uses yield or await)." m = getattr(o, name)
) try:
elif retval is not None: try:
raise RuntimeError(f"{name} should not return a result.") set_up()
finally: test_result.testsRun += 1
tear_down() retval = m()
print(f"{OK_COLOR} ok{DEFAULT_COLOR}") if isinstance(retval, generator_type):
except SkipTest as e: raise RuntimeError(
print(" skipped:", e.args[0]) f"{name} must not be a generator (it is async, uses yield or await)."
test_result.skippedNum += 1 )
except AssertionError as e: elif retval is not None:
print(f"{ERROR_COLOR} failed{DEFAULT_COLOR}") raise RuntimeError(f"{name} should not return a result.")
sys.print_exception(e) finally:
test_result.failuresNum += 1 tear_down()
except BaseException as e: print(f"{OK_COLOR} ok{DEFAULT_COLOR}")
print(f"{ERROR_COLOR} errored:{DEFAULT_COLOR}", e) except SkipTest as e:
sys.print_exception(e) print(" skipped:", e.args[0])
test_result.errorsNum += 1 test_result.skippedNum += 1
except AssertionError as e:
print(f"{ERROR_COLOR} failed{DEFAULT_COLOR}")
sys.print_exception(e)
test_result.failuresNum += 1
except BaseException as e:
print(f"{ERROR_COLOR} errored:{DEFAULT_COLOR}", e)
sys.print_exception(e)
test_result.errorsNum += 1
def main(module="__main__"): def main(module="__main__"):