亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python利用Excel讀取和存儲(chǔ)測(cè)試數(shù)據(jù)完成接口自動(dòng)化教程

瀏覽:5日期:2022-07-26 18:11:59

http_request2.py用于發(fā)起http請(qǐng)求

#讀取多條測(cè)試用例#1、導(dǎo)入requests模塊import requests#從 class_12_19.do_excel1導(dǎo)入read_data函數(shù)from do_excel2 import read_datafrom do_excel2 import write_datafrom do_excel2 import count_case#定義http請(qǐng)求函數(shù)COOKIE=Nonedef http_request2(method,url,data): if method==’get’: print(’發(fā)起一個(gè)get請(qǐng)求’) result=requests.get(url,data,cookies=COOKIE) else: print(’發(fā)起一個(gè)post請(qǐng)求’) result=requests.post(url,data,cookies=COOKIE) return result #返回響應(yīng)體 # return result.json() #返回響應(yīng)結(jié)果:結(jié)果是字典類型:{’status’: 1, ’code’: ’10001’, ’data’: None, ’msg’: ’登錄成功’}#從Excel讀取到多條測(cè)試數(shù)據(jù)sheets=[’login’,’recharge’,’withdraw’]for sheet1 in sheets: max_row=count_case(sheet1) print(max_row) for case_id in range(1,max_row): data=read_data(sheet1,case_id) print(’讀取到第{}條測(cè)試用例:’.format(data[0])) print(’測(cè)試數(shù)據(jù) ’,data) #print(type(data[2])) #調(diào)用函數(shù)發(fā)起http請(qǐng)求 result=http_request2(data[4],data[2],eval(data[3])) print(’響應(yīng)結(jié)果為 ’,result.json()) if result.cookies: COOKIE=result.cookies #將測(cè)試實(shí)際結(jié)果寫入excel #write_data(case_id+1,6,result[’code’]) write_data(sheet1,case_id+1,7,str(result.json())) #對(duì)比測(cè)試結(jié)果和期望結(jié)果 if result.json()[’code’]==str(data[5]): print(’測(cè)試通過(guò)’) #將用例執(zhí)行結(jié)果寫入Excel write_data(sheet1,case_id+1,8,’Pass’) else: write_data(sheet1,case_id+1,8,’Fail’) print(’測(cè)試失敗’)

do_excel2.py完成對(duì)excel中用例的讀、寫、統(tǒng)計(jì)

# 導(dǎo)入load_workbookfrom openpyxl import load_workbook#讀取測(cè)試數(shù)據(jù)#將excel中每一條測(cè)試用例讀取到一個(gè)列表中#讀取一條測(cè)試用例——寫到一個(gè)函數(shù)中def read_data(sheet_name,case_id): # 打開excel workbook1=load_workbook(’test_case2.xlsx’) # 定位表單(test_data) sheet1=workbook1[sheet_name] print(sheet1) test_case=[] #用來(lái)存儲(chǔ)每一行數(shù)據(jù),也就是一條測(cè)試用例 test_case.append(sheet1.cell(case_id+1,1).value) test_case.append(sheet1.cell(case_id+1,2).value) test_case.append(sheet1.cell(case_id+1,3).value) test_case.append(sheet1.cell(case_id+1,4).value) test_case.append(sheet1.cell(case_id+1,5).value) test_case.append(sheet1.cell(case_id+1,6).value) return test_case #將讀取到的用例返回#調(diào)用函數(shù)讀取第1條測(cè)試用例,并將返回結(jié)果保存在data中# data=read_data(1)# print(data)#將測(cè)試結(jié)果寫會(huì)exceldef write_data(sheet_name,row,col,value): workbook1=load_workbook(’test_case2.xlsx’) sheet=workbook1[sheet_name] sheet.cell(row,col).value=value workbook1.save(’test_case2.xlsx’)#統(tǒng)計(jì)測(cè)試用例的行數(shù)def count_case(sheet_name): workbook1=load_workbook(’test_case2.xlsx’) sheet=workbook1[sheet_name] max_row=sheet.max_row #統(tǒng)計(jì)測(cè)試用例的行數(shù) return max_row

test_case2.xlsx存儲(chǔ)測(cè)試用例

python利用Excel讀取和存儲(chǔ)測(cè)試數(shù)據(jù)完成接口自動(dòng)化教程

python利用Excel讀取和存儲(chǔ)測(cè)試數(shù)據(jù)完成接口自動(dòng)化教程

補(bǔ)充知識(shí):python用unittest+HTMLTestRunner+csv的框架測(cè)試并生成測(cè)試報(bào)告

直接貼代碼:

import csv # 導(dǎo)入scv庫(kù),可以讀取csv文件from selenium import webdriverimport unittestfrom time import sleepimport timeimport osimport HTMLTestRunnerimport codecsimport sysdr = webdriver.Chrome()class testLo(unittest.TestCase): def setUp(self): pass def test_login(self): ’’’登陸測(cè)試’’’ path = ’F:Python_test’ # 要讀取的scv文件路徑 my_file = ’F:pythonprojectinterfaceTesttestFiless.csv’ # csv.reader()讀取csv文件, # Python3.X用open,Python2.X用file,’r’為讀取 # open(file,’r’)中’r’為讀取權(quán)限,w為寫入,還有rb,wd等涉及到編碼的讀寫屬性 #data = csv.reader(codecs.open(my_file, ’r’, encoding=’UTF-8’,errors= ’ignore’)) with codecs.open(my_file, ’r’, encoding=’UTF-8’,errors= ’ignore’) as f: data=csv.reader((line.replace(’x00’,’’) for line in f)) # for循環(huán)將讀取到的csv文件的內(nèi)容一行行循環(huán),這里定義了user變量(可自定義) # user[0]表示csv文件的第一列,user[1]表示第二列,user[N]表示第N列 # for循環(huán)有個(gè)缺點(diǎn),就是一旦遇到錯(cuò)誤,循環(huán)就停止,所以用try,except保證循環(huán)執(zhí)行完 print(my_file) for user in data: print(user) dr.get(’https://passport.cnblogs.com/user/signin’) # dr.find_element_by_id(’input1’).clear() dr.find_element_by_id(’input1’).send_keys(user[0]) # dr.find_element_by_id(’input2’).clear() dr.find_element_by_id(’input2’).send_keys(user[1]) dr.find_element_by_id(’signin’).click() sleep(1) print(’n’ + ’測(cè)試項(xiàng):’ + user[2]) dr.get_screenshot_as_file(path + user[3] + '.jpg') try: assert dr.find_element_by_id(user[4]).text try: error_message = dr.find_element_by_id(user[4]).text self.assertEqual(error_message, user[5]) print(’提示信息正確!預(yù)期值與實(shí)際值一致:’) print(’預(yù)期值:’ + user[5]) print(’實(shí)際值:’ + error_message) except: print(’提示信息錯(cuò)誤!預(yù)期值與實(shí)際值不符:’) print(’預(yù)期值:’ + user[5]) print(’實(shí)際值:’ + error_message) except: print(’提示信息類型錯(cuò)誤,請(qǐng)確認(rèn)元素名稱是否正確!’) def tearDown(self): dr.refresh() # 關(guān)閉瀏覽器 dr.quit()if __name__ == '__main__': # 定義腳本標(biāo)題,加u為了防止中文亂碼 report_title = u’登陸模塊測(cè)試報(bào)告’ # 定義腳本內(nèi)容,加u為了防止中文亂碼 desc = u’登陸模塊測(cè)試報(bào)告詳情:’ # 定義date為日期,time為時(shí)間 date = time.strftime('%Y%m%d') time = time.strftime('%Y%m%d%H%M%S') # 定義path為文件路徑,目錄級(jí)別,可根據(jù)實(shí)際情況自定義修改 path = ’F:Python_test’ + date + 'login' + time + '' # 定義報(bào)告文件路徑和名字,路徑為前面定義的path,名字為report(可自定義),格式為.html report_path = path + 'report.html' # 判斷是否定義的路徑目錄存在,不能存在則創(chuàng)建 if not os.path.exists(path): os.makedirs(path) else: pass # 定義一個(gè)測(cè)試容器 testsuite = unittest.TestSuite() # 將測(cè)試用例添加到容器 testsuite.addTest(testLo('test_login')) # 將運(yùn)行結(jié)果保存到report,名字為定義的路徑和文件名,運(yùn)行腳本 report = open(report_path, ’wb’) #with open(report_path, ’wb’) as report: runner = HTMLTestRunner.HTMLTestRunner(stream=report, title=report_title, description=desc) runner.run(testsuite) # 關(guān)閉report,腳本結(jié)束 report.close()

csv文件格式:

python利用Excel讀取和存儲(chǔ)測(cè)試數(shù)據(jù)完成接口自動(dòng)化教程

備注:

使用python處理中文csv文件,并讓execl正確顯示中文(避免亂碼)設(shè)施編碼格式為:utf_8_sig,示例:

’’’’’ 將結(jié)果導(dǎo)出到result.csv中,以UTF_8 with BOM編碼(微軟產(chǎn)品能正確識(shí)別UTF_8 with BOM存儲(chǔ)的中文文件)存儲(chǔ) ’’’ #data.to_csv(’result_utf8_no_bom.csv’,encoding=’utf_8’)#導(dǎo)出的結(jié)果不能別excel正確識(shí)別 data.to_csv(’result_utf8_with_bom.csv’,encoding=’utf_8_sig’)

以上這篇python利用Excel讀取和存儲(chǔ)測(cè)試數(shù)據(jù)完成接口自動(dòng)化教程就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: python
相關(guān)文章:
主站蜘蛛池模板: 男人午夜影院 | 一区欧美 | 精品欧美一区视频在线观看 | 日韩 亚洲 中文 图片 小说 | 美国一级毛片aa | 国产一级淫片a免费播放口欧美 | 亚洲欧美在线观看视频 | 毛片三级在线观看 | 99在线精品视频免费观里 | 真人一级一级特黄高清毛片 | 黄+色+性+人免费 | 国产美女亚洲精品久久久久久 | 欧美成人精品福利在线视频 | 成年美女黄网站色大免费视频 | 色婷婷综合久久久中文字幕 | 亚洲 欧美 自拍 另类 欧美 | 婷婷性 | 亚洲国产成人超福利久久精品 | 亚洲美女精品视频 | 亚洲日韩成人 | 欧美日韩中文一区二区三区 | 亚洲综合伊人色一区 | 免费一级毛片在线播放放视频 | 国产亚洲福利精品一区二区 | 亚洲欧美另类国产综合 | 特级毛片a级毛免费播放 | 夜夜爱成人免费网站 | 欧美v在线 | 欧美亚洲专区 | 亚洲乱理伦片在线看中字 | 久久国产精品自线拍免费 | 香蕉视频在线观看黄 | 国产成人午夜片在线观看 | 黄色大片网站 | 国产成人精品午夜视频' | 美国一级做a爰片性色毛片 美国人与性xxxxxxx | 亚洲偷图色综合色就色 | 黄色一级大片视频 | 日韩一区二区三 | 天天套图 | 中国日韩欧美中文日韩欧美色 |