這個是根據上次框架版本進行的優化html
用python獲取excel文件中測試用例數據python
經過requets測試接口、並使用正則表達式驗證響應信息內容正則表達式
生成xml文件測試報告json
版本更新內容:api
1. 整理了CreateTest.test_main()流程邏輯服務器
2. 優化了testcase.xls文件格式app
3. 添加了生成XML文件測試報告框架
#!/usr/bin/env python # -*- coding: utf_8 -*- # 獲取測試用例文件excel import xlrd import json class CreateExcel: def __init__(self): pass @classmethod def open_excel(cls): path = "testcase.xls" workbook = xlrd.open_workbook(path) table = workbook.sheets()[0] return table # 獲取sheet @classmethod def get_nrows(cls, table): nrows = table.nrows return nrows # 獲取行號 @classmethod def get_id(cls, table, nrows): testid = [] for i in range(1, nrows): testid.append(table.cell(i, 0).value) return testid @classmethod def get_name(cls, table, nrows): testname = [] for i in range(1, nrows): testname.append(table.cell(i, 1).value) return testname # 獲取用例name @classmethod def get_data(cls, table, nrows): testdata = [] for i in range(1, nrows): try: data = json.loads(table.cell(i, 2).value) testdata.append(data) except ValueError: testdata.append(None) return testdata # 獲取data接口參數 @classmethod def get_url(cls, table, nrows): testurl = [] for i in range(1, nrows): testurl.append(table.cell(i, 3).value) return testurl # 獲取接口測試url @classmethod def get_method(cls, table, nrows): testmethod = [] for i in range(1, nrows): testmethod.append(table.cell(i, 4).value) return testmethod # 獲取接口測試method @classmethod def get_pattern(cls, table, nrows): testpattern = [] for i in range(1, nrows): testpattern.append(table.cell(i, 5).value) return testpattern # 獲取接口指望響應結果
#!/usr/bin/env python # -*- coding: utf_8 -*- # 測試核心組件 import requests import re from datetime import datetime from createexcel import CreateExcel from xml.dom import minidom import sys class CreateTest: reload(sys) sys.setdefaultencoding("utf-8") # 避免字符串寫入文件出錯 def __init__(self): pass @classmethod def test_api(cls, method, url, data): global results try: if method == "post": results = requests.post(url, data) if method == "get": results = requests.get(url, data) return results except Exception.__bases__: print "服務器訪問失敗" # 接口函數 @classmethod def test_on(cls): print "用例執行開始" @classmethod def test_close(cls): print "用例執行結束" @classmethod def test_result(cls, pa): global report try: pattern = re.compile(pa) match = pattern.search(testresults.text) if match.group() == pa: report = "測試經過" except AttributeError: report = "測試失敗" return report # 正則表達式檢測 @classmethod def test_http(cls, code): print "請求返回狀態碼: ", code @classmethod def test_time(cls): nowtime = datetime.today() time = nowtime.strftime("%Y-%m-%d %H:%M:%S") return time # 獲取當前時間轉化字符串 @classmethod def test_report(cls): nowtime = datetime.today() reportime = nowtime.strftime("%Y%m%d%H%M%S") reportname = reportime + ".xml" return reportname # 獲取測試報告文件名稱 @classmethod def test_main(cls): global testresults table = CreateExcel.open_excel() nrows = CreateExcel.get_nrows(table) xml = minidom.Document() xml.appendChild(xml.createComment("測試報告")) caselist = xml.createElement("caselist") xml.appendChild(caselist) for i in range(0, nrows - 1): testid = CreateExcel.get_id(table, nrows)[i] testname = CreateExcel.get_name(table, nrows)[i] testdata = CreateExcel.get_data(table, nrows)[i] testurl = CreateExcel.get_url(table, nrows)[i] testmethod = CreateExcel.get_method(table, nrows)[i] testpattern = CreateExcel.get_pattern(table, nrows)[i] # 執行測試 CreateTest.test_on() testresults = CreateTest.test_api(testmethod, testurl, testdata) testcode = str(testresults.status_code) try: CreateTest.test_http(testresults.status_code) except AttributeError: pass CreateTest.test_close() # 執行結束 # 生成xml文件 case = xml.createElement("case") case.setAttribute("id", testid) # 輸入用例ID name = xml.createElement("name") name.appendChild(xml.createTextNode(testname)) # 輸入用例名稱 method = xml.createElement("method") method.appendChild(xml.createTextNode(testmethod)) # 輸入接口類型 code = xml.createElement("code") code.appendChild((xml.createTextNode(testcode))) # 輸入用例返回狀態碼 result = xml.createElement("result") result.appendChild(xml.createTextNode(CreateTest.test_result(testpattern))) # 輸入用例測試結果 time = xml.createElement("time") time.appendChild(xml.createTextNode(CreateTest.test_time())) # 輸入用例執行時間 case.appendChild(name) case.appendChild(method) case.appendChild(code) case.appendChild(result) case.appendChild(time) caselist.appendChild(case) # xml文件生成結束 filename = file(CreateTest.test_report(), "w+") # 生成以當前時間命名的測試報告文件 xml.writexml(filename) filename.close() # 關閉文件 if __name__ == '__main__': CreateTest.test_main()
下面是測試入口:dom
#!/usr/bin/env python # -*- coding: utf_8 -*- # **************************************************************** # interface.py # Author : ChenLei # Version : 2.0 # Date : 2016-4-15 # **************************************************************** import time from createtest import CreateTest start = time.clock() CreateTest.test_main() end = time.clock() print "接口自動化腳本運行時間:%.03f seconds" % (end - start)
運行後自動生成 當前時間的xml文件 以下:函數
除非註明,本博客文章均爲原創,轉載請以連接形式標明本文地址
本文地址:http://www.cnblogs.com/cllovewxq/p/5394549.html