class TestInvest(unittest.TestCase): # 通过读取配置文件得到 cases.xlsx file_name = config.read('excel', 'file_name') # 拼接配置文件的路径和名称 file_path = os.path.join(p_path.DATA_PATH, file_name) # Excel 表格名称 sheet_name = config.read('excel', 'sendMCode_sheet') # url 地址 url = config.read('http', 'combat_url') # 读取 excel 数据 test_data = ExcelHandler(file_path).read(sheet_name) @classmethod def setUpClass(cls): # HTTP请求 cls.req = RequestsHandler() # 初始化日志操作 cls.logger = logger @classmethod def tearDownClass(cls): pass @data(*test_data) def test_invest(self, test_info): # 访问的url地址 user_url = self.url client = suds.client.Client(user_url) data = eval(test_info['data']) res = client.service.sendMCode(data) self.assertEqual(test_info['expected'], res['retInfo'])
class TestLogin(unittest.TestCase): # 通过读取配置文件得到 cases.xlsx file_name = config.read('excel', 'file_name') # 拼接配置文件的路径和名称 file_path = os.path.join(p_path.DATA_PATH, file_name) # Excel 表格名称 sheet_name = config.read('excel', 'login_sheet') # url 地址 url = config.read('http', 'base_url') # 读取 headers 信息 headers = config.read('http', 'headers') # 读取 excel 数据 test_data = ExcelHandler(file_path).read(sheet_name) # 测试类方法,每一个测试类之前运行一次 @classmethod def setUpClass(cls): # 导入HTTP请求 cls.req = RequestsHandler() @data(*test_data) def test_register(self, test_info): # 调用 requests 模块访问接口 res = self.req.json( test_info['method'], # 请求方式 self.url + test_info['url'], # url地址 # 测试数据 获取json的默认是字典格式,所以要进行转换 json=eval(test_info['data']), # 读取 headers 信息 标题头 是字符串,需要转化成字典 headers=eval(self.headers)) print(res) # 预期结果,实际结果 self.assertEqual(test_info['expected'], res['msg']) # 部分断言,判断msg里面的数据
def login(): """登录,获取 token, member_id""" req = RequestsHandler() # 登录, 测试账号来登录 res = req.json( 'post', # 也可以读取Excel测试数据 config.read('http', 'base_url') + config.read('http', 'login_url'), json={ "mobile_phone": config.read('accounts', 'mobile_phone'), "pwd": config.read('accounts', 'password') }, headers=eval(config.read('http', 'headers'))) data = { "token": res['data']['token_info']['token'], "member_id": res['data']['id'] } return data
class Context: """保存临时替换的数据""" phone = config.read('accounts', 'mobile_phone') pwd = config.read('accounts', 'password') member_id = config.read('accounts', 'member_id') @property def loan_id(self): # Context().loan_id """查询数据库,获取最新的 loan.id 作为 Context的 loan_id 属性。""" db = MyDBHandler() loan = db.query("SELECT * FROM loan ORDER BY id DESC;", one=True) return str(loan['id']) @property def above_balance(self): db = MyDBHandler() user = db.query('SELECT * FROM member WHERE id=%s;', args=[self.member_id]) return str(user['leave_amount'] + 1)
def __init__(self, **kw): # 获取的数据默认是元组 super().__init__( host=config.read('db', 'host'), port=eval(config.read('db', 'port')), # int类型去掉两端字符串 user=config.read('db', 'user'), password=config.read('db', 'password'), charset=config.read('db', 'charset'), database=config.read('db', 'database'), cursorclass=DictCursor, # 默认是元组 **kw)
class TestRecharge(unittest.TestCase): # 通过读取配置文件得到 cases.xlsx file_name = config.read('excel', 'file_name') # 拼接配置文件的路径和名称 file_path = os.path.join(p_path.DATA_PATH, file_name) # Excel 表格名称 sheet_name = config.read('excel', 'recharge_sheet') # url 地址 url = config.read('http', 'base_url') # 读取 headers 信息 获取 请求头 # TODO cls.headers['Authorization'] = 'Bearer ' + cls.user_info['token'] # TypeError: 'str' object does not support item assignment headers = eval(config.read('http', 'headers')) # 读取 excel 数据 test_data = ExcelHandler(file_path).read(sheet_name) # 写入,写入 excel_headers = ExcelHandler(file_path).headers(sheet_name) # TODO 写入断言结果的表格 result_index = excel_headers.index('result') # 测试类方法,每一个测试类之前运行一次 @classmethod def setUpClass(cls): # HTTP请求 cls.req = RequestsHandler() # 登录 提前准备好的测试账号来登录 # 1、先登录获取token 用户id member_id cls.user_info = login() # 2、组装、Authorization 请求头是v2必传需要拼接是由:Bearer + '空格' + token cls.headers['Authorization'] = 'Bearer ' + cls.user_info['token'] @classmethod def tearDownClass(cls): pass def setUp(self): # 每个用例执行一次 self.db = MyDBHandler() def tearDown(self): # 每个之后运行,关闭游标和数据连接 self.db.close() @data(*test_data) def test_recharge(self, test_info): # test_info里面是测试文档里的数据 if '#member_id#' in test_info[ 'data']: # TypeError: 参数二必须是 str 类型,不能是 int 类型 test_info['data'] = test_info['data'].replace( '#member_id#', str(self.user_info['member_id'])) if '*wrong_member_id*' in test_info['data']: # 只要用户id不是他自己就可以了,后面加一 test_info['data'] = test_info['data'].replace( '*wrong_member_id*', str(self.user_info['member_id'] + 1)) # 3、发送请求 之前需要把表格里的用户member_id进行替换,表格里是“member”占位 # 5、查询数据库,对比余额, # 投资之前金额 user_info = self.db.query('select * from member where id = %s;', args=[self.user_info['member_id']]) amount = user_info['leave_amount'] # 查询数据库获取用户金额 res = self.req.json( test_info['method'], # 请求方式 self.url + test_info['url'], # url地址 # 测试数据 获取json的默认是字典格式,所以要进行转换 json=eval(test_info['data']), # 读取 headers 信息 标题头 是字符串,需要转化成字典 headers=self.headers) # 4、替换member_id self.assertEqual(test_info['expected'], res['code']) # 4.1、再次查询数据库,对比两个数据库查到的结果相减 == 投资金额 # 投资之后金额 user_info_after = self.db.query('select * from member where id = %s;', args=[self.user_info['member_id']]) amount_after = user_info_after['leave_amount'] # 查询数据库获取用户金额 # 5、查询数据库,对比余额, if res['code'] == 0: print(amount, amount_after) self.assertEqual(amount_after - amount, json.loads(test_info['data'])['amount'])
class TestInvest(unittest.TestCase): # 读取配置文件 file_name = config.read('excel', 'file_name') file_path = os.path.join(p_path.DATA_PATH, file_name) # Excel 表格名称 sheet_name = config.read('excel', 'invest_sheet') # url 地址 url = config.read('http', 'base_url') # 读取 headers 信息 获取 请求头 headers = eval(config.read('http', 'headers')) # 读取 excel 数据 test_data = ExcelHandler(file_path).read(sheet_name) # 写入,写入 excel_headers = ExcelHandler(file_path).headers(sheet_name) # TODO 写入断言结果的表格 result_index = excel_headers.index('result') @classmethod def setUpClass(cls): cls.req = RequestsHandler() # 登录, 测试账号来登录 cls.user_info = login() cls.headers['Authorization'] = 'Bearer ' + cls.user_info['token'] @classmethod def tearDownClass(cls): pass def setUp(self): # 每个测试用例执行前运行 self.db = MyDBHandler() def tearDown(self): # 每个测试用例之后运行 self.db.close() @data(*test_data) def test_invest(self, test_info): # 1 登录,拿到 token_id, 作为接下来的接口请求头信息 # 2,创建标 # 3, 审核标 # 4, 投资 # 余额是否相等 test_info['data'] = replace_label(test_info['data']) print(test_info['data']) user_info = self.db.query('SELECT * FROM member WHERE id=%s;', args=[self.user_info['member_id']]) before_money = user_info['leave_amount'] res = self.req.json(test_info['method'], self.url + test_info['url'], json=json.loads((test_info['data'])), headers=self.headers) # 替换 member_id print(res) self.assertEqual(test_info['expected'], res['code']) if res['code'] == 0 and test_info['url'] == '/member/invest': user_info_after = self.db.query( 'SELECT * FROM member WHERE id=%s;', args=[self.user_info['member_id']]) after_money = user_info_after['leave_amount'] self.assertEqual( before_money - json.loads(test_info['data'])['amount'], after_money)