简单的Python+requests+excel示例进行接口测试
接口文档:
登录系统 请求消息 POST /api/mgr/signin HTTP/1.1 Content-Type: application/x-www-form-urlencoded 请求参数 http 请求消息 body 中 参数以 格式 x-www-form-urlencoded 存储 需要携带如下参数, username 用户名 password 密码 响应消息 HTTP/1.1 200 OK Content-Type: application/json 响应内容 http 响应消息 body 中, 数据以json格式存储, 如果登录成功,返回如下 { "ret": 0 } ret 为 0 表示登录成功 如果登录失败,返回失败的原因,示例如下 { "ret": 1, "msg": "用户名或者密码错误" } ret 不为 0 表示登录失败, msg字段描述登录失败的原因
测试用例:
执行用例中Login_01,输入错误的密码登录
import xlrd3 import requests import json ''' 定义一个方法,获取Excel文件指定页签指定单元格的内容 ''' def getExcelContent(filePath,sheetName,row,col): with xlrd3.open_workbook(filePath) as f: sheetNames = f.sheet_names() if sheetName in sheetNames: pass else: print('Error : not found this sheetName') return rows = f.sheet_by_name(sheetName).nrows cols = f.sheet_by_name(sheetName).ncols if row in range(rows): pass else: print('Error : wrong row') return if col in range(cols): pass else: print('Error : wrong col') return content = f.sheet_by_name(sheetName).cell_value(row,col) return content if __name__ == '__main__': filePath = 'D:\PycharmProjects\AutoAPI\TestCase\TestCase.xls' sheetName = '模块A' row = 1 testCaseNum = getExcelContent(filePath,sheetName,row,0) testCaseName = getExcelContent(filePath,sheetName,row,1) host = getExcelContent(filePath,sheetName,row,2) path = getExcelContent(filePath,sheetName,row,3) url = host+path method = getExcelContent(filePath,sheetName,row,4) params = getExcelContent(filePath,sheetName,row,5) headers = getExcelContent(filePath,sheetName,row,6) checkField = getExcelContent(filePath,sheetName,row,7) checkValue = getExcelContent(filePath,sheetName,row,8) if method == 'get': r = requests.get(url,params=params) if method == 'post': # 请求头headers要求是字典格式,所以需要使用json.loads()函数将json字符串转换为字典 r = requests.post(url,data=params,headers=json.loads(headers)) #返回消息为json字符串,转换为字典 message = json.loads(r.text) print("用例编号 : " + testCaseNum + "\n用例标题 : " + testCaseName + "\n接口返回值 : " + message['msg'] + "\n校验字段值 : " + checkValue) #判断接口返回值与校验字段值是否 if message['msg'] == checkValue: print("测试结果 : 接口返回值与校验字段值相符,测试通过") else: print("测试结果 : 接口返回值与校验字段值不符,测试不通过")
运行结果:
用例编号 : Login_01
用例标题 : 输入错误的密码登录
接口返回值 : 用户名或者密码错误
校验字段值 : 用户名或者密码错误
测试结果 : 接口返回值与校验字段值相符,测试通过
执行用例中的Login_03 输入正确的用户名和密码登录
import xlrd3 import requests import json # 定义一个方法,获取Excel文件指定页签指定单元格的内容 def getExcelContent(filePath,sheetName,row,col): with xlrd3.open_workbook(filePath) as f: sheetNames = f.sheet_names() if sheetName in sheetNames: pass else: print('Error : not found this sheetName') return rows = f.sheet_by_name(sheetName).nrows cols = f.sheet_by_name(sheetName).ncols if row in range(rows): pass else: print('Error : wrong row') return if col in range(cols): pass else: print('Error : wrong col') return content = f.sheet_by_name(sheetName).cell_value(row,col) return content if __name__ == '__main__': filePath = 'D:\PycharmProjects\AutoAPI\TestCase\TestCase.xls' sheetName = '模块A' row = 3 testCaseNum = getExcelContent(filePath,sheetName,row,0) testCaseName = getExcelContent(filePath,sheetName,row,1) host = getExcelContent(filePath,sheetName,row,2) path = getExcelContent(filePath,sheetName,row,3) url = host+path method = getExcelContent(filePath,sheetName,row,4) params = getExcelContent(filePath,sheetName,row,5) headers = getExcelContent(filePath,sheetName,row,6) checkField = getExcelContent(filePath,sheetName,row,7) checkValue = getExcelContent(filePath,sheetName,row,8) if method == 'get': r = requests.get(url,params=params) if method == 'post': # 请求头headers是字典格式,所以需要json.loads()函数是将json格式数据转换为字典 r = requests.post(url,data=json.loads(params),headers=json.loads(headers)) message = json.loads(r.text) print("用例编号 : " + testCaseNum + "\n用例标题 : " + testCaseName + "\n接口返回值 : " + str(message[checkField]) + "\n校验字段值 : " + str(int(checkValue))) if message[checkField] == int(checkValue): print("测试结果 : 接口返回值与校验字段值相符,测试通过") else: print("测试结果 : 接口返回值与校验字段值不符,测试不通过")
执行结果
用例编号 : Login_03
用例标题 : 输入正确的用户名和密码登录
接口返回值 : 0
校验字段值 : 0
测试结果 : 接口返回值与校验字段值相符,测试通过
钟声敲响了日落,柏油路跃过山坡,一直通向北方的是我们想象,长大后也未曾经过~