主要内容:
测试用例函数需要编写的代码:
- 1.获取实际结果,requests.request()
- 2.预期结果
- 3.断言?
除了这三个操作以外,都是可以作为前置或者后置。(登录获取member_id,token)
审核接口分析:
1.新增项目前需登录:
接口名称:/member/login ,请求方法:POST,管理员token
接口名称:/member/login ,请求方法:POST,借款人token
2.审核前需要新增项目:
接口名称:/loan/add , 请求方法:POST ,请求参数?member_id,借款人token
3.审核项目:
接口方法:/loan/audit ,请求方法:PATCH ,请求参数:load_id, 管理员token
前置接口定义:
import requests
from setting.config import host
from jsonpath import jsonpath
from setting.secret import user, admin_user
class ApiCase:
"""访问项目接口需要用到的独立的方法或者属性"""
@classmethod
def login(cls):
"""访问登录接口"""
url = host + '/member/login'
method = 'post'
headers = {"X-Lemonban-Media-Type": "lemonban.v2"}
data = user
login_resp = requests.request(method=method, url=url, headers=headers, json=data).json()
# print(login_resp)
# 获取 member_id和token
# cls.member_id = str(login_resp['data']['id'])
# cls.token = login_resp['data']['token_info']['token']
# 通过jsonpath获取
cls.member_id = str(jsonpath(login_resp, '$..id')[0])
cls.token = jsonpath(login_resp, '$..token')[0]
# cls.before_money = jsonpath(login_resp, '$..leave_amount')[0]
# return cls.member_id, cls.token
@classmethod
def admin_login(cls):
"""访问登录接口"""
url = host + '/member/login'
method = 'post'
headers = {"X-Lemonban-Media-Type": "lemonban.v2"}
data = admin_user
login_resp = requests.request(method=method, url=url, headers=headers, json=data).json()
# 通过jsonpath获取
cls.admin_member_id = str(jsonpath(login_resp, '$..id')[0])
cls.admin_token = jsonpath(login_resp, '$..token')[0]
@classmethod
def add_project(cls, token, member_id):
"""访问添加项目的接口"""
url = host + '/loan/add '
method = 'post'
headers = {"X-Lemonban-Media-Type": "lemonban.v2",
"Authorization": f"Bearer {token}"}
json_data = {"member_id": member_id,
"amount": 100, "title": "小目标",
"loan_rate": 18, "loan_term": 5, "loan_date_type": 1, "bidding_days": 1}
login_resp = requests.request(method=method, url=url, headers=headers, json=json_data).json()
# 通过jsonpath获取
cls.loan_id = str(jsonpath(login_resp, '$..id')[0])
审核测试用例代码:
import unittest
import requests
import json
from common.excel import read_excel
from ddt import ddt, data
from setting.config import config_file, host
from tests.base import ApiCase
# 得到测试数据
excel_data = read_excel(file=config_file, sheet_name='audit')
@ddt
class TestAudit(unittest.TestCase, ApiCase):
@classmethod
def setUpClass(cls) -> None:
'''测试用例需要的前置夹具,每个测试类只执行一次的前置
1.登录
2.获取充值接口需要的member_id和token
'''
# TestAudit继承ApiCase,cls.login(),对象可以获取类属性
# 普通用户登陆
cls.login()
# 管理员登陆
cls.admin_login()
def setUp(self) -> None:
# 添加项目
self.add_project(token=self.token, member_id=self.member_id)
def tearDown(self) -> None:
pass
@data(*excel_data)
def test_audit(self, info):
url = host + info['url']
json_data = info['json']
headers = info['headers']
# 替换请求体
if '#loan_id#' in json_data:
json_data = json_data.replace("#loan_id#", self.loan_id)
# 替换请求头
if '#admin_token#' in headers:
headers = headers.replace("#admin_token#", self.admin_token)
# 将json数据转化为字典
json_data = json.loads(json_data)
headers = json.loads(headers)
expected = json.loads(info['expected'])
audit_resp = requests.request(method=info['method'], url=url, headers=headers, json=json_data).json()
for key, value in expected.items():
self.assertEqual(value, audit_resp[key])
|