前言:前文實現(xiàn)了接口的自動化的基本功能,從讀取數(shù)據(jù)到斷言,但是因業(yè)務(wù)需求,考慮到每次注冊賬號都需要使用新的手機號,所以本文實現(xiàn)替換手機號的功能。
1.創(chuàng)建表格api_phone填寫參數(shù)key、value

image.png
2.代碼
from openpyxl import load_workbook
import xlrd
class DoExcel:
def __init__(self,excelpath):
self.wb = load_workbook(excelpath)
self.excelpath=excelpath
self.sheet_apitest = self.wb.get_sheet_by_name('api_test')
self.sheet_phone = self.wb.get_sheet_by_name('api_phone')
#讀取phone表格內(nèi)容
def Excel_phone(self):
init_datas={}
for index in range(2,self.sheet_phone.max_row+1):
key=self.sheet_phone.cell(row=index,column=1).value
init_datas[key]=self.sheet_phone.cell(row=index,column=2).value
# init_datas['${phone_b}']=str(int(init_datas['${phone_b}'])+1)
return init_datas
#修改phone表格手機號
def modify_phone(self):
res=int(self.sheet_phone.cell(row=2,column=2).value)
print(res)
self.sheet_phone.cell(row=2,column=2).value=str(res+1)
print(self.sheet_phone.cell(row=2,column=2).value)
#保存phone表格
def save_excelFile(self,):
self.wb.save(self.excelpath)
#讀取api_test表格
def Excel_api_test(self):
all_case_datas=[]
for i in range(2,self.sheet_apitest.max_row+1):
case_data={}
case_data["url"]=self.sheet_apitest.cell(row=i,column=1).value
request_data=self.sheet_apitest.cell(row=i,column=2).value
init_datas=self.Excel_phone()
#替換request_data 中的手機號
if request_data is not None and len(init_datas)>0:
for key , value in init_datas.items():
if request_data.find(key)!=-1:
request_data = request_data.replace(str(key),str(value))
case_data["request_data"]=request_data
case_data["method"]=self.sheet_apitest.cell(row=i,column=3).value
case_data["expected_data"]=self.sheet_apitest.cell(row=i,column=4).value
if self.sheet_apitest.cell(row=i,column=5).value is not None:
case_data['related_exp']=self.sheet_apitest.cell(row=i,column=5).value
all_case_datas.append(case_data)
return all_case_datas
a=DoExcel('/Users/xiaolongxia/PycharmProjects/api_test/text.xlsx')
print(a.Excel_api_test())
print(a.Excel_phone())
a.modify_phone()
a.save_excelFile()
3.運行結(jié)果

image.png

image.png
- 備注:使用 sheet=wb.get_sheet_by_name('frequency')方法會有錯誤警告所以將該方法修改為 sheet=wb["frequency"]