Python+requests+excel接口测试
2018-06-14 17:00:13
环境准备:
- Python 3.7
- requests库
- xlrd
1、创建Excel文件
2、读取Excel文件
import xlrd class readExcel(object): def __init__(self, path): self.path = path @property def getSheet(self): # 获取索引 xl = xlrd.open_workbook(self.path) sheet = xl.sheet_by_index(1) # print( xl.sheet_names() ) 打印所有sheet名字 # print (sheet.cell_value( 2, 3 )) 打印第3行第4列 return sheet @property def getRows(self): # 获取行数 row = self.getSheet.nrows return row @property def getCol(self): # 获取列数 col = self.getSheet.ncols return col # 以下是分别获取每一列的数值 @property def getId(self): TestId = [] for i in range( 1, self.getRows ): TestId.append( self.getSheet.cell_value( i, 0 ) ) # print(TestName) return TestId @property def getName(self): TestName = [] for i in range(1, self.getRows): TestName.append(self.getSheet.cell_value(i, 1)) # print(TestName) return TestName @property def getData(self): TestData = [] for i in range(1, self.getRows): TestData.append(self.getSheet.cell_value(i, 2)) return TestData @property def getUrl(self): TestUrl = [] for i in range(1, self.getRows): TestUrl.append(self.getSheet.cell_value(i, 3)) return TestUrl @property def getMethod(self): TestMethod = [] for i in range(1, self.getRows): TestMethod.append(self.getSheet.cell_value(i, 4)) return TestMethod @property def getStatusCode(self): TestUid = [] for i in range(1, self.getRows): TestUid.append(self.getSheet.cell_value(i, 5)) return TestUid @property def getCode(self): TestCode = [] for i in range(1, self.getRows): TestCode.append(self.getSheet.cell_value(i, 6)) return TestCode
3、封装请求类型与返回的数据,此处只封装了get和post请求,还有delete、put、options、head等,有兴趣的可以自行添加
import requests import json from baseData import readExcel from common import keywords class testApi(object): def __init__(self, method, url, data): self.method = method self.url = url self.data = data @property def headers(self): headers = { "Content-Type": "application/json" } return headers @property def testApi(self): # 根据不同的访问方式来访问接口 try: if self.method == 'post': r = requests.post(self.url, data=json.dumps(eval(self.data)), headers=self.headers) elif self.method == 'get': r = requests.get(self.url, params=self.data) return r except: print('失败') def getCode(self): # 获取访问接口的状态码 code = self.testApi.json()['code'] return code def getStatusCode(self): # 获取返回信息status_code status_code = self.testApi.json()['status_code'] return status_code def getJson(self): # 获取返回信息的json数据 json_data = self.testApi.json() return json_data
4、通过unittest执行测试用例,用HTMLTestRunner生成测试报告
from baseData import readExcel from testApiWay import testApi from base_test import baseTest import unittest from HTMLTestRunner import HTMLTestRunner from common import keywords #一些参数封装成了关键字 class testLoginApi( baseTest ): def testLoginApi(self): '''测试登陆接口,登陆的几种情况。''' excel = readExcel( r'C:\Users\Jasmine\Desktop\data.xlsx' ) name = excel.getName data = excel.getData url = excel.getUrl method = excel.getMethod id = excel.getId status_code = excel.getStatusCode code = excel.getCode row = excel.getRows # print(code) for i in range( 0, row - 1 ): api = testApi( method[i], url[i], data[i]) # apicode = api.getCode() # print(apicode) apistatus = api.getStatusCode() print(apistatus) apijson = api.getJson() if apistatus == status_code[i]: print('{}.{}:测试成功。json数据为:{}'.format( id[i], name[i], apijson )) #i+1 else: print('{}.{}:测试失败。json数据为:{}'.format( id[i], name[i], apijson )) # 生成测试报告 def runAutomation(): suite = unittest.TestLoader().loadTestsFromTestCase( testLoginApi ) runner = HTMLTestRunner( stream=open(keywords.Time+ 'testReport.html', 'wb' ), #k.getNowTime() title=u'TestReport', description=u'测试报告详细信息' ) runner.run( suite ) if __name__ == '__main__': # runAutomation() unittest.main( verbosity=2 )
ps:Python3中HTMLTestRunner生成的测试报告很简陋,需要自己手动修改HTMLTestRunner.py文件,由于我是初学者,所以还在研究用其他方法生成测试报告