-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathownpytest.py
More file actions
102 lines (76 loc) · 2.53 KB
/
ownpytest.py
File metadata and controls
102 lines (76 loc) · 2.53 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
import sys
import importlib
from inspect import getmembers, isfunction
import traceback
fixtures_mapping = {}
def get_traceback(ex):
tb = ex.__traceback__
trace = []
while tb is not None:
if tb.tb_next is None:
break
tb = tb.tb_next
trace.append(
{
"filename": tb.tb_frame.f_code.co_filename,
"name": tb.tb_frame.f_code.co_name,
"lineno": tb.tb_lineno,
"traceback": traceback.format_tb(tb),
"message": ex.args,
}
)
return trace
def get_test_files():
for dr in os.listdir(sys.path[0]):
if dr != "tests":
continue
for files in os.listdir(dr):
if not (files.startswith("test") and files.endswith(".py")):
continue
yield dr + "." + files[:-3]
def get_test_functions(file_path):
module = importlib.import_module(file_path)
for members in getmembers(module, isfunction):
if members[0].startswith("test_"):
yield members
def test_runner():
errors = {}
# get folder starts with tests in the current python path
for files in get_test_files():
for test_function_name, test_function_object in get_test_functions(files):
test_args = test_function_object.__code__.co_varnames
# Run the tests here
print("Running test: ", test_function_name)
if errors:
print("\n------------------------ Errors -----------------------------------")
else:
print("\n------------- Success (No errors found) ---------------------------")
class raises:
def __init__(self, exception):
self.exception = exception
def __enter__(self):
pass
def __exit__(self, exc_type, exc_val, exc_tb):
if isinstance(exc_val, self.exception):
return True
else:
raise AssertionError(f"Expected {self.exception}, got {exc_val}")
def parametrize(keys, values):
# keys = "test_input,expected"
# values = [("3+5", 8), ("2+4", 6), ("6*9", 54)]
# output = [{"test_input": "3+5", "expected": 8},
# {"test_input": "2+4", "expected": 6},
# {"test_input": "6*9", "expected": 54}]
def decorator(func):
# the function, to run subtests
def wrapper(*args, **kwargs):
pass
return wrapper
return decorator
def fixture(function):
def fixture_wrapper():
return function()
return fixture_wrapper
if __name__ == "__main__":
test_runner()