




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、python學習筆記(接口自動化框架 V2.0)這個是根據上次框架版本進行的優化用python獲取excel文件中測試用例數據通過requets測試接口、并使用正則表達式驗證響應信息內容生成xml文件測試報告版本更新內容:1. 整理了CreateTest.test_main()流程邏輯2. 優化了testcase.xls文件格式3. 添加了生成XML文件測試報告代碼如下: 1 #!/usr/bin/env python 2 # -*- coding: utf_8 -*- 3 # 獲取測試用例文件excel 4 5 import xlrd 6 import json 7 8 9 class Cr
2、eateExcel: 10 def _init_(self): 11 pass 12 13 classmethod 14 def open_excel(cls): 15 path = testcase.xls 16 workbook = xlrd.open_workbook(path) 17 table = workbook.sheets()0 18 return table 19 20 # 獲取sheet 21 22 classmethod 23 def get_nrows(cls, table): 24 nrows = table.nrows 25 return nrows 26 27 #
3、 獲取行號 28 29 classmethod 30 def get_id(cls, table, nrows): 31 testid = 32 for i in range(1, nrows): 33 testid.append(table.cell(i, 0).value) 34 return testid 35 36 classmethod 37 def get_name(cls, table, nrows): 38 testname = 39 for i in range(1, nrows): 40 testname.append(table.cell(i, 1).value) 41
4、return testname 42 43 # 獲取用例name 44 45 classmethod 46 def get_data(cls, table, nrows): 47 testdata = 48 for i in range(1, nrows): 49 try: 50 data = json.loads(table.cell(i, 2).value) 51 testdata.append(data) 52 except ValueError: 53 testdata.append(None) 54 return testdata 55 56 # 獲取data接口參數 57 58 c
5、lassmethod 59 def get_url(cls, table, nrows): 60 testurl = 61 for i in range(1, nrows): 62 testurl.append(table.cell(i, 3).value) 63 return testurl 64 65 # 獲取接口測試url 66 67 classmethod 68 def get_method(cls, table, nrows): 69 testmethod = 70 for i in range(1, nrows): 71 testmethod.append(table.cell(i
6、, 4).value) 72 return testmethod 73 74 # 獲取接口測試method 75 76 classmethod 77 def get_pattern(cls, table, nrows): 78 testpattern = 79 for i in range(1, nrows): 80 testpattern.append(table.cell(i, 5).value) 81 return testpattern 82 83 # 獲取接口期望響應結果 84 1 #!/usr/bin/env python 2 # -*- coding: utf_8 -*- 3 #
7、 測試核心組件 4 5 import requests 6 import re 7 from datetime import datetime 8 from createexcel import CreateExcel 9 from xml.dom import minidom 10 import sys 11 12 13 class CreateTest: 14 reload(sys) 15 sys.setdefaultencoding(utf-8) 16 17 # 避免字符串寫入文件出錯 18 19 def _init_(self): 20 pass 21 22 classmethod 2
8、3 def test_api(cls, method, url, data): 24 global results 25 try: 26 if method = post: 27 results = requests.post(url, data) 28 if method = get: 29 results = requests.get(url, data) 30 return results 31 except Exception._bases_: 32 print 服務器訪問失敗 33 34 # 接口函數 35 36 classmethod 37 def test_on(cls): 38
9、 print 用例執行開始 39 40 classmethod 41 def test_close(cls): 42 print 用例執行結束 43 44 classmethod 45 def test_result(cls, pa): 46 global report 47 try: 48 pattern = pile(pa) 49 match = pattern.search(testresults.text) 50 if match.group() = pa: 51 report = 測試通過 52 except AttributeError: 53 report = 測試失敗 54 r
10、eturn report 55 56 # 正則表達式檢測 57 58 classmethod 59 def test_http(cls, code): 60 print 請求返回狀態碼: , code 61 62 classmethod 63 def test_time(cls): 64 nowtime = datetime.today() 65 time = nowtime.strftime(%Y-%m-%d %H:%M:%S) 66 return time 67 68 # 獲取當前時間轉化字符串 69 70 classmethod 71 def test_report(cls): 72 n
11、owtime = datetime.today() 73 reportime = nowtime.strftime(%Y%m%d%H%M%S) 74 reportname = reportime + .xml 75 return reportname 76 77 # 獲取測試報告文件名稱 78 79 classmethod 80 def test_main(cls): 81 global testresults 82 table = CreateExcel.open_excel() 83 nrows = CreateExcel.get_nrows(table) 84 xml = minidom
12、.Document() 85 xml.appendChild(xml.createComment(測試報告) 86 caselist = xml.createElement(caselist) 87 xml.appendChild(caselist) 88 for i in range(0, nrows - 1): 89 testid = CreateExcel.get_id(table, nrows)i 90 testname = CreateExcel.get_name(table, nrows)i 91 testdata = CreateExcel.get_data(table, nro
13、ws)i 92 testurl = CreateExcel.get_url(table, nrows)i 93 testmethod = CreateExcel.get_method(table, nrows)i 94 testpattern = CreateExcel.get_pattern(table, nrows)i 95 96 # 執行測試 97 CreateTest.test_on() 98 testresults = CreateTest.test_api(testmethod, testurl, testdata) 99 testcode = str(testresults.st
14、atus_code)100 try:101 CreateTest.test_http(testresults.status_code)102 except AttributeError:103 pass104 CreateTest.test_close()105 # 執行結束106 # 生成xml文件107 case = xml.createElement(case)108 case.setAttribute(id, testid)109 # 輸入用例ID110 111 name = xml.createElement(name)112 name.appendChild(xml.createT
15、extNode(testname)113 # 輸入用例名稱114 method = xml.createElement(method)115 method.appendChild(xml.createTextNode(testmethod)116 # 輸入接口類型117 code = xml.createElement(code)118 code.appendChild(xml.createTextNode(testcode)119 # 輸入用例返回狀態碼120 result = xml.createElement(result)121 result.appendChild(xml.creat
16、eTextNode(CreateTest.test_result(testpattern)122 # 輸入用例測試結果123 time = xml.createElement(time)124 time.appendChild(xml.createTextNode(CreateTest.test_time()125 # 輸入用例執行時間126 127 case.appendChild(name)128 case.appendChild(method)129 case.appendChild(code)130 case.appendChild(result)131 case.appendChil
17、d(time)132 133 caselist.appendChild(case)134 # xml文件生成結束135 filename = file(CreateTest.test_report(), w+)136 # 生成以當前時間命名的測試報告文件137 xml.writexml(filename)138 filename.close()139 # 關閉文件140 141 142 if _name_ = _main_:143 CreateTest.test_main()下面是測試入口: 1 #!/usr/bin/env python 2 # -*- coding: utf_8 -*- 3 # * 4 # interface.py 5 # A
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 宇航級芯片管理制度
- 安全網格化管理制度
- 安裝師安裝管理制度
- 定制品訂單管理制度
- 實訓室專人管理制度
- 審批局保密管理制度
- 客服部客戶管理制度
- 宮腔鏡手術管理制度
- 家庭式公司管理制度
- 應急供水點管理制度
- 2025江西中考:歷史高頻考點
- 品控考試題及答案
- 急性心肌梗死診斷分型及再灌注治療策略李軼課件
- 船舶消防知識試題及答案
- 《建黨偉業》觀后感課件
- 專題08 文學作品閱讀(必考題型梳理)60題(原卷版)-2023-2024學年八年級語文下學期期中專題復習(浙江專用)
- 2025年安全生產月主題宣貫課件
- 微生物檢驗數據記錄與管理試題及答案
- 廣東省美術試題及答案
- 數學建模思想在中小學數學課堂教學中的應用研究
- 2025年五級應急救援員資格理論考試題庫(含答案)
評論
0/150
提交評論