我只不过是一只边学边写笔记的测试同学~ 如有错误之处,还请大佬们指正。 大家共同学习,共同进步吧~
首先,本文excel中case涉及的字段以及顺序为:
- 创建Base包;封装base_request请求方法
import requests
class BaseRequests:
def send_post(self, url, data, header=None):
res = requests.post(url=url, data=data, headers=header)
return res
def send_get(self, url, data, header=None):
res = requests.post(url=url, params=data, headers=header)
return res
def run_main(self, method, url, data, header=None):
if method =='get':
res = self.send_get(url, data, header)
else:
res = self.send_post(url, data, header)
return res
base_request = BaseRequests()
- 创建Util包;封装获取excel中数据的各种方法;(前提需要有一个Case包用来存放excel的用例,本文使用的是testcase.xlsx)
import openpyxl
import os
base_path = os.getcwd()
path = os.path.abspath(os.path.dirname(os.getcwd()))
"""
1.加载excel信息 ——load_excel
2.加载excel中所有sheet信息 ——get_sheet_data
3.获取行数 ——get_rows
4.获取某一行的信息 ——get_rows_value
5.获取某一列的数据 ——get_columns_value
6.获取行号 ——get_rows_number
7.获取某一个单元格的内容 ——get_cell_value
8.获取excel里面的所有的数据(也就是最终的用例) ——get_excel_data
9.写入数据
"""
class GetExcel:
def load_excel(self):
excel = openpyxl.load_workbook(path+'/Case/testcase.xlsx')
return excel
def get_sheet_data(self, index=None):
sheet_name = self.load_excel().sheetnames
if index == None:
index = 0
data = self.load_excel()[sheet_name[index]]
return data
def get_rows(self):
data = self.get_sheet_data().max_row
return data
def get_rows_value(self, row):
row_list = []
for i in self.get_sheet_data()[row]:
row_list.append(i.value)
return row_list
def get_columns_value(self, key=None):
column_list = []
if key == None:
key = 'A'
column_list_data =self.get_sheet_data()[key]
for i in column_list_data:
column_list.append(i.value)
return column_list
def get_row_number(self, case_id):
num = 1
cols_data =self.get_columns_value()
for col_data in cols_data:
if case_id == col_data:
return num
num +=1
return num
def get_cell_value(self, rows, cols):
data = self.get_sheet_data().cell(row=rows, column=cols).value
return data
def get_excel_data(self):
data_list = []
for i in range(self.get_rows()):
data_list.append(self.get_rows_value(i+2))
return data_list
def write_excel_data(self, rows, cols, value):
wb = self.load_excel()
wr = wb.active
wr.cell(rows, cols, value)
wb.save(path + '/Case/testcase.xlse')
excel_data = GetExcel()
3.创建Run包;主要是run_main主程序运行
import os
base_path = os.getcwd()
path = os.path.abspath(os.path.dirname(os.getcwd()))
from Util.get_excel import excel_data
class RunMain:
def run_main(self):
rows = excel_data.get_rows()
for i in range(rows):
data = excel_data.get_rows_value(i+2)
print(data)
4.使用ini文件做公共配置;创建Config包;创建server.ini文件
[server]
host=https://xx.xxxx.net
phone=xxxx
pwd=xxxxxx
is_run=3
5.此时需要读取server.ini文件中配置的信息;于是在Util文件中,封装get_server.py
import configparser
import os
base_path = os.getcwd()
path = os.path.abspath(os.path.dirname(os.getcwd()))
class GetServer:
def load_ini(self):
file_path = path +'/Config/server.ini'
cf = configparser.ConfigParser()
cf.read(file_path, encoding='utf-8-sig')
return cf
def get_value(self, key, section=None):
if section == None:
section = 'server'
cf = self.load_ini()
try:
data = cf.get(section, key)
except Exception:
print("没有获取到值")
data = None
return data
server_data = GetServer()
6.此时;在Base(base_request)中;将通过get_server.py获取到的host与url进行拼接;此时base_request.py整体如下:
import requests
from Util.get_server import server_data
class BaseRequests:
def send_post(self, url, data, header=None):
res = requests.post(url=url, data=data, headers=header)
return res
def send_get(self, url, data, header=None):
res = requests.post(url=url, params=data, headers=header)
return res
def run_main(self, method, url, data, header=None):
base_url = server_data.get_value('host')
if 'http' not in url:
url = base_url + url
if method =='get':
res = self.send_get(url, data, header)
else:
res = self.send_post(url, data, header)
return res
base_request = BaseRequests()
7.进一步完善Run(run_main.py);引入excel中的数据;具体如下:
import os
from Util.get_header import get_header
base_path = os.getcwd()
path = os.path.abspath(os.path.dirname(os.getcwd()))
from Util.get_excel import excel_data
from Base.base_request import base_request
class RunMain:
def run_main(self):
rows = excel_data.get_rows()
for i in range(rows):
header = None
data = excel_data.get_rows_value(i+2)
is_run = data[2]
if is_run == 'yes':
method = data[6]
url = data[5]
data1 = data[7]
is_header = data[9]
if is_header == 'yes':
header = get_header()
res = base_request.run_main(method, url, data1, header).json()
print(res)
第一部分完结;待更新第二部分…… 此时,目录结构为: Test为项目名称 Base(base_request.py) Case(testcase.xlsx) Config(server.ini) Run(runmain.py) Util(get_excel.py;get_server.py)
|