小試牛刀——python接口測試小框架


用例設計:

 


執行用例代碼:

# -*- coding: UTF-8 -*-
import xlrd,logging,urllib,urllib2,json,sys
from pylsy import pylsytable

#######################################################################################################
#定義系統輸出編碼
reload(sys)
sys.setdefaultencoding('utf-8')

#########################################################################################################
#定義日志輸出
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='myapp.log',
filemode='w')

#################################################################################################
#定義一個StreamHandler,將INFO級別或更高的日志信息打印到標准錯誤,並將其添加到當前的日志處理對象#
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
#################################################################################################

###################################################################################################
#處理excel表格
data = xlrd.open_workbook('C:\Users\xxxxx\Desktop\API.xls')#打開excel表格
logging.info("打開%s excel表格成功"%data)
table = data.sheet_by_name(u'Sheet2')#打開工作表sheet2
logging.info("打開%s表成功"%table)
nrows = table.nrows#統計行數
logging.info("表中有%s行"%nrows)
ncols = table.ncols#統計列數
logging.info("表中有%s列"%ncols)
logging.info("開始進行循環")
name_1=[];url_1=[];params_1=[];type_1=[];Expected_result_1=[];Actual_result_1 =[];test_result_1=[];Remarks_1=[]#定義數組
Success=0;fail=0 #初始化成功失敗用例
##################################################################################################################
for i in range(1,nrows):#遍歷excel表格
cell_A3 =table.row_values(i)
#獲取excel表格中的數據
name = cell_A3[0]
url = cell_A3[1]
params=eval(cell_A3[2])
type = cell_A3[3]
error_code =cell_A3[4]
Remarks =cell_A3[5]
logging.info(url)
#############################################################################################################################3
params =urllib.urlencode(params) #參數化處理
logging.info(params)
url2 = urllib2.Request(url,params)
print "***********開始執行請求************"
response = urllib2.urlopen(url2)
logging.info(response)
apicontent = response.read()
logging.info(apicontent)
apicontent = json.loads(apicontent)
#驗證返回值
if apicontent["error_code"]==int(error_code):
name2="通過"
print name+"測試通過"
else:
name2="失敗"
print name+"測試失敗"
name_1.append(name)
url_1.append(url)
params_1.append(params)
type_1.append(type)
Expected_result_1.append(int(error_code))
Actual_result_1.append(apicontent["error_code"])
test_result_1.append(name2)
Remarks_1.append(Remarks)
if name2=="通過":
Success+=1
elif name2=="失敗":
fail +=1
else:
print "測試結果異常"

##############################################################################################################################
#輸出表格形式
attributes =["urlname","url","params","type","Expected_result","Actual_result","test_result","Remarks"]
table =pylsytable(attributes)
name =name_1
url =url_1
params=params_1
type=type_1
Expected_result=Expected_result_1
Actual_result =Actual_result_1
test_result=test_result_1
Remarks=Remarks_1
table.add_data("urlname",name)
table.add_data("url",url)
table.add_data("params",params)
table.add_data("type",type)
table.add_data("Expected_result",Expected_result)
table.add_data("Actual_result",Actual_result)
table.add_data("test_result",test_result)
table.add_data("Remarks",Remarks)
table._create_table()
print table
print "成功的用例個數為:%s"%Success,"失敗的用例個數為:%s"%fail
print "***********執行測試成功************"


執行結果:

 

 

 
       


免責聲明!

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



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