-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPythonTester.py
324 lines (285 loc) · 11.5 KB
/
PythonTester.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 24 20:49:00 2016
@author: peter
"""
import os
import importlib
import sys, getopt
import json
import csv
import random
import copy
import time
#### Settings
## Init Settings
# Available modules in modules folder
testModules = []
# Available functions in modules
testFunctions = {}
## Settings Creation
# Extra variables injection into modul variable list
commonVariables = {
"Arg":[],
"ModuleIteration":1,
"FunctionIteration":1
}
# Available settings available in modules
settings = {
"common":{
"serverUrl":"https://europe.slamby.com/demo/",
"secret":"s3cr3t"
},
"moduleSettings":{}
}
## Test Plan Creation
# Custom settings read from custom settings file. -p
customSettings = {}
# Test Plan Container
testPlan = {}
randomTestPlan = []
## Test
# Test Instances
testInstances = {}
# Test Data
testResult = {
"stat":{},
"result":[]
}
## ---------------------
## Helpers
## ---------------------
# Helper
def loadClass(module):
return getattr(importlib.import_module("modules.{0}".format(module)), module)
# Helper
def exportToJson(fileName,content):
if not os.path.exists(os.path.dirname(fileName)):
try:
os.makedirs(os.path.dirname(fileName))
except OSError as e: # Guard against race condition
print(e)
with open(fileName,"w") as outfile:
json.dump(content,outfile,sort_keys = True, indent = 4)
# Helper
def importJson(fileName):
with open(fileName) as setting_file:
return json.load(setting_file)
# Helper
def exportDictToCsv(fileName,dictionary):
if not os.path.exists(os.path.dirname(fileName)):
try:
os.makedirs(os.path.dirname(fileName))
except OSError as e: # Guard against race condition
print(e)
with open(fileName,"wb") as f:
w = csv.DictWriter(f,dictionary[0].keys())
w.writeheader()
w.writerow(dictionary)
# Helper
def importJsonFromCsv(fileName):
result = []
with open(fileName,"rb") as csvfile:
reader = csv.DictReader(csvfile)
for item in reader:
result.append(item)
return result
# Helper
def exportListToCsv(fileName,content):
if not os.path.exists(os.path.dirname(fileName)):
try:
os.makedirs(os.path.dirname(fileName))
except OSError as e: # Guard against race condition
print(e)
with open(fileName,"wb") as f:
w = csv.writer(f,quoting=csv.QUOTE_ALL)
for row in content:
m = []
for item in row:
if type(item) is dict and len(item) > 0:
m.append(json.dumps(item, separators=(',',':')))
m.append(item)
w.writerow(m)
# Helper
def readJsonFromString(string):
return json.loads(string)
## ---------------------
## Init
## ---------------------
# Detecting modules in modules folder
modules = os.listdir("./{0}".format("modules/"))
for module in modules:
if module.endswith(".py") and module != "__init__.py":
testModules.append("{0}".format(os.path.splitext(module)[0]))
# Detecting functions in available modules
# Create settings template file
for module in testModules:
if module not in settings.keys():
settings["moduleSettings"][module] = {}
testFunctions[module] = []
clss = loadClass(module)()
for item in dir(clss):
if item[0] == "_" and item[1] is not "_":
testFunctions[module].append(item)
settings["moduleSettings"][module] = vars(clss)
for commonVariable in commonVariables.keys():
if commonVariable not in settings["moduleSettings"][module].keys():
settings["moduleSettings"][module][commonVariable] = commonVariables[commonVariable]
# Create Test Plan from settings file
def testPlanner():
global testFunctions
global testPlan
for module in customSettings["moduleSettings"]:
moduleInstanceNumber = len(customSettings["moduleSettings"][module]["Arg"]) if len(customSettings["moduleSettings"][module]["Arg"]) > 0 else 1
moduleIteration = customSettings["moduleSettings"][module]["ModuleIteration"] if customSettings["moduleSettings"][module]["ModuleIteration"] > 0 else 1
functionIteration = customSettings["moduleSettings"][module]["FunctionIteration"] if customSettings["moduleSettings"][module]["FunctionIteration"] > 0 else 1
for instanceArgNumber in range(0,moduleInstanceNumber):
if moduleIteration > 1:
for moduleIterationNumber in range(0,moduleIteration):
instanceName = "{0}_{1}_{2}".format(module,instanceArgNumber,moduleIterationNumber)
if instanceName not in testPlan.keys():
testPlan[instanceName] = {"settings":customSettings["moduleSettings"][module],"actions":[],"moduleName":module}
if len(testPlan[instanceName]["actions"]) == 0:
testPlan[instanceName]["actions"].append({"action":"Start","result":{}})
for functionIterationNumber in range(0,functionIteration):
for function in testFunctions[module]:
testPlan[instanceName]["actions"].append({"action":function,"result":{}})
testPlan[instanceName]["actions"].append({"action":"Stop","result":{}})
# Check for common settings options
for setting in testPlan[instanceName]["settings"]:
if testPlan[instanceName]["settings"][setting] == "":
if setting in customSettings["common"].keys():
testPlan[instanceName]["settings"][setting] = customSettings["common"][setting]
else:
instanceName = "{0}_{1}_{2}".format(module,instanceArgNumber,0)
if instanceName not in testPlan.keys():
testPlan[instanceName] = {"settings":customSettings["moduleSettings"][module],"actions":[],"moduleName":module}
if len(testPlan[instanceName]["actions"]) == 0:
testPlan[instanceName]["actions"].append({"action":"Start","result":{}})
for functionIterationNumber in range(0,functionIteration):
for function in testFunctions[module]:
testPlan[instanceName]["actions"].append({"action":function,"result":{}})
testPlan[instanceName]["actions"].append({"action":"Stop","result":{}})
# Check for common settings options
for setting in testPlan[instanceName]["settings"]:
if testPlan[instanceName]["settings"][setting] == "":
if setting in customSettings["common"].keys():
testPlan[instanceName]["settings"][setting] = customSettings["common"][setting]
newPlan = {}
for instanceName in testPlan:
newPlan[instanceName] = copy.deepcopy(testPlan[instanceName])
if len(newPlan[instanceName]["settings"]["Arg"]) > 0:
a = testPlan[instanceName]["settings"]["Arg"][int(instanceName.split("_")[1])]
newPlan[instanceName]["settings"]["Arg"] = a
# Rewrite modified plan
testPlan = newPlan
# Randomizer
def randomTestGenerator():
global testPlan
# Random instance selection
randomInstanceName = testPlan.keys()[random.randint(0,len(testPlan.keys())-1)]
# If Start method still exists
if testPlan[randomInstanceName]["actions"][0]["action"] == "Start":
settings = testPlan[randomInstanceName]["settings"].copy()
if len(testPlan[randomInstanceName]["settings"]["Arg"]) == 0:
settings["Arg"] = ""
addToRandomTestPlan(
randomInstanceName,
testPlan[randomInstanceName]["moduleName"],
"Start",
settings
)
del testPlan[randomInstanceName]["actions"][0]
if len(testPlan[randomInstanceName]["actions"]) > 1:
actionIndex = random.randint(0,len(testPlan[randomInstanceName]["actions"])-2)
else:
actionIndex = 0
addToRandomTestPlan(
randomInstanceName,
testPlan[randomInstanceName]["moduleName"],
testPlan[randomInstanceName]["actions"][actionIndex]["action"],
{}
)
del testPlan[randomInstanceName]["actions"][actionIndex]
if len(testPlan[randomInstanceName]["actions"]) == 0:
del testPlan[randomInstanceName]
if len(testPlan) > 0:
randomTestGenerator()
def addToRandomTestPlan(instanceName,module,action,settings):
global randomTestPlan
if len(randomTestPlan) == 0:
randomTestPlan.append(["InstanceName","Module","Action","Settings"])
randomTestPlan.append([instanceName,module,action,settings])
def tester(fileName):
global testInstances
for testCase in importJsonFromCsv(fileName):
# Create Instance if new test case start
if testCase["Action"] == "Start":
testInstances[testCase["InstanceName"]] = loadClass(testCase["Module"])()
# Settings auto loading
settings = readJsonFromString(testCase["Settings"])
for variable in settings:
# if variable exists in Instance Class
instanceVariables = vars(testInstances[testCase["InstanceName"]])
if variable in instanceVariables.keys():
setattr(testInstances[testCase["InstanceName"]],variable,settings[variable])
# Run Test
test(
testCase["InstanceName"],
testInstances[testCase["InstanceName"]],
testCase["Action"]
)
def test(instanceName,instance,action):
start = time.time()
result = getattr(instance,action)()
stop = time.time()
timeElapsed = round(stop-start,5)
log(
instanceName,
action,
result,
timeElapsed
)
def log(instanceName,action,result,elapsedTime):
global testResult
if len(testResult["result"]) == 0:
testResult["result"].append(["Instance","Action","Result","Elapsed Time"])
testResult["result"].append([instanceName,action,result,elapsedTime])
## ---------------------
## Console Level
## ---------------------
def main(argv):
global customSettings
faq = '''
python PythonTester.py
-h (print help message)
-s <SettingsFileName> (generate settings template)
-p <SettingsFileName> (generate test plan from settings file)
-t <TestPlanFileName>
'''
try:
opts, args = getopt.getopt(argv,"hi:s:p:t:",["i=","s=","p=","t="])
except getopt.GetoptError:
print (faq)
sys.exit(2)
# Manage available options
for opt, arg in opts:
if opt == '-h':
print(faq)
sys.exit()
elif opt in ("-s", "--ofile"):
fileName = "test_settings.json"
if arg is not "":
fileName = arg
exportToJson(fileName,settings)
elif opt in ("-p", "--ofile"):
customSettings = importJson(arg)
testPlanner()
exportToJson("{0}/plan.json".format(os.path.dirname(arg)),testPlan)
randomTestGenerator()
exportListToCsv("{0}/plan.random.csv".format(os.path.dirname(arg)),randomTestPlan)
elif opt in ("-t", "--ofile"):
tester(arg)
exportListToCsv("{0}/result.csv".format(os.path.dirname(arg)),testResult["result"])
if __name__ == "__main__":
main(sys.argv[1:])