同事有一份 EXCEL 表格,目前有 1064 行的值,需要把这 1064 行的值,分别进行接口测试,返回值里面的 TotalRecords,如果数字是大于 100 的,就把表格中对应的这个值提取出来。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@File : LockCheckData.py
@Time : 2020/06/29
"""
import json
import allure
import requests
import pandas as pd
def GetExcelValue(filepath, sheet_name):
data = pd.read_excel(filepath, sheet_name=sheet_name, keep_default_na=False)
return data
@allure.step("获取personId值: 第{0}行, 第{1}列")
def GetPersonId(x, y):
data = GetExcelValue("D:/QCC_MyProjects/DataSource/logo_check.xlsx", "logo_check")
personId = data.iloc[x, y]
return personId
@allure.step("接口请求开始")
def check_result(x, y):
baseurl = "http://x.x.x.x:xxxx/api/person/getBossDJGData"
# 添加URL信息
data = {"baseurl": baseurl}
baseurlValue = json.dumps(data, ensure_ascii=False)
allure.attach(baseurlValue, "URL地址", allure.attachment_type.JSON)
# 传值列表
params = {"personId": GetPersonId(x, y)}
res = requests.get(url=baseurl, params=params)
# JSON文件转为字典格式
resp_data = json.loads(res.text)
return resp_data
def checkdata():
"""
获取值
:return:
"""
for i in range(0, 1063): # 从0循环到1062,excel中是从第1行到第1064行
data = check_result(i, 0) # 发送行列对应的KEY值进行接口请求
print(data)
new_data_value = json.dumps(data, ensure_ascii=False)
message_value = json.loads(new_data_value, strict=False)["Result"]["Relative"]["Paging"]["TotalRecords"] # 读取接口返回值中的TotalRecords值
if message_value >= 100:
with open("morethan100.txt", mode="a+") as fp:
newdata = GetPersonId(i, 0) # 传值
fp.write(newdata+"\n") # 追加并写入文本中
fp.flush() # 设置缓冲区
elif message_value < 100:
with open("lessthan100.txt", mode="a+") as fp:
newdata = GetPersonId(i, 0) # 传值
fp.write(newdata+"\n") # 追加并写入文本中
fp.flush() # 设置缓冲区
if __name__ == '__main__':
checkdata()