forked from asm-products/saulify-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunreport.py
74 lines (55 loc) · 1.93 KB
/
runreport.py
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
import os
import json
import argparse
import colorama
import saulify.sitespec as sitespec
SPEC_DIRECTORY = "sitespecs"
colorama.init()
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--pretty", help="Pretty print test results",
action="store_true")
args = parser.parse_args()
def test_passed(report):
""" Whether all components of a scraper test succeeded """
if report["status"] != "OK":
return False
for result in report["result"].values():
if result["missing"]:
return False
return True
def print_report(report):
""" Converts test report dictionary to a human-readable format """
if report["status"] == "OK":
result = "PASS" if test_passed(report) else "FAIL"
else:
result = "EXCEPTION"
result_colors = {
"PASS": colorama.Fore.GREEN,
"FAIL": colorama.Fore.RED,
"EXCEPTION": colorama.Fore.RED
}
print(result_colors[result] + "{0} : {1}".format(result, report["url"]))
if report["status"] == "EXCEPTION":
print(report["message"])
elif test_passed(report):
r = report["result"]
stats = ", ".join(["{0} {1}".format(len(r[c]["found"]), c) for c in r])
print("Found " + stats)
else:
for category, result in report["result"].items():
if result["missing"]:
count = len(result["missing"])
print("Missing {0} {1}:".format(count, category))
for item in result["missing"]:
print(item)
if __name__ == "__main__":
for fname in os.listdir(SPEC_DIRECTORY):
fpath = os.path.join(SPEC_DIRECTORY, fname)
test_cases = sitespec.load_testcases(fpath)
for test_case in test_cases:
report = test_case.run()
if args.pretty:
print_report(report)
print("\n")
else:
print(json.dumps(report))