標籤:
之前是利用python內建的unittest測試架構
這次自己設計一個 之後再一點點往裡面加功能
(ps:當然這個架構真的是很簡單。。很簡單。。。很簡單。。。)
excel檔案格式:
1 #!/usr/bin/env python 2 # -*- coding: utf_8 -*- 3 4 import xlrd 5 import json 6 7 8 class CreateExcel: 9 def __init__(self):10 pass11 12 @classmethod13 def open_excel(cls):14 path = "testcase.xls"15 workbook = xlrd.open_workbook(path)16 table = workbook.sheets()[0]17 return table18 # 擷取sheet19 20 @classmethod21 def get_nrows(cls, table):22 nrows = table.nrows23 return nrows24 # 擷取行號25 26 @classmethod27 def get_name(cls, table, nrows):28 testname = []29 for i in range(1, nrows):30 testname.append(table.cell(i, 0).value)31 return testname32 # 擷取用例name33 34 @classmethod35 def get_data(cls, table, nrows):36 testdata = []37 for i in range(1, nrows):38 data = json.loads(table.cell(i, 1).value)39 testdata.append(data)40 return testdata41 # 擷取data介面參數42 43 @classmethod44 def get_url(cls, table, nrows):45 testurl = []46 for i in range(1, nrows):47 testurl.append(table.cell(i, 2).value)48 return testurl49 # 擷取介面測試url50 51 @classmethod52 def get_method(cls, table, nrows):53 testmethod = []54 for i in range(1, nrows):55 testmethod.append(table.cell(i, 3).value)56 return testmethod57 # 擷取介面測試method58 59 @classmethod60 def get_pattern(cls, table, nrows):61 testpattern = []62 for i in range(1, nrows):63 testpattern.append(table.cell(i, 4).value)64 return testpattern65 # 擷取介面期望響應結果66 67 @classmethod68 def get_report(cls, table, nrows):69 testreport = []70 for i in range(1, nrows):71 testreport.append(table.cell(i, 5).value)72 return testreport73 # 擷取用例期望的運行結果74 75 76 if __name__ == "__main__":77 CreateExcel()
上面代碼是處理excel文檔的
下面代碼是測試平台
1 #!/usr/bin/env python 2 # -*- coding: utf_8 -*- 3 4 5 import requests 6 import re 7 from createexcel import CreateExcel 8 9 10 class CreateTest:11 def __init__(self):12 pass13 14 @classmethod15 def test_api(cls, method, url, data):16 global results17 if method == "post":18 results = requests.post(url, data)19 if method == "get":20 results = requests.get(url, data)21 return results22 23 @classmethod24 def test_on(cls):25 print "用例執行開始"26 27 @classmethod28 def test_close(cls):29 print "用例執行結束"30 31 @classmethod32 def test_result(cls, ra, rb):33 if ra == rb:34 print "測試結果: 測試通過"35 else:36 print "測試結果: 測試失敗"37 38 @classmethod39 def test_http(cls, code):40 if code == 200:41 print "測試請求: 請求通過"42 43 @classmethod44 def test_main(cls):45 global report46 table = CreateExcel.open_excel()47 nrows = CreateExcel.get_nrows(table)48 for i in range(0, nrows - 1):49 testname = CreateExcel.get_name(table, nrows)[i]50 testdata = CreateExcel.get_data(table, nrows)[i]51 testurl = CreateExcel.get_url(table, nrows)[i]52 testmethod = CreateExcel.get_method(table, nrows)[i]53 testpattern = CreateExcel.get_pattern(table, nrows)[i]54 testreport = CreateExcel.get_report(table, nrows)[i]55 CreateTest.test_on()56 print "測試案例:", testname57 try:58 testresults = CreateTest.test_api(testmethod, testurl, testdata)59 CreateTest.test_http(testresults.status_code)60 pattern = re.compile(testpattern)61 match = pattern.search(testresults.url)62 if match.group() == testpattern:63 report = "pass"64 CreateTest.test_result(testreport, report)65 except AttributeError:66 report = "no"67 CreateTest.test_result(testreport, report)68 except Exception.__base__:69 print "測試請求: 請求失敗"70 report = None71 CreateTest.test_result(testreport, report)72 CreateTest.test_close()73 74 75 if __name__ == ‘__main__‘:76 CreateTest()
python學習筆記(介面自動化架構)