添加项目测试用例:
import unittest
import os
import requests
from unittestreport import ddt, list_data
from common.handle_excel import HandleExcel
from common.handle_path import DATA_DIR
from common.handle_conf import conf
from common.tools import replace_data
from common.handle_log import my_log
from common.handle_mysql import HandleDB
'''
添加项目,需要登陆
定义用例的前置方法(类级别)
'''
@ddt
class TestAdd(unittest.TestCase):
excel = HandleExcel(os.path.join(DATA_DIR, ' apicase.xlsx', 'add'))
cases = excel.read_data()
db = HandleDB()
@classmethod
def setUpClass(cls) -> None:
'''前置登陆'''
url = conf.get('env', 'base_url') + '/member/login'
params = {
"mobile_phone": conf.get("test_data", "mobile"),
"ped": conf.get("test_data", "pwd")
}
headers = eval(conf.get("env", "headers"))
response = requests.post(url=url, json=params, headers=headers)
res = response.json()[0]
token = jsonpath(res, '$..token')
headers['Authorization'] = 'Bearer ' + token
cls.headers = headers
cls.member_id = jsonpath(res, '$..id')[0]
@list_data(cases)
def test_add(self, item):
url = conf.get('env', 'base_url') + item['url']
item['data'] = replace_data(item['data', TestAdd])
params = eval(item['data'])
expected = eval(item['expected'])
method = item['methond']
sql = "select * from futureloan.loan where member_id={}".format(self.member_id)
start_count = self.db.find_one(sql)
response = requests.request(method=method, url=url, json=params, headers=self.headers)
res = response.json()
sql = "select * from futureloan.loan where member_id={}".format(self.member_id)
end_count = self.db.find_one(sql)
try:
self.assertEqual(expected['code'], res['code'])
self.assertEqual(expected['msg'], res['msg'])
except AssertionError as e:
my_log.error("用例{}执行失败".format(item['title']))
if res['msg'] == 'OK':
self.assertEqual(end_count - start_count, 1)
else:
self.assertEqual(end_count - start_count, 0)
my_log.exception(e)
raise e
else:
my_log.info("用例{}执行成功".format(item['title']))