以前是利用python自帶的unittest測試框架python
此次本身設計一個 以後再一點點往裏面加功能json
(ps:固然這個框架真的是很簡單。。很簡單。。。很簡單。。。)api
excel文件格式:app
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 pass 11 12 @classmethod 13 def open_excel(cls): 14 path = "testcase.xls" 15 workbook = xlrd.open_workbook(path) 16 table = workbook.sheets()[0] 17 return table 18 # 獲取sheet 19 20 @classmethod 21 def get_nrows(cls, table): 22 nrows = table.nrows 23 return nrows 24 # 獲取行號 25 26 @classmethod 27 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 testname 32 # 獲取用例name 33 34 @classmethod 35 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 testdata 41 # 獲取data接口參數 42 43 @classmethod 44 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 testurl 49 # 獲取接口測試url 50 51 @classmethod 52 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 testmethod 57 # 獲取接口測試method 58 59 @classmethod 60 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 testpattern 65 # 獲取接口指望響應結果 66 67 @classmethod 68 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 testreport 73 # 獲取用例指望的運行結果 74 75 76 if __name__ == "__main__": 77 CreateExcel()
上面代碼是處理excel文檔的框架
下面代碼是測試平臺post
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 pass 13 14 @classmethod 15 def test_api(cls, method, url, data): 16 global results 17 if method == "post": 18 results = requests.post(url, data) 19 if method == "get": 20 results = requests.get(url, data) 21 return results 22 23 @classmethod 24 def test_on(cls): 25 print "用例執行開始" 26 27 @classmethod 28 def test_close(cls): 29 print "用例執行結束" 30 31 @classmethod 32 def test_result(cls, ra, rb): 33 if ra == rb: 34 print "測試結果: 測試經過" 35 else: 36 print "測試結果: 測試失敗" 37 38 @classmethod 39 def test_http(cls, code): 40 if code == 200: 41 print "測試請求: 請求經過" 42 43 @classmethod 44 def test_main(cls): 45 global report 46 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 "測試用例:", testname 57 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 = None 71 CreateTest.test_result(testreport, report) 72 CreateTest.test_close() 73 74 75 if __name__ == '__main__': 76 CreateTest()