-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy pathrun_tests.py
More file actions
88 lines (74 loc) · 2.48 KB
/
Copy pathrun_tests.py
File metadata and controls
88 lines (74 loc) · 2.48 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
import os, sys, importlib.util, traceback, mechanicalsoup
from tests.test_helpers import BASE_URL, ADMIN_PASSWORD, ROOT_DIR
FAILED = False
def remove_config():
yaml_path = os.path.join(ROOT_DIR, "config/baikal.yaml")
if os.path.exists(yaml_path):
try:
with open(yaml_path, "r", encoding="utf-8") as f:
content = f.read()
if "abbc0ffed774e98a495fe5a2596ed7deab14211f250673ad661be7b759c9a7fd" not in content:
# If this is contained, it is a test file that can safely be deleted.
# Keep in sync with mod.ADMIN_PASSWORD
assert False, "There already is a config file that was not created by a test. Stopping."
os.remove(yaml_path)
except Exception as e:
print("Error while checking baikal.yaml:", e)
sys.exit(1)
def load_tests(path):
files = []
for r, _, f in os.walk(path):
for x in f:
if x.endswith(".py"):
files.append(os.path.join(r, x))
assert files != []
return sorted(files)
def run_file(path):
global FAILED
test_folder = os.path.dirname(os.path.abspath(path))
if test_folder not in sys.path:
sys.path.insert(0, test_folder)
spec = importlib.util.spec_from_file_location("mod", path)
mod = importlib.util.module_from_spec(spec)
try:
spec.loader.exec_module(mod)
except:
print("ERR in import:", path)
FAILED = True
traceback.print_exc()
return
setup_function = None
for s in dir(mod):
if s == "setup":
setup_function = getattr(mod, s)
for n in dir(mod):
if not n.startswith("test_"):
continue
print(path, "::", n, sep = '')
remove_config()
browser = mechanicalsoup.StatefulBrowser()
test_function = getattr(mod, n)
try:
setup_function()
test_function(browser)
print("[OK]")
except:
FAILED = True
traceback.print_exc()
print(browser.get_current_page())
print("[FAIL]: ", path, "::", n, sep = '')
def main():
global FAILED
if len(sys.argv) > 1:
run_file(sys.argv[1])
else:
for f in load_tests("tests"):
run_file(f)
if FAILED:
print("Some tests failed.")
sys.exit(1)
else:
print("All tests passed.")
sys.exit(0)
if __name__ == "__main__":
main()