工具类
# coding:utf-8 import xlrd #execel 表格 格式 class ExcelUtil(): def __init__(self, excelPath, sheetName): self.data = xlrd.open_workbook(excelPath) self.table = self.data.sheet_by_name(sheetName) # 获取第一行作为key值 self.keys = self.table.row_values(0) # 获取某表的总行数 self.rowNum = self.table.nrows # 获取某表的总列数 self.colNum = self.table.ncols def return_dict_data(self): if self.rowNum <= 1: print("总行数小于1") else: r = [] j=1 for i in range(self.rowNum-1): s = {} # 从第二行取对应values值 values = self.table.row_values(j) for x in range(self.colNum): s[self.keys[x]] = values[x] r.append(s) j+=1 return r import yaml #yaml 格式 #带测试用例编号key一起读取! def yaml_data_with_file(file_name, key): with open(file_name, "r") as f: data = yaml.load(f,Loader=yaml.FullLoader)[key] ret = list() for k in data.keys(): v=data[k]; v["key"]=k; ret.append(v); return ret #filePath形参:String,表示txt或csv文件的路径!例如:"C:/A/1.csv" "C:/A/1.csv" def readTxtOrCsv(filePath): temp=[] fp=open(filePath,mode="r",encoding="utf-8") for line in fp: line=line.strip() temp.append(line) fp.close() headers=temp[0].split(",") values=[] for i in range(len(temp)): if i>=1: values.append(temp[i]) ret = [] for x in values: data={} temp1=x.split(",") for y in range(len(headers)): k=headers[y] v=temp1[y] data[k]=v ret.append(data) return ret #引入加密hashlib import hashlib #函数原型:String jiami(String data) #形参data:未加密的字符串 #函数功能:对字符串data进行加密! def Md5(data): pwd_jiami = hashlib.md5(bytes(data, encoding='utf-8')).hexdigest() return pwd_jiami
1.#案例:测试数据存储在excel表格中,读取这些测试数据,并封装成pytest测试框架需要的格式! #引入工具 from utils.readUtil import ExcelUtil test_data1=ExcelUtil("../data/data.xlsx","Sheet1").return_dict_data() print(test_data1)
2.#案例:测试数据存储在yaml文件,读取这些测试数据,并封装成pytest框架需要的格式! #引入功能 from utils.readUtil import yaml_data_with_file test_data2=yaml_data_with_file("../data/data.yaml","test_login_001") print(test_data2)
3.#案例:测试数据存储在txt或csv文件中,读取这些测试数据,并封装成pytest框架需要的格式! #引入工具 from utils.readUtil import readTxtOrCsv #使用 # test_txt=readTxtOrCsv("../data/data_test.txt") # print(test_txt) test_csv=readTxtOrCsv("../data/data.csv") print(test_csv)
4.#技术:发送加密接口! #案例:post http://abc.com/user/login #请求参数(form):username=zansan&pwd=sadsadsadsa import requests from utils.readUtil import Md5 fp=open("../data/data1.txt","r") for line in fp: line=line.rstrip() a=line.split(",") zanhao=a[0] pwd=Md5(a[1]) #发请求 d=\ { "username":zanhao, "pwd":pwd } response=requests.post(url="http://abc.com/user/login",data=d) fp.close()
xlrd
库:
1.0.0
PyYAML
库:
5.3
记得安库