-
Notifications
You must be signed in to change notification settings - Fork 16
/
functional_test_csv_to_testbook.py
92 lines (70 loc) · 2.94 KB
/
functional_test_csv_to_testbook.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import csv, yaml, os
def migrate(infile, outfile, suite, testset):
with open(infile, "r") as f:
reader = csv.reader(f)
rows = [row for row in reader]
rows = rows[1:]
last_row_empty = False
tests = []
current_test = {}
current_step = {}
for row in rows:
if _empty(row) or _note_row(row):
last_row_empty = True
continue
if _test_title(row) and last_row_empty:
if len(current_step.keys()) > 0:
current_test["steps"].append(current_step)
current_step = {}
if len(current_test.keys()) > 0:
tests.append(current_test)
current_test = {"title": row[2].strip(), "context" : {"role" : ""}, "steps": []}
else: # this is a regular test row
if len(current_test.keys()) == 0:
raise Exception("Attempt to add step before test is initialised")
if row[1]:
current_test["context"]["role"] = row[1].strip()
if row[2]:
if len(current_step.keys()) > 0:
current_test["steps"].append(current_step)
current_step = {"step": row[2].strip()}
if row[3]:
if "results" not in current_step:
current_step["results"] = []
current_step["results"].append(row[3].strip())
last_row_empty = False
if len(current_step.keys()) > 0:
current_test["steps"].append(current_step)
if len(current_test.keys()) > 0:
tests.append(current_test)
testset = {
"suite": suite,
"testset": testset,
"tests": tests
}
outdir = os.path.dirname(outfile)
os.makedirs(outdir, exist_ok=True)
with open(outfile, "w") as f:
yaml.safe_dump(testset, stream=f, sort_keys=False)
def _test_title(row):
return row[1] == "" and row[2] != "" and row[3] == ""
def _empty(row):
return row[0] == "" and row[1] == "" and row[2] == "" and row[3] == ""
def _note_row(row):
return row[0] != "" and row[1] == "" and row[2] == "" and row[3] == ""
def migrate_all(indir, outdir):
for fn in os.listdir(indir):
print(fn)
rawname = fn[:-1 * len(".csv")]
suite, testset = rawname.split(" - ")
path = os.path.join(indir, fn)
subpath = os.path.join(_safe_name(suite), _safe_name(testset) + ".yml")
out = os.path.join(outdir, subpath)
migrate(path, out, suite, testset)
def _safe_name(name):
return name.lower().replace(" ", "_")
# migrate("/home/richard/tmp/doaj/functionaltests/Application State Change - Application State Change.csv",
# "/home/richard/Code/External/doaj3/doajtest/testbook/migrate/application_state_change/application_state_change.yml",
# "Application State Change",
# "Application State Change")
migrate_all("/home/richard/tmp/doaj/functionaltests", "/home/richard/Code/External/doaj3/doajtest/testbook/migration/")