标签:lin cookie header flask框架 核心 模板 .text interface 模版
设计原理 基于http协议接口的测试设计,莫过于Python的requests库,简单粗暴易理解。
设计模式 采用python的flask框架,搭建一套接口自动化测试平台。 测试用例维护:采用Excel 测试结果保存:采用MongoDb存储,HTML页面展示
相关核心代码介绍:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
import xlrd import os # **************************************************************** # Excel模版设置 # self.interFace = 0 #Excel中测试用例接口对应为第1列 # self.interFaceArgList = 1 #Excel中测试用例接口参数列表对应为第2列 # self.loginChoice = 2 #Excel中测试用例接口是否需要登录为第3列 # self.loginJson = 3 #Excel中测试用例接口是否需要登录为第4列 # self.titleIndex = 4 #Excel中测试用例标题行索引为第5列 # self.caseBegin = 5 #Excel中测试用例开始行索引为第6列 # **************************************************************** class ExcelSheet: def __init__( self , sFile, interFace = 0 , interFaceArgList = 1 , loginInterFace = 2 , loginJson = 3 , titleIndex = 4 , caseBegin = 5 ): try : excel = xlrd.open_workbook(sFile) except Exception as e: print e exit() self .sheet = excel.sheet_by_index( 0 ) # 查询Excel的第一个sheet self .interFace = interFace self .interFaceArgList = interFaceArgList self .loginInterFace = loginInterFace self .titleIndex = titleIndex self .caseBegin = caseBegin self .loginJson = loginJson def sheet_name( self ): return self .sheets.name def nrows( self ): return self .sheet.nrows def ncols( self ): return self .sheet.ncols def cellxy( self , rowx, colx): # type: (object, object) -> object cell_value = self .sheet.cell(rowx, colx).value # 对数字的处理 if self .sheet.cell(rowx, colx).ctype in ( 2 , 3 ) and int (cell_value) = = cell_value: cell_value = int (cell_value) return cell_value # interFace 测试接口URL def get_interFace( self ): return self .cellxy( self .interFace, 1 ) # interFace接口的参数List def get_interFaceArgList( self ): return self .cellxy( self .interFaceArgList, 1 ).split( "#" ) # 测试用例的参数项 def get_titleIndex( self ): return self .sheet.row_values( self .titleIndex) # 登录的接口地址 def get_loginInterFace( self ): return self .cellxy( self .loginInterFace, 1 ) # 获取登录接口参数的json def get_loginJson( self ): return str ( self .cellxy( self .loginJson, 1 )) # 返回单行用例的数据字典 def get_by_line( self , line): tempdict = dict () data = dict () if line < self .caseBegin: return False else : for col in range ( self .ncols()): if self .cellxy( self .titleIndex, col) in self .get_interFaceArgList(): if self .cellxy(line, col) ! = ‘X‘ : data[ self .cellxy( self .titleIndex, col)] = self .cellxy(line, col) else : tempdict[ self .cellxy( self .titleIndex, col)] = self .cellxy(line, col) tempdict[ "data" ] = data return tempdict |
requests的post和get请求代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import requests def postRequest(url, data, cookie): header = { "Content-Type" : "application/json" } if cookie: return requests.post(url, data = data, cookies = cookie, headers = header) else : return requests.post(url, data = data, headers = header) def postRequest(url, cookie): header = { "Content-Type" : "application/json" } if cookie: return requests.get(url, cookies = cookie, headers = header) else : return requests.get(url, headers = header) |
1
2
3
4
5
6
7
|
def checkReturnResult(req_result): try : realResult = eval (req_result) except : return False else : return req_result |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# -*- coding: utf-8 -*- import pymongo class ConMongoDb: #读取配置文件 def __init__( self ): MongoIP = "192.168.X.XXX" MongoPort = 27017 #链接MongoDb self .conn = pymongo.Connection(MongoIP, MongoPort) #选择数据库 self .db = self .conn.test self .collection = self .db.SocketTest ‘‘‘ # ************************************************** # InsertMongo:往MongoDb插入数据 # ************************************************** ‘‘‘ def InsertMongo( self , Lst): self .collection.insert(Lst) |
def close(self): return self.conn.disconnect()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
import time ExcelSheet = ExcelSheet( "104137PC.xlsx" ) interFace = ExcelSheet.get_interFace() interFaceArgList = ExcelSheet.get_interFaceArgList() titleIndex = ExcelSheet.get_titleIndex() loginInterFace = ExcelSheet.get_loginInterFace() # 判断是否需要登录 if loginInterFace: if "username" not in titleIndex or "password" not in titleIndex: print "Test Case File not include username or password" exit() else : # 获取登录接口参数的json loginJson = ExcelSheet.get_loginJson() caseList = list () for line in range ( 5 , ExcelSheet.nrows()): lineContent = ExcelSheet.get_by_line(line) # 获取登录后的cookie if loginInterFace: # 需要登录,用户名密码,替换 loginJson = loginJson.replace( "#username#" , lineContent[ "username" ]) loginJson = loginJson.replace( "#password#" , str (lineContent[ "password" ])) result = postRequest(loginInterFace, eval (loginJson), False ) print result.text cookie = result.cookies else : cookie = False # reqtype 不填默认post if lineContent.has_key( "reqtype" ): if lineContent[ "reqtype" ].upper() = = "POST" : interFaceResult = postRequest(interFace, lineContent[ "data" ], cookie) else : interFaceResult = postRequest(interFace, cookie) else : interFaceResult = postRequest(interFace, lineContent[ "data" ], cookie) req_result = interFaceResult.text # 非页面,可直接比较,也可以转换成字典进行某个value进行比较 if checkReturnResult(req_result): if checkReturnResult(req_result) = = lineContent[ "Except_result" ]: TestResult = "PASS" else : TestResult = "FAIL" #如果返回是页面的话,就查找特殊标志词 else : if ineContent[ "Except_result" ] in req_result: TestResult = "PASS" else : TestResult = "FAIL" lineContent[ "result" ] = TestResult caseList.append(lineContent) TestDate = time.strftime( "%Y-%m-%d %H:%M:%S" , time.localtime(time.time())) content = { "interFace" : interFace, "caseList" : caseList, "testdate" : TestDate} MyngoCls = ConMongoDb() MyngoCls.InsertMongo(content) MyngoCls.close() |
整个流程梳理:
标签:lin cookie header flask框架 核心 模板 .text interface 模版
原文地址:https://www.cnblogs.com/txx403341512/p/9355523.html