python?requests實現(xiàn)上傳excel數(shù)據(jù)流
requests上傳excel數(shù)據(jù)流
headers=self.headers
#獲取導(dǎo)入模版
file_home = self.import_template
log.info(file_home)
wb = load_workbook(filename=file_home)
ws = wb['sheet1']
# 修改產(chǎn)廢單位名稱,以及備注
ws['b3'] = 'itest產(chǎn)廢單位'+self.dic["t"]
ws['s3'] = 'i原料銷售'+self.dic["t"]
wb.save(file_home)
url=self.url_1+"/companies/import?companyType=2"
payload={}
m=MultipartEncoder(
fields={
"parent_dir":'/',
"name":'file',
"filename":'name.xlsx',
'file':('name.xlsx',open(file_home,'rb'),'application/vnd.ms-excel')
}
)
headers['Content-Type']=m.content_type
r=requests.post(url,headers=headers,data=m)
log.info(r.json())數(shù)據(jù)驅(qū)動之python+requests+excel
數(shù)據(jù)驅(qū)動
是根據(jù)數(shù)據(jù)來測試的,如讀取 excel表中的測試用例自動填寫測試結(jié)果,發(fā)送測試報告包括以下模塊:
- 1.獲取用例
- 2.調(diào)用接口
- 3.校驗結(jié)果
- 4.發(fā)送測試報告
- 5.異常處理
- 6.日志模塊
1. 首先設(shè)計好測試用例

2.建立文件結(jié)構(gòu)
該自動化測試框架命名為:ATP,bin目錄下寫主程序,cases目錄下放測試用例,conf目錄下放配置文件,lib目錄下放各個封裝好的模塊,logs目錄下放日志文件,和readme文件。

3.封裝模塊
common.py:封裝讀取excel用例、調(diào)用接口、檢驗結(jié)果、寫入報告這幾個模塊。
"""
第一步:讀取excel中用例
第二步:根據(jù)用例發(fā)送請求
第三步:校驗結(jié)果
第四步:將測試結(jié)果、返回報文寫入excel
"""
import xlrd,requests
from xlutils import copy
from lib.log import atp_log
class OpCase(object):
def get_case(self,file_path):
cases= [] #定義一個列表存放所有的cases
if file_path.endswith('.xls') or file_path.endswith('.xlsx'):
try:
book = xlrd.open_workbook(file_path)
sheet = book.sheet_by_index(0)
for i in range(1,sheet.nrows):
row_data = sheet.row_values(i) #獲取的每一行數(shù)據(jù)存到列表row_data
cases.append(row_data[4:8])
atp_log.info('共讀取%s條用例'%(len(cases)))
self.file_path = file_path #因為該函數(shù)已經(jīng)傳了參數(shù)路徑,為方便write_excel引用,在此實例化
except Exception as e:
atp_log.error('[%s]用例獲取失敗,錯誤信息:%s'%(file_path,e))
else:
atp_log.error('用例文件不合法,%s'%file_path)
return cases
def my_request(self,url,method,data):
data = self.dataToDict(data)
try:
if method.upper() == 'POST':
res = requests.post(url,data).text
elif method.uper() == 'GET':
res = requests.get(url,params=data).text
else:
atp_log.warning('該請求方式暫不支持')
res = '該請求方式暫不支持'
except Exception as e:
msg = '【%s】接口調(diào)用失敗,%s'%(url,e)
atp_log.error(msg)
res = msg
return res
def dataToDict(self,data): #把數(shù)據(jù)轉(zhuǎn)成字典。
res = {}
data = data.split(',')
for d in data: #
k, v = d.split('=')
res[k] = v
def check_res(self,res,check): #res:實際結(jié)果,check:預(yù)期結(jié)果
res = res.replace('": "','=').replace('": ','=')
for c in check.split(','):
if c not in res:
atp_log.info('結(jié)果校驗失敗,預(yù)期結(jié)果:【%s】,實際結(jié)果【%s】'%(c,res))
return '失敗'
return '成功'
def write_excel(self,case_res):
book = xlrd.open_workbook(self.file_path)
new_book = copy.copy(book)
sheet = new_book.get_sheet(0)
row = 1
for case_case in case_res:
sheet.write(row,8,case_case[0])
sheet.write(row,9,case_case[1])
row += 1
new_book.save(self.file_path.replace('xlsx','xls'))log.py:封裝日志模塊
import logging,os
from logging import handlers
from conf import setting
class Mylogger():
def __init__(self,file_name,level='info',backCount=5,when='D'):
logger = logging.getLogger() # 先實例化一個logger對象,先創(chuàng)建一個辦公室
logger.setLevel(self.get_level(level)) # 設(shè)置日志的級別
# f1 = logging.FileHandler(filename='a.log',mode='a',encoding='utf-8') #找到寫日志文件的這個人
c1 = logging.StreamHandler() # 負責(zé)往控制臺輸出的
b1 = handlers.TimedRotatingFileHandler(filename=file_name, when=when, interval=1, backupCount=backCount, encoding='utf-8')
fmt = logging.Formatter('%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s')
c1.setFormatter(fmt)
b1.setFormatter(fmt)
logger.addHandler(c1)
logger.addHandler(b1)
self.logger = logger
def get_level(self,str):
level = {
'debug':logging.DEBUG,
'info':logging.INFO,
'warm':logging.WARNING,
'error':logging.ERROR
}
str = str.lower()
return level.get(str)
path = os.path.join(setting.LOG_PATH,setting.LOG_NAME)
atp_log = Mylogger(path,'debug').logger
#直接在這里實例化,用的時候不用再實例化了
#別的地方用的時候,直接atp_log.warnning('xxxx')send_mail.py:封裝發(fā)送郵件模塊
import yagmail
from conf import setting
from lib.log import atp_log
def sendmail(title,content,attrs=None):
m = yagmail.SMTP(host=setting.MAIL_HOST,user=setting.MAIL_USER,
password=setting.MAIL_PASSWRD,smtp_ssl=True)
m.send(to=setting.TO,
subject=title,
contents = content,
attachments = attrs)
atp_log.info('發(fā)送郵件完成')4.配置文件
setting.py,配置文件:設(shè)置郵件地址、日志默認級別、用例存放路徑、日志存放路徑、日志文件名
import os
BASE_PATH = os.path.dirname(
os.path.dirname(os.path.abspath(__file__))
) #三層目錄定位到ATP目錄
MAIL_HOST = 'smtp.qq.com'
MAIL_USER='12*****89@qq.com'
MAIL_PASSWRD = 'gjn*****bcgh'
TO = [
'12*****9@qq.com'
]
LEVEL = 'debug' #設(shè)置日志默認級別
LOG_PATH = os.path.join(BASE_PATH,'logs') #日志文件在logs目錄下
CASE_PATH = os.path.join(BASE_PATH,'cases') #用例文件在cases目錄下
LOG_NAME = 'atp_log' #設(shè)置日志文件名5.將ATP文件
Mark directory as Sources Root
6.編寫主程序
start.py
import os,sys
BASE_PATH = os.path.dirname(
os.path.dirname(os.path.abspath(__file__))
)
sys.path.insert(0,BASE_PATH)
from lib.common import OpCase
from lib.send_mail import sendmail
from conf import setting
class CaseRun(object):
def find_case(self):
op = OpCase()
for f in os.listdir(setting.CASE_PATH): #每次循環(huán)的時候讀一個excel
abs_path = os.path.join(setting.CASE_PATH,f)
case_list = op.get_case(abs_path)
res_list = []
pass_count,fail_count= 0,0
for case in case_list: #循環(huán)每一個excel里面的所有用例
url,method,req_data,check = case
res = op.my_request(url,method,req_data) #調(diào)用完接口返回的結(jié)果
status = op.check_res(res,check)
res_list.append([res,status])
if status == '通過':
pass_count += 1
else:
fail_count += 1
op.write_excel(res_list)
msg = '''
xx你好,
本次共運行%s條用例,通過%s條,失敗%s條。
'''%(len(res_list),pass_count,fail_count)
sendmail('測試用例運行結(jié)果',content=msg,attrs=abs_path)
CaseRun().find_case()OK,數(shù)據(jù)驅(qū)動自動化測試框架編寫完成,運行 start.py 程序,收到郵件內(nèi)容如下:

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python plt.imshow函數(shù)及其參數(shù)使用
plt.imshow()是Matplotlib庫中的一個函數(shù),主要用于顯示圖像或矩陣數(shù)據(jù),本文主要介紹了Python plt.imshow函數(shù)及其參數(shù)使用,具有一定的參考價值,感興趣的可以了解一下2024-02-02
神經(jīng)網(wǎng)絡(luò)相關(guān)之基礎(chǔ)概念的講解
今天小編就為大家分享一篇關(guān)于神經(jīng)網(wǎng)絡(luò)相關(guān)之基礎(chǔ)概念的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
pandas 如何保存數(shù)據(jù)到excel,csv
這篇文章主要介紹了pandas 如何保存數(shù)據(jù)到excel,csv的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
python分析實現(xiàn)微信釘釘?shù)溶浖嚅_分身
我發(fā)現(xiàn)壇友分享的很多都是通過cmd?去start?多個微信,雖然能實現(xiàn)多開,但不夠靈活,比如我上午登錄了一個微信,下午在登錄就不太好用了,當然也可能是我start的姿勢不對。于是我就搜了下單實例原理,自己動手實現(xiàn)了個隨用隨開的2022-02-02

