接口公共方法有:數據引擎、http引擎、Excel引擎html
一、數據引擎:獲取用例、結果檢查、結果統計web
# -*- coding:utf-8 -*- from XlsEngine import XlsEngine_rd import os '''獲取用例''' def getCase(): filepath = os.path.abspath('.') filename = filepath + "/interfacetest/Data/InterfaceData.xlsx" data = XlsEngine_rd(filename) data.xlrd_open() sheet = data.xlrd_object.sheet_by_index(0) rows = sheet.nrows domain = sheet.cell_value(1,1) header_temp = sheet.cell_value(2,1) header=eval(header_temp) case_list=[] for i in range(3,rows): case_list.append(sheet.row_values(i)) return domain,case_list,header '''結果檢查''' def resultCheck(actual_result, expect_result): result = "Failed" actualre = actual_result.content area = (expect_result.split(':'))[0] expect = (expect_result.split(':'))[1] if area == "response_code": if str(actual_result.status_code) == expect: result = "Pass" actualre = "response_code:"+expect if area == "content": expect = expect_result.replace("content:","").encode('utf-8') actual = actual_result.content if expect in str(actual): result = "Pass" actualre = expect return result,actualre '''結果統計''' def countResult(resultlist): passcount=0 failcount=0 for result in resultlist: if result[5] == 'Pass': passcount+=1 else: failcount+=1 return passcount,failcount
二、http引擎,用於發送請求和響應接收,提供登陸方法,供須要登陸的接口調用app
# -*- coding:utf-8 -*- import requests def getData(s, url, data, header, method): re=object isexcept = False if method == "post": try: re = s.post(url, headers=header, data=data) except requests.exceptions.ConnectionError,e: re = e isexcept = True if method == "get": try: re = s.get(url, headers=header, data=data) except requests.exceptions.ConnectionError,e: re = e isexcept = True if method == "delete": try: re = s.delete(url+"/"+data) except requests.exceptions.ConnectionError,e: re = e isexcept = True if method == "put": try: re = s.put(url) except requests.exceptions.ConnectionError,e: re = e isexcept = True return re,isexcept def login(): s=object isexcept = False try: url="http://baidu.com/login" header = {"Referer": "http://baidu.com"} data={"username":"admin", "password":"xxx"} s=requests.Session() s.post(url, headers=header, data=data) except requests.exceptions.ConnectionError,e: s=e isexcept = True return s, isexcept
三、Excel引擎:excel文件操做框架
# coding=utf-8 import xlrd import xlwt class XlsEngine_rd(): def __init__(self, filename): self.xls_name = filename self.xlrd_object = None self.xlwt_object = None self.isopenfailed = True def xlrd_open(self): try: #xlrd.Book.encoding="utf-8" self.xlrd_object = xlrd.open_workbook(self.xls_name) self.isopenfailed = False except Exception,e: self.isopenfailed = True self.xlrd_object = None print(e) return [self.isopenfailed, self.xlrd_object]