48 lines
976 B
Python
48 lines
976 B
Python
"""
|
|
Helper functions for a basic test suite
|
|
"""
|
|
|
|
from typing import Callable
|
|
from enum import StrEnum
|
|
from dataclasses import dataclass
|
|
from traceback import print_exc
|
|
|
|
|
|
class TestResult(StrEnum):
|
|
PASS = "PASS"
|
|
FAIL = "FAIL"
|
|
SKIP = "SKIP"
|
|
|
|
|
|
@dataclass
|
|
class TestInfo:
|
|
suite_name: str
|
|
test_name: str
|
|
result: TestResult
|
|
|
|
def __str__(self):
|
|
# TODO colour?
|
|
return f"[{self.suite_name}.{self.test_name}] {self.result}"
|
|
|
|
|
|
|
|
def run_test(suite_name: str, test_fn: Callable, do_skip = False) -> TestResult:
|
|
test_name = test_fn.__name__
|
|
|
|
print(f"[{suite_name}.{test_name}] Running...")
|
|
|
|
if do_skip:
|
|
res = TestInfo(suite_name, test_name, TestResult.SKIP)
|
|
else:
|
|
try:
|
|
test_fn()
|
|
res = TestInfo(suite_name, test_name, TestResult.PASS)
|
|
except AssertionError:
|
|
res = TestInfo(suite_name, test_name, TestResult.FAIL)
|
|
print_exc()
|
|
|
|
|
|
|
|
print(res)
|
|
return res
|