1 # 接上节课-框架准备后
2 1、把测试用例写入Excel上
3 规范:id title method url data expeced
4 2、运用第三方库;openpyxl
5 # 首先确保终端或者pychrm pip安装路径一致;没有安装可以自行安装
6 pip install openpyxl
7 对于Excel上的用例: 行 row 列 column 列
8 from openpyxl import load_workbook
9 # excle的文件路径
10 excel_path = "测试用例Excel使用的路径"
11 # 1.加载一个excle,得到工作薄 workbook
12 wb = load_work(excel_path)
13 # 2、选择一个表单 -通过表单名 sheet
14 sh = wb["xx接口"]
15 # 3、在选择的表单当中,读取某个单元格的数据,修改/写入数据到某个单元格 cell
16 # 行号和列号都是从1开始
17 # 读取
18 cell_value = sh.cell(2, 3).value # 读取
19 print(cell_value)
20
21 # 得到当前sheet的总行号,总列号
22 row_nums = sh.max_row
23 col_nums = sh.max_column
24
25 # 只读取第一行(作为key)
26 # 行号是1 通过代码自动得到列号
27 # keys = []
28 # for col_index in range(1, sh.max_column + 1):
29 # keys.append(sh.cell(1, col_index).value)
30 # print(keys)
31
32 # # 遍历行号,取第一行
33 # for row_index in range(2, sh.max_row + 1):
34 # values = []
35 # # 在每一行里面,从第1列开始,获取所有列的值
36 # for col_index in range(1, sh.max_column + 1):
37 # values.append(sh.cell(row_index, col_index).value)
38 # # keys和values打包 - zip函数
39 # case = dict(zip(keys, values))
40 # print(case)
41
42 # 方式二 -
43 data = list(sh.values)
44 print(data)
45 keys = data[0] # 获取所有的列名
46 all_data = []
47 for row in data[1:]:
48 row_dict = dict(zip(keys,row))
49 all_data.append(row_dict)
50
51
52 # # 列表推导式 列表名 = [值 表达式]
53 # keys = [sh.cell(1, col_index).value for col_index in range(1, sh.max_column + 1)]
54
55
56 # # 读取所有行
57 # for row in sh.rows:
58 # # print(row)
59 # for item in row:
60 # print(item.value, end=" ")
61 # print()
62
63 # # 给某个单元格写入值
64 # sh.cell(2, 3).value = "get"
65 #
66 # # 一旦做了修改,就要保存
67 # # filename如果不是打开的excel文件,那就是另存为
68 # # 如果是打开的excel文件,保存到原文件中
69 # # 保存的时候,要保证没有其它程序在使用当前文件。否则会报Permission Error
70 # wb.save(excel_path)
71
72 3、编写脚本遍历Excel上的测试用例读取
73 4、拓展知识点:
74 4.1、zip用法
75 a = ["class", "teacher", "student"]
76 b = ["py37", "xj", "many", "hello"]
77 c = [1, 2, 3]
78
79 res = dict(zip(a,b))
80 print(res)
81
82 res = list(zip(a,b,c))
83 print(res)
84 4.2、josn与字典的区别
85 import json
86 """
87 python: None
88 java/javascript: null(python不认识。如果响应结果当中有null, 需要转换成None)
89
90 字典:数据类型。
91 json: 数据格式。 json格式的字符串
92
93 内置库:json
94 json.loads() 把json串,转换成python字典
95 json.dumps() 把python字典,转换成json串
96
97 关于requests处理json参数的文章:
98 https://www.cnblogs.com/Simple-Small/p/9830270.html
99
100 """
101
102 req_data = '{"mobile_phone": "18610100022","pwd": "123456789","reg_name": "py37小简", "test": null}'
103 req_dict = json.loads(req_data)
104 print(type(req_dict))
105 print(req_dict)
106 # req_dict_eval = eval(req_data) # eval无法自动处理null
107 4.3、期望结果与实际结果的比对
108 import ast
109
110 # 从excel当中,读取出来的断言列表
111 check_str = '[{"expr":"$.code","expected":0,"type":"eq"},{"expr":"$.msg","expected":"OK","type":"eq"}]'
112
113 # 把字符串转换成python列表
114 check_list = ast.literal_eval(check_str) # 比eval安全一点。转成列表。
115 print(check_list)
116
117 # 响应结果
118 response = {
119 "code": 0,
120 "msg": "OK",
121 "data": {
122 "id": 1000396245,
123 "reg_name": "小柠檬",
124 "mobile_phone": "13300003488"
125 },
126 "copyright": "Copyright 柠檬班 © 2017-2020 湖南省零檬信息技术有限公司 All Rights Reserved"
127 }
128
129 # 如何从响应果当中,通过jsonpath表达式,提取出要比对的数据。
130 # 第三方库:jsonpath
131 import jsonpath
132
133 # 比对结果列表
134 check_res = []
135
136 for check in check_list:
137 # 通过jsonpath表达式,从响应结果当中拿到了实际结果
138 actual = jsonpath.jsonpath(response, check["expr"])
139 if isinstance(actual,list):
140 actual = actual[0]
141 # 与实际结果做比对
142 if check["type"] == "eq":
143 check_res.append(actual == check["expected"])
144
145 if False in check_res:
146 AssertionError
147 4.4、Faker的使用
148 """
149 faker造数据:
150 faker的官方文档:https://faker.readthedocs.io/en/master/locales.html
151 测试派文章:http://testingpai.com/article/1615615023407
152
153 1、安装:
154 pip install faker
155
156 2、使用:
157 1) 导入:from faker import Faker
158 2) 语言支持:
159 简体中文:zh_CN
160 繁体中文:zh_TW
161 美国英文:en_US
162 英国英文:en_GB
163 德文:de_DE
164 日文:ja_JP
165 韩文:ko_KR
166 法文:fr_FR
167 比如中文:f = Faker("zh_CN")
168
169 3) faker提供的数据生成类型:
170 faker.Provider
171
172 示例:
173 f = Faker("zh_CN")
174 print(f.phone_number())
175
176 支持自己定义数据生成规则 :
177 Faker 已经提供了足够丰富的信息生成,包括名字、手机号、邮箱地址、邮编等等。尽管如此,可能还是没有办法满足你的需求。
178
179 这时,可以利用自定义扩展,引用外部的 provider,自定义你要的功能。
180
181 Faker 对象可以通过 add_provider 方法将自定义的 Provider 添加到对象中,自定义的 Provider 需要继承自 BaseProvider。
182
183
184
185 """
186 from faker import Faker
187
188 f = Faker("zh_CN")
189 print(f.phone_number())
190 4.5、断言的封装
191 import ast
192 import jsonpath
193
194 from py_37.Py_Api接口自动化.class_api06.common.mylogger import logger
195
196
197 class MyAssert:
198
199 def assert_response_value(self,check_str, response_dict):
200 """
201 :param check_str: 从excel当中,读取出来的断言列。是一个列表形式的字符串。里面的成员是一个断言
202 :param response_dict: 接口请求之后的响应数据,是字典类型。
203 :return: None
204 """
205 # 所有断言的比对结果列表
206 check_res = []
207
208 # 把字符串转换成python列表
209 check_list = ast.literal_eval(check_str) # 比eval安全一点。转成列表。
210
211 for check in check_list:
212 logger.info("要断言的内容为:\n{}".format(check))
213 # 通过jsonpath表达式,从响应结果当中拿到了实际结果
214 actual = jsonpath.jsonpath(response_dict, check["expr"])
215 if isinstance(actual, list):
216 actual = actual[0]
217 logger.info("从响应结果当中,提取到的值为:\n{}".format(actual))
218 logger.info("期望结果为:\n{}".format(check["expected"]))
219 # 与实际结果做比对
220 if check["type"] == "eq":
221 logger.info("比对2个值是否相等。")
222 logger.info("比对结果为:\n{}".format(actual == check["expected"]))
223 check_res.append(actual == check["expected"])
224
225 if False in check_res:
226 logger.error("部分断言失败!,请查看比对结果为False的")
227 raise AssertionError
228 else:
229 logger.info("所有断言成功!")