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 CreateExcel:
 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     # 獲取行號
 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         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     @classmethod
 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, 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 # 測試核心組件
  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
 23     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         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 = re.compile(pa)
 49             match = pattern.search(testresults.text)
 50             if match.group() == pa:
 51                 report = "測試通過"
 52         except AttributeError:
 53             report = "測試失敗"
 54         return 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         nowtime = 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.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, nrows)[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.status_code)
100             try:
101                 CreateTest.test_http(testresults.status_code)
102             except AttributeError:
103                 pass
104             CreateTest.test_close()
105             # 執行結束
106             # 生成xml文件
107             case = xml.createElement("case")
108             case.setAttribute("id", testid)
109             # 輸入用例ID
110 
111             name = xml.createElement("name")
112             name.appendChild(xml.createTextNode(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.createTextNode(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.appendChild(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 # Author     : ChenLei
 6 # Version    : 2.0
 7 # Date       : 2016-4-15
 8 # ****************************************************************
 9 
10 import time
11 from createtest import CreateTest
12 
13 start = time.clock()
14 CreateTest.test_main()
15 end = time.clock()
16 
17 print "接口自動化腳本運行時間:%.03f seconds" % (end - start)

 運行后自動生成 當前時間的xml文件 如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM