目录标题
前言:创作不易,大佬求打赏
一、接口读取excel的读取、调用、问题
1、以表名,关键字读取excel方法
def get_excelData(sheetName,testName): # sheetName是页名,testName是关键字
excel="./data/系统接口测试用例.xls"
workBook = xlrd.open_workbook(excel,formatting_info=True)
workSheet = workBook.sheet_by_name(sheetName)
# print(workSheet.col_values(0))
retnList=[]
index=0
for one in workSheet.col_values(0):
if testName in one:
retnNine=workSheet.cell_value(index, 9)
retnEleven=workSheet.cell_value(index, 11)
# print(type(retnNiht))
# 将string格式的retnNine和retnEleven 转换为字典格式,已元祖的形式放入列表中
retnList.append((json.loads(retnNine),json.loads(retnEleven)))
# 转换为字典格式
index+=1
return retnList
2、装饰器调用excel方法
class TestLogin: # [({indata,respData}),({indata,respData})]
#[ ( { 字典1,字典2 } ),( { 字典1,字典2 } ) ]
# [({下图中的请求参数,预期结果}),({下图中的请求参数,预期结果})]
@pytest.mark.parametrize('indata,respData', get_excelData('登录模块', 'Login'))
def test_login(self, indata, respData):
url = "http://100.100.100.100:8082/account/login"
resp = requests.post(url, params=indata).json() #返回json格式
if resp["code"] == respData['code']:
print("-----------")
if __name__ == '__main__':
pytest.main(["test_abcd.py", "-s"])
- 具体调用关系
3、请求参数的data中存在列表[ ]字段
例:
id=2870
idMenu=4803
idShop=5336
specsJson=[{"specs":"默认","packing_fee":"0","price":"20"}]
specsJson是[ ],在excel中则像下面那么写即可
将 [ ] 用 " " 框起来,里面用 ‘ ’ 框起来字段
“specsJson”:"[{‘specs’:‘默认’,‘packing_fee’:‘0’,‘price’:‘20’}]"
- 例子的:excel的写法
{“name”:“葱香千层饼”,“id”:“2870”,“idShop”:“5336”,“category_id”:“4803”,“specsJson”:"[{‘specs’:‘默认’,‘packing_fee’:‘0’,‘price’:‘20’}]"}
二、yaml的使用
1、基本语法
安装yaml
pip install pyyaml
①、读取
def host():
file = open("../zTest0404/test.yaml", encoding="UTF-8")
# yaml.读取(哪个文件,处理警告)
result = yaml.load(file, Loader=yaml.FullLoader)
# print(result["host"])
return result
if __name__ == '__main__':
print(host())
②、yaml读取列表list
- name
- age
- data
③、list包含字典
-
name: xiaoming
age: 18
-
data: 1000
④、字典里包含列表
hongxing:
- 10
- 20
- 30
mingzhu:
- 10
- 20
- 30
⑤、一个yaml读取多个数据
hongxing:
- 10
- 20
- 30
---
mingzhu:
- 10
- 20
- 30
def host2():
file = open("../zTest0404/test.yaml", encoding="UTF-8")
result = yaml.load_all(file, Loader=yaml.FullLoader) #处理警告
# print(result["host"])
for one in result:
print(one)
if __name__ == '__main__':
print(host2())
读取结果:
{'hongxing': [10, 20, 30]}
{'mingzhu': [10, 20, 30]}
2、yaml格式测试用例的调用
yaml文件示例:
-
url: /www/file #url地址
method: POST #方法:get、post
detail: 账号密码正确 #详情
headers: #请求头
data:
username: wgy001
password: 123456 #请求体
resp: #预期结果
code: 20000
msg: 成功
-
url: /www/file #url地址
method: POST #方法:get、post
detail: 账号正确,密码错误 #详情
headers: #请求头
data:
username: wgy001
password: qwertt #请求体
resp: #预期结果
code: 9999
msg: 输入的密码错误
代码:
def host():
file = open("../zTest0404/test.yaml",encoding="UTF-8")
result = yaml.load(file, Loader=yaml.FullLoader) #处理警告
resList=[]
for one in result:
resList.append((one['data'],one['resp']))
return resList
if __name__ == '__main__':
print(host())
返回值为:
[ ( { 字典1,字典2 } ),( { 字典1,字典2 } ) ]
[({'username': 'wgy001', 'password': 123456}, {'code': 20000, 'msg': '成功'}), ({'username': 'wgy001', 'password': 'qwertt'}, {'code': 9999, 'msg': '输入的密码错误'})]
也就是调用时使用的格式:
class TestLogin: # [{(indata,respData),(indata,respData)}]
# [