class TestRegister(unittest.TestCase): def setUp(self): self.db = DoMysql() sql = 'SELECT COUNT(*) count FROM user_db.t_user_info;' self.before_line_count = self.db.fetch_one(sql)['count'] @data(*cases) def test_register(self, case): my_logger.info('正在执行第{}条用例:{}'.format(case.case_id, case.title)) my_logger.info('-----------开始检查url地址-------------') url = ReadConfig().get('test_api', 'url_pre') + case.url my_logger.info('请求url:{}'.format(url)) # 因注册的用户名不能重复,做随机处理 last_user_id = random_string.create_str() setattr(Context, 'user_id', last_user_id) # 因测试验证码超时需要新的手机号验证 if case.title == '发送短信验证码用于超时': normal_mobile = mobile_phone.creat_mobile() setattr(Context, 'normal_mobile', normal_mobile) data = DoRegex.replace(case.data) data = json.loads(data) my_logger.info('-----------开始检查参数-------------') my_logger.info('注册请求参数:{0}'.format(data)) if case.title == '注册验证码超时': time.sleep(500)
class TestUserAuth(unittest.TestCase): def setUp(self): self.db = DoMysql() # 随机产生身份证号,放入上下文 ID_card_No = get_card_id() setattr(Context, 'cre_id', ID_card_No) #实名认证前查询实名认证表记录 auth_sql = 'SELECT COUNT(*) count FROM user_db.t_user_auth_info;' self.before_auth_count = self.db.fetch_one(auth_sql)['count'] @data(*cases) def test_user_auth(self, case): my_logger.info('正在执行第{}条用例:{}'.format(case.case_id, case.title)) my_logger.info('-----------开始检查url地址-------------') url = ReadConfig().get('test_api', 'url_pre') + case.url my_logger.info('请求url:{}'.format(url)) data = DoRegex().replace(case.data) data = json.loads(data) my_logger.info('-----------开始检查参数-------------') my_logger.info('请求参数:{}'.format(data)) res = WebserviceRequest(url).get_result(case.port_name, data) time.sleep(60) my_logger.info('请求结果:{}'.format(res)) try: self.assertEqual(case.expected, res['retInfo']) do_excel.bace_write_by_case_id('verifiedUserAuth', case.case_id, str(res), 'success') Test_result = 'pass' except AssertionError as e: my_logger.error('出错了,{}'.format(e)) do_excel.bace_write_by_case_id('verifiedUserAuth', case.case_id, str(res), 'fail') Test_result = 'fail' raise e except Exception as e: raise e my_logger.info('本条用例测试结果:{}'.format(Test_result)) # 如果发送短信验证码成功 if res['retCode'] == '0' and case.port_name == 'sendMCode': sql_code = 'SELECT Fverify_code FROM sms_db_20.t_mvcode_info_3 where Fmobile_no={0};'.format( getattr(Context, 'normal_mobile')) verify_code = DoMysql().fetch_one(sql_code)['Fverify_code'] if verify_code: # 如果查到验证码就放入上下文 setattr(Context, 'verify_code', verify_code) # 如果注册成功,查询uid放入上下文 if res['retCode'] == '0' and case.port_name == 'userRegister': sql_uid = "SELECT Fuid uid FROM user_db.t_user_info where Fuser_id='{0}' and Fip='{1}' ORDER BY Fcreate_time DESC;".format( getattr(Context, 'user_id'), Context.client_ip) uid = str(DoMysql().fetch_one(sql_uid)['uid']) if uid: # 如果查询到Fuid就放入上下文 setattr(Context, 'uid', uid)
class TestSendMCode(unittest.TestCase): def setUp(self): self.do_mysql = DoMysql() mobile = mobile_phone.creat_mobile() setattr(Context, 'mobile', mobile) @data(*cases) def test_send_Mcode(self, case): my_logger.info('正在执行第{}条用例:{}'.format(case.case_id, case.title)) my_logger.info('-----------开始检查url地址-------------') url = ReadConfig().get('test_api', 'url_pre') + case.url my_logger.info('请求url:{}'.format(url)) data = DoRegex().replace(case.data) data = json.loads(data) my_logger.info('-----------开始检查请求参数-------------') my_logger.info('请求参数:{}'.format(data)) try: resp = WebserviceRequest(url).get_result(case.port_name, data) time.sleep(60) my_logger.info('请求接口结果:{}'.format(resp)) if resp['retCode'] == '0': self.assertEqual(case.expected, resp['retInfo']) do_excel.bace_write_by_case_id('sendMcode', case.case_id, str(resp), 'success') Test_result = 'pass' # 数据验证 sql = 'SELECT * FROM sms_db_20.t_mvcode_info_3 where Fmobile_no={};'.format( getattr(Context, 'mobile')) seek_data = self.do_mysql.fetch_one(sql) actual = { "client_ip": seek_data['Fclient_ip'], "tmpl_id": str(seek_data['Ftmpl_id']), "mobile": str(seek_data['Fmobile_no']) } self.assertDictEqual(data, actual) my_logger.info('发送验证码成功,查询数据库,添加一条数据与请求参数相同') except Exception as e: # 异常测试用例,需要捕捉错误信息进行断言 self.assertIn(case.expected, str(e)) do_excel.bace_write_by_case_id('sendMcode', case.case_id, str(e.__dict__['fault'][1]), 'success') sql = 'SELECT * FROM sms_db_20.t_mvcode_info_3 where Fmobile_no={};'.format( getattr(Context, 'mobile')) seek_data = self.do_mysql.fetch_one(sql) expected = None self.assertEqual(expected, seek_data) my_logger.info('发送验证码成功,数据库查询无数据') Test_result = 'pass' except AssertionError as e: Test_result = 'fail' my_logger.error('出错了,{}'.format(e)) do_excel.bace_write_by_case_id('sendMcode', case.case_id, e, 'fail') raise e my_logger.info('本条用例测试结果:{}'.format(Test_result)) def tearDown(self): self.do_mysql.close() my_logger.info('关闭数据库连接,测试环境还原')