读excel文件,借助openpyxl工具
import os
import requests
os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"
base_url = "https://testnet.starscan.io/explorer-api"
import os
import openpyxl
file_dir = os.path.abspath(os.path.dirname(__file__))
class GetDatas:
def getExcels(self,sheet):
data_list = []
wk = openpyxl.load_workbook(f"{file_dir}\exports.xlsx")
sheet1 = wk[sheet]
for i in range(2, sheet1.max_row + 1):
dict_data = {}
for j in range(1, sheet1.max_column + 1):
key = sheet1.cell(1, j).value
value = sheet1.cell(row=i, column=j).value
dict_data[key] = value
data_list.append(dict_data)
return data_list
if __name__ == '__main__':
datas = GetDatas().getExcels("Sheet1")
print(datas[0]["url"])
print(datas[0]["headers"])
import json
import unittest
import requests
from ddt import ddt, data
from read_excel.common import base_url
from read_excel.get_excel_datas import GetDatas
@ddt
class TestGetData(unittest.TestCase):
def setUp(self) -> None:
self.url = base_url
@data(*GetDatas().getExcels("Sheet1"))
def test_url_datas(self, dataList):
print(dataList)
headers = json.loads(dataList["headers"])
datas = json.loads(dataList["datas"])
response = requests.request(method=dataList["method"], url=self.url+dataList["url"], headers=headers, json=datas)
assert response.status_code == dataList["status_code"]
res = response.json()
print(res)
assert res["msg"] == dataList["context_actual"]
if __name__ == '__main__':
unittest.main()