-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunittest-generator.py
More file actions
233 lines (191 loc) · 7.79 KB
/
unittest-generator.py
File metadata and controls
233 lines (191 loc) · 7.79 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import jinja2
import yaml
import os
import mock
import contextlib
import re
env = jinja2.Environment(
trim_blocks=True,
lstrip_blocks=True,
loader=jinja2.FileSystemLoader('.')
)
template = env.get_template('unittest.pyt')
filelist = os.listdir("./schemas")
filelist = [name for name in filelist if ".yml" in name and ".swp" not in name]
for i in range(len(filelist)):
filelist[i] = filelist[i].replace(".yml", "")
def patch_func(func, config, method_name, mock_name):
def run(*args, **kwargs):
error = None
try:
result = func(*args, **kwargs)
result_type = 'success'
except Exception as e:
error = e
result = e.__class__.__name__
result_type = 'error'
for case in config["methods"][method_name]["mocks"][mock_name]["cases"]:
if case["args"] == args and case["kwargs"] == kwargs:
return
config["methods"][method_name]["mocks"][mock_name]["cases"].append(
{ "args": list(args), "kwargs": kwargs, "return_value": result, "return_type": result_type }
)
if error:
raise(error)
return result
return run
def config_padding(config):
for key, value in config["methods"].iteritems():
value["mocks"] = {}
for call in value["calls"]:
call["args"] = [] if "args" not in call else call["args"]
call["kwargs"] = [] if "kwargs" not in call else call["kwargs"]
for case in value["cases"]:
case["args"] = [] if "args" not in case else case["args"]
case["kwargs"] = [] if "kwargs" not in case else case["kwargs"]
return config
def find_mocks(config) :
try:
if config["package"] == "":
_module = __import__(config["module"])
else:
_module = __import__(config["package"], fromlist = [config["module"]])
except ImportError:
# Todo: Display error message
return
mock_modules = [ config["module"] + '.' + mock_module for mock_module in config["mock"] ]
for key, value in config["methods"].iteritems():
_attr = _module
for call in value["calls"]:
try:
_attr = getattr(_attr, call["name"])
if call["type"] == "last_method":
_method = _attr
elif call["type"] == "method":
call["args"] = [] if "args" not in call else call["args"]
call["kwargs"] = [] if "kwargs" not in call else call["kwargs"]
_attr = _attr(*call["args"], **call["kwargs"])
elif call["type"] == "variable":
pass
else:
pass
except Exception as e:
# Todo: Display error message
pass
for case in value["cases"]:
with contextlib.nested(mock.patch(*mock_modules)) as mocks:
try:
result = _method(*case["args"], **case["kwargs"])
except Exception as e:
# print e.__class__.__name__
pass
for index, m in enumerate(mock_modules):
for mcall in mocks[index].method_calls:
real_call = re.search("^[^(]*", repr(mcall)).group().replace("call", m)
if real_call not in value["mocks"]:
value["mocks"][real_call] = {
"calls": real_call.replace(config["module"] + ".", "").split('.'),
"cases": []
}
return config
def real_run(config):
try:
if config["package"] == "":
_module = __import__(config["module"])
else:
_module = __import__(config["package"], fromlist = [config["module"]])
except ImportError:
# Todo: Display error message
return
mock_modules = [ config["module"] + '.' + mock_module for mock_module in config["mock"] ]
for key, value in config["methods"].iteritems():
_attr = _module
for name, content in value["mocks"].iteritems():
for call in content["calls"][:-1]:
try:
_attr = getattr(_attr, call)
except Exception as e:
# Todo: Display error message
pass
try:
setattr(_attr, content["calls"][-1], patch_func(getattr(_attr, content["calls"][-1]), config, key, name))
except Exception as e:
# Todo: Display error message
pass
_attr = _module
for call in value["calls"]:
try:
_attr = getattr(_attr, call["name"])
if call["type"] == "last_method":
_method = _attr
elif call["type"] == "method":
call["args"] = [] if "args" not in call else call["args"]
call["kwargs"] = [] if "kwargs" not in call else call["kwargs"]
_attr = _attr(*call["args"], **call["kwargs"])
elif call["type"] == "variable":
pass
else:
pass
except Exception as e:
# Todo: Display error message
pass
for case in value["cases"]:
try:
case["result"] = _method(*case["args"], **case["kwargs"])
case["result_type"] = "success"
except Exception as e:
case["result"] = e.__class__.__name__
case["result_type"] = "error"
return config
def repr_config(config):
for key, value in config["methods"].iteritems():
for case in value["cases"]:
case["result"] = repr(case["result"])
for index, arg in enumerate(case["args"]):
case["args"][index] = repr(arg)
for kw, arg in case["kwargs"].iteritems():
case["kwargs"][kw] = repr(arg)
for s, mock in value["mocks"].iteritems():
for case in mock["cases"]:
case["return_value"] = repr(case["return_value"])
for index, arg in enumerate(case["args"]):
case["args"][index] = repr(arg)
for kw, arg in case["kwargs"].iteritems():
case["kwargs"][kw] = repr(arg)
return config
def gen_model():
for testcase in filelist:
with open('./schemas/%s.yml' % testcase) as ifile:
config = yaml.load(ifile)
config = config_padding(config)
config = find_mocks(config)
config = real_run(config)
config = repr_config(config)
content = template.render(**config)
with open('./test_files/test_%s.py' % testcase, 'w') as ofile:
ofile.write(content)
def run_test():
for testcase in filelist:
try:
os.system("python ./test_files/test_%s.py" % testcase)
except:
pass
gen_model()
run_test()
"""
I want the config be the form when using in the template, (repr before giving to template)
get_object:
calls:
- { name: 'GraphAPI', args: [], kwargs: {access_token: 'post_id', version: '2.2'}, type: 'method' }
- { name: 'get_object', type: 'last_method' }
cases:
- { args: [], kwargs: {id: 'post_id'}, result: object, result_type: 'error/success' }
mocks:{
'facebook.requests.request': {
calls: ['requests', 'request']
cases:
- { args: ['GET', 'http://www.google.com.tw'], kwargs: {}, return_value: '<HTTP 200>', return_type: 'success' }
- { args: [], kwargs: {}, return_value: 'ParameterError?', return_type: 'error'}
},
}
"""