common.py
7.81 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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import operator
import configparser
import requests
import json
from commons.Logging import Logger
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
log=Logger()
def get_global_config(file,section, key):
current_path=os.path.dirname(__file__)
src_path=os.path.dirname(current_path)
# global_setting_path=src_path+'/config/global_data.conf'
global_setting_path=src_path+'/config/'+file+".conf"
#验证文件是否存在
file_path = os.path.exists(global_setting_path)
if file_path:
#获取文件的数据
if isinstance(key,int):
log.error("key of section cannot be int Type :<%r> "%str(key))
raise Exception("key of section cannot be int Type :<%r> "%str(key))
else:
config = configparser.ConfigParser()
config.read(global_setting_path,encoding="utf-8")
return config.get(section, key)
else:
log.error("File Not Exist :<%r> "%str(global_setting_path))
raise Exception("File Not Exist :<%r> "%str(global_setting_path))
def get_api_config(api,section,key):
current_path=os.path.dirname(__file__)
src_path=os.path.dirname(current_path)
api_path=src_path+"/config/api/"
api_config_path=api_path+api+".conf"
#验证文件是否存在
file_path = os.path.exists(api_config_path)
if file_path:
#获取文件的数据
if isinstance(key,int):
log.error("key of section cannot be int Type :<%r> "%str(key))
raise Exception("key of section cannot be int Type :<%r> "%str(key))
else:
config = configparser.ConfigParser()
config.read(api_config_path,encoding="utf-8")
return config.get(section, key)
else:
log.error("File Not Exist :<%r> "%str(api_config_path))
raise Exception("File Not Exist :<%r> "%str(api_config_path))
def set_priority(priority_list):
#用户自定义优先级方法,设定有需要运行的用例优先级
global _global_priority
if priority_list:
_global_priority=priority_list
return _global_priority
else:
_global_priority=[1,2,3]
return _global_priority
def set_mark(case_mark):
#用户自定义优先级方法,设定有需要运行的用例优先级
global _global_mark
if case_mark:
_global_mark=case_mark
return _global_mark
else:
_global_mark=False
return _global_mark
def read_priority_list():
#需要重新设定一个方法来读取配置的全局优先级变量,且必须在全局变量初始化后使用,不然会报错
priority_list=[1,2,3]
try:
return _global_priority
except Exception as e:
return priority_list
def read_case_mark():
#需要重新设定一个方法来读取配置的全局优先级变量,且必须在全局变量初始化后使用,不然会报错
casemark=False
try:
return _global_mark
except Exception as e:
return casemark
def mark(tag=None,p=None):
#此处的try主要用于,当用户需要直接运行单个用例文件时,没提前_global_priority和_global_mark初始化
try:
#用户没有输入任何值
if _global_priority == [1,2,3] and _global_mark == False :
return True
#只指定用例标签
elif _global_priority == [1,2,3] and _global_mark != False:
if tag == _global_mark:
return True
else:
return False
#只指定用例优先级
elif _global_priority != [1,2,3] and _global_mark == False:
if p in _global_priority:
return True
else:
return False
#同时指定优先级和标签
elif _global_priority != [1,2,3] and _global_mark != False:
if p in _global_priority and tag == _global_mark:
return True
else:
return False
except Exception as e:
return True
def get_session(name,password):
pass
return "header"
def get_token(name,password):
pass
return "token"
def get_request_Url(api):
return get_api_config(api,'RequestUrl', 'url')
def get_request_Json(api):
return get_api_config(api,'JsonStructrue', 'requestJson')
def dict_diff(a1,b1):
#提取出dict与str或list的异常对比场景
if type(a1)!= type(b1):
print("实际响应json的元素{}的类型{}和期望响应json元素{}的类型{}不一致".format(a1,type(a1),b1,type(b1)))
return False
d1=list(set(a1.keys()).difference(set(b1.keys())))
d2=list(set(b1.keys()).difference(set(a1.keys())))
if len(d1)==0 and len(d2)==0:
return True
else:
print("实际json比期望json多了键值",d1)
print("实际json比期望json少了键值",d2)
return False
def list_diff(a1,b1):
#提取出list与str或dict的异常对比场景
if type(a1)!= type(b1):
print("实际响应json的元素{}的类型{}和期望响应json元素{}的类型{}不一致".format(a1,type(a1),b1,type(b1)))
return False
elif len(a1)!=len(b1):
print("实际响应json列表长度{}和期望响应json列表长度{}不一致".format(a1,b1))
return False
else:
return True
#无法判断[[1],[2]]和[[1],[1]]的区别,只能判断列表长度和字典键值是否一致
#a为实际对比的json数据,b为期望的json数据
def compare_json(a,b):
#判断检查数据是否为列表格式的数据
if isinstance(a,list):
if list_diff(a,b)==True:
#为列表时必须进行排序,否则可能出现列表对比的位置不一致
a.sort()
b.sort()
for i in range(len(a)):
#判断列表中的指定的元素是否为字典格式
if compare_json(a[i],b[i]) == False:
return False
else:
return False
#判断检查数据是否为字典格式的数据
elif isinstance(a,dict):
if dict_diff(a,b)==True:
#如果第一层键值相同时,遍历检查内层所有键值
for key in a.keys():
#内层键值为列表数据类型时
if compare_json(a[key],b[key]) == False:
return False
else:
return False
#期望数据非dict和list类型时,直接进行对比,可能会导致{"code":"200"},{"code":"201"}出现不同的情况,所以注释下面的结果
# elif operator.eq(a, b)==False:
# print("实际数据的{}和期望数据的{}不一致".format(a,b))
# return False
#接口返回值是可控的情况可以用下面这个方法
def cmp_json(actual_data,expect_data):
log.info("======对比期望和实际响应Json格式======")
try:
if len(expect_data)!=0 and len(actual_data)!=0:
expect_data=json.loads(expect_data)
result=compare_json(actual_data,expect_data)
if result==None:
log.info("响应数据和期望数据的Json格式一致")
return True
else:
log.error("66响应数据和期望数据的Json格式不一致")
return False
elif len(expect_data)!=0 and len(actual_data)==0:
log.error("对比数据中,实际响应数据为空")
assert False
elif len(expect_data)==0 and len(actual_data)!=0:
log.error("对比数据中,期望响应数据为空,请检查配置文件")
assert False
elif len(expect_data)==0 and len(actual_data)==0:
log.error("对比数据都为空")
assert False
except Exception as e:
log.error("对比响应数据和期望数据的Json格式 ERROR == %s"%e)
return False