HTMLify
TESTS.py
Views: 415 | Author: abh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | class Test: def __init__(self, function): self.passed = 0 self.failed = 0 self.function = function def sfill(self, obj, n=0): if n and n > len(str(obj)): return str(obj) + " " * (n - len(str(obj))) n += 4 return self.sfill( obj, n) def load_test(self): try: exec("from ." + self.function.__name__ + " import TESTCASES as TC", globals(), locals()) TESTCASES = locals()["TC"] except ImportError: return None return TESTCASES def run_tests(self): test_cases = self.load_test() if not test_cases: print("No testcases found for function", self.function.__name__) return None testno = 0 print("Test", "Status", "Arguments ", "Expected", "Returnd", sep="\t") for test_case in test_cases: testno += 1 function_return = self.function(*test_case["positional"], **test_case["keyword"]) if test_case["return"] == function_return: self.passed += 1 status = "\033[32mPassed\033[0m" else: self.failed += 1 status = "\033[31mFailed\033[0m" print( self.sfill(testno, 4), status, self.sfill(test_case["positional"]), test_case["keyword"], self.sfill(test_case["return"], 8), self.sfill(function_return, 8), sep="\t" ) print("\nPassed ", self.passed, "out of ", self.failed + self.passed) |