标签:res 数据 xxx amount 传递 编写测试用例 %s __init__ des
(1)编写测试方法
http_requests.py
import requests class HttpRequest: def http_request(self, url, data, method,headers = None): ‘‘‘url:请求地址 http://xxxx:port param:传递的参数 非必填参数 字典的格式传递参数 method:请求方式 支持get,post,put... cookie:请求的时候传递的cookie值 ‘‘‘ if method.lower() == ‘get‘: res = requests.get(url, data) elif method.lower() == ‘post‘: res = requests.post(url, data, headers=headers) else: res = requests.patch(url, data, headers=headers) return res
(2)编写测试用例方法
test_http.py
import unittest import json from TestAuto.api_item.qingchengdai_Parameterization.http_request import HttpRequest from TestAuto.api_item.qingchengdai_Parameterization.get_data import GetData class TestHttp(unittest.TestCase): def setUp(self): pass def __init__(self,methodName,url,data,method,expected): # 通过初始化函数来传参数 super(TestHttp,self).__init__(methodName) # 保留父类的方法 self.url = url self.data = data self.method = method self.expected = expected def test_login_api(self): # 登录 # for item in test_data_login: res = HttpRequest().http_request(self.url, json.dumps(self.data), self.method,getattr(GetData,‘headers‘)) try: self.assertEqual(self.expected,res.json()[‘code‘]) except AssertionError as e: print("test_login_api error is {}".format(e)) raise e print(res.json()) def test_recharge_api(self): # 充值(需要登录的均可使用改api) login_url = ‘http://api.lemonban.com/futureloan/member/login‘ # 登录地址 test_data_login = {‘mobile_phone‘: ‘18669019958‘, ‘pwd‘: ‘12345678‘} Auth_json = HttpRequest().http_request(login_url, json.dumps(test_data_login), ‘POST‘,getattr(GetData, ‘headers‘)).json() token = Auth_json[‘data‘][‘token_info‘][‘token‘] token_type = Auth_json[‘data‘][‘token_info‘][‘token_type‘] Authorization_value = token_type + ‘ ‘ + token getattr(GetData, ‘headers‘)[‘Authorization‘] = Authorization_value # for item1 in test_data_recharge: res = HttpRequest().http_request(self.url, json.dumps(self.data), self.method, getattr(GetData, ‘headers‘)) try: self.assertEqual(self.expected,res.json()[‘code‘]) except AssertionError as e: print("test_login_api error is {}".format(e)) raise e print(res.json()) def tearDown(self): pass
请求头方法:
get_data.py
class GetData: headers = {‘X-Lemonban-Media-Type‘: ‘lemonban.v2‘, ‘Content-Type‘: ‘application/json; charset=UTF-8‘,‘Authorization‘:None}
(3)测试数据与生成测试报告
test_suite.py
import unittest import time from TestAuto.api_item.qingchengdai_Parameterization.test_http import TestHttp # 类名 import HTMLTestRunner login_url = ‘http://api.lemonban.com/futureloan/member/login‘# 登录地址 recharge_url = ‘http://api.lemonban.com/futureloan/member/recharge‘# 充值地址 test_data_login = [{‘url‘:login_url,‘data‘:{‘mobile_phone‘: ‘18669019958‘, ‘pwd‘: ‘12345678‘},‘method‘:‘post‘,‘expected‘:0}, {‘url‘:login_url,‘data‘:{‘mobile_phone‘: ‘18669019958‘, ‘pwd‘: ‘12345‘},‘method‘:‘post‘, ‘expected‘:1001}] test_data_recharge = [{‘url‘: recharge_url, ‘data‘: {‘member_id‘: ‘899710‘, ‘amount‘: ‘1000‘}, ‘method‘: ‘post‘, ‘expected‘: 0}, {‘url‘: recharge_url, ‘data‘: {‘member_id‘: ‘899711‘, ‘amount‘: ‘1000‘}, ‘method‘: ‘post‘, ‘expected‘: 1007}] suite = unittest.TestSuite() for item in test_data_login: # 创建登录实例 suite.addTest(TestHttp(‘test_login_api‘,item[‘url‘],item[‘data‘],item[‘method‘],item[‘expected‘])) # 实例的方式加载用例 for item1 in test_data_login: # 创建充值实例 suite.addTest(TestHttp("test_recharge_api",item1[‘url‘],item1[‘data‘],item1[‘method‘],item1[‘expected‘])) with open(‘test_report_qingchengdai_Parameterization.html‘,‘wb‘) as file: runner = HTMLTestRunner.HTMLTestRunner(stream=file, verbosity=2, title= time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time())) +‘测试报告标题‘, description=‘测试报告描述‘ ) runner.run(suite) if‘__name__‘ == ‘__main__‘: unittest.TestCase()
标签:res 数据 xxx amount 传递 编写测试用例 %s __init__ des
原文地址:https://www.cnblogs.com/kite123/p/12567772.html