python - 接口自动化测试实战 - case1 - 优化版
作者:互联网
题目:
基于以下两个接口和数据完成接口自动化测试,并生成测试报告:
'''
登录
login='http://47.107.168.87:8080/futureloan/mvc/api/member/login'
login_data={'mobilephone':18688773467,'pwd':'123456'}
充值
recharge='http://47.107.168.87:8080/futureloan/mvc/api/member/recharge'
recharge_data={'mobilephone':18688773467,'amount':'1000'}
'''
test_excel.xlsx:
http_request.py
# -*- coding:utf-8 -*- ''' @project: jiaxy @author: Jimmy @file: http_request.py @ide: PyCharm Community Edition @time: 2018-12-05 10:06 @blog: https://www.cnblogs.com/gotesting/ ''' import requests class HttpRequest: def http_request(self,url,param,method,cookies=None): if method == 'get': res = requests.get(url,param,cookies = cookies) elif method == 'post': res = requests.post(url,param,cookies = cookies) else: print('请求方法错误!') return res
read_excel.py
# -*- coding:utf-8 -*- ''' @project: jiaxy @author: Jimmy @file: read_excel.py @ide: PyCharm Community Edition @time: 2018-12-05 11:57 @blog: https://www.cnblogs.com/gotesting/ ''' from openpyxl import load_workbook class ReadExcel: def read_excel(self): wb = load_workbook('test_excel.xlsx') sheet = wb['登录及充值测试数据'] test_data = [] for i in range(2,sheet.max_row+1): sub_data = {} sub_data['url'] = sheet.cell(i,1).value sub_data['param'] = eval(sheet.cell(i,2).value) sub_data['method'] = sheet.cell(i,3).value sub_data['expected'] = sheet.cell(i,4).value test_data.append(sub_data) return test_data
test_api.py
# -*- coding:utf-8 -*- ''' @project: jiaxy @author: Jimmy @file: TestApi.py @ide: PyCharm Community Edition @time: 2018-12-05 10:09 @blog: https://www.cnblogs.com/gotesting/ ''' import unittest from TestApi.http_request import HttpRequest cookies = None class TestHttpApi(unittest.TestCase): def setUp(self): pass def tearDown(self): pass # 改写__init__方法,使用超继承 def __init__(self,url,param,method,expected,methodName): self.url = url self.param = param self.method = method self.expected = expected super(TestHttpApi,self).__init__(methodName) def test_api(self): global cookies res = HttpRequest().http_request(self.url,self.param,self.method,cookies) print('请求结果:',res.json()) if res.cookies: cookies = res.cookies try: self.assertEquals(self.expected,res.json()['msg']) except AssertionError as e: print('断言异常:',e) raise e
test_suite.py
# -*- coding:utf-8 -*- ''' @project: jiaxy @author: Jimmy @file: test_suite.py @ide: PyCharm Community Edition @time: 2018-12-05 10:28 @blog: https://www.cnblogs.com/gotesting/ ''' import unittest import HTMLTestRunner from TestApi.test_api import TestHttpApi from TestApi.read_excel import ReadExcel # 定义测试数据列表 test_data = ReadExcel().read_excel() print(test_data) ''' test_data = [{'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login','param':{'mobilephone':'','pwd':'123456'},'method':'get','expected':'手机号不能为空'}, {'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login','param':{'mobilephone':18688773467,'pwd':''},'method':'get','expected':'密码不能为空'}, {'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login','param':{'mobilephone':18688773467,'pwd':'12345678'},'method':'get','expected':'用户名或密码错误'}, {'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login','param':{'mobilephone':18688773467,'pwd':'123456'},'method':'get','expected':'登录成功'}, {'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge','param':{'mobilephone':'','amount':'1000'},'method':'post','expected':'手机号不能为空'}, {'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge','param':{'mobilephone':1868877346,'amount':'1000'},'method':'post','expected':'手机号码格式不正确'}, {'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge','param':{'mobilephone':18688773467,'amount':''},'method':'post','expected':'请输入金额'}, {'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge','param':{'mobilephone':18688773467,'amount':'100000000000'},'method':'post','expected':'请输入范围在0到50万之间的正数金额'}, {'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge','param':{'mobilephone':18688773467,'amount':'1000'},'method':'post','expected':'充值成功'}] ''' # 加载测试集 suite = unittest.TestSuite() for item in test_data: suite.addTest(TestHttpApi(item['url'], item['param'], item['method'], item['expected'], 'test_api')) # 执行测试,输出测试报告 with open('TestApi.html','wb+') as file: runner = HTMLTestRunner.HTMLTestRunner(file) runner.run(suite)
测试报告:
标签:http,python,self,param,url,api,接口,case1,method 来源: https://www.cnblogs.com/yangguanghandsome/p/12667512.html