Exemplo n.º 1
0
 def recharge(self):
     headers = {
         "X-Lemonban-Media-Type": "lemonban.v2",
         "Authorization": self.token
     }
     data = {"member_id": self.member_id, "amount": "50000"}
     url = "".join([env_data.host, "/member/recharge"])
     request_handler.visit(url, method="POST", json=data, headers=headers)
     query_sql = "select leave_amount from member where id = {};".format(
         self.member_id)
     original_data = self.db.query(query_sql)
     original_amt = original_data['leave_amount']
     return original_amt
Exemplo n.º 2
0
 def add_loan(self):
     data = {
         "member_id":
         self.invest_member_id,
         "title":
         "烧烤投资{}".format(datetime.datetime.now().strftime("%y%m%d%H%M%S")),
         "amount":
         80000.00,
         "loan_rate":
         12.0,
         "loan_term":
         12,
         "loan_date_type":
         1,
         "bidding_days":
         10
     }
     res = request_handler.visit(url="".join([self.host, "/loan/add"]),
                                 method="POST",
                                 json=data,
                                 headers={
                                     "X-Lemonban-Media-Type": "lemonban.v2",
                                     "Authorization": self.invest_token
                                 })
     # 返回新增项目的loan_id
     return jsonpath(res, "$..id")[0]
Exemplo n.º 3
0
 def test_get_info(self, data_info):
     global TestResult
     logger.info('******************正在执行user_info模块用例******************')
     logger.info('正在执行第{}条用例:{}'.format(data_info['case_id'],
                                        data_info['title']))
     logger.info('请求的数据是{}'.format(data_info))
     # 拼接请求地址url
     url = "".join([env_data.host, data_info['url']])
     res = request_handler.visit(url,
                                 method=data_info['method'],
                                 headers=json.loads(data_info['headers']))
     try:
         for k, v in eval(data_info['expected']).items():
             self.assertTrue(v == res[k])
         TestResult = 'PASS'
         logger.info("第{}条用例执行结果为{}".format(data_info['case_id'],
                                            TestResult))
     except AssertionError as e:
         logger.error("执行接口测试出现错误,错误是{}".format(e))
         TestResult = 'FAILED'
         logger.error("第{}条用例执行结果为{}".format(data_info['case_id'],
                                             TestResult))
     finally:
         logger.info('*************开始回写数据***************')
         self.exl.write_excel(env_data.info_sheet, data_info['case_id'] + 1,
                              9, str(res))
         logger.info('正在回写实际结果actual:{}'.format(str(res)))
         self.exl.write_excel(env_data.info_sheet, data_info['case_id'] + 1,
                              10, TestResult)
         logger.info('正在回写测试结果test_result:{}'.format(TestResult))
         logger.info('*************结束回写数据***************')
Exemplo n.º 4
0
 def audit_loan(self):
     data = {"loan_id": self.loan_id, "approved_or_not": "true"}
     res = request_handler.visit(url="".join([self.host, "/loan/audit"]),
                                 method="PATCH",
                                 json=data,
                                 headers={
                                     "X-Lemonban-Media-Type": "lemonban.v2",
                                     "Authorization": self.admin_token
                                 })
     return jsonpath(data, "$.loan_id")[0]
Exemplo n.º 5
0
    def login(self, user):
        res = request_handler.visit(
            url="".join([ConfHandler.host, "/member/login"]),
            method="POST",
            json=user,
            headers={"X-Lemonban-Media-Type": "lemonban.v2"})
        # 取token的前缀
        token_type = jsonpath(res, "$..token_type")[0]
        # 取token后半部分
        token_str = jsonpath(res, "$..token")[0]
        # 拼接token
        token = " ".join([token_type, token_str])
        # 获取登录用户的id
        id = jsonpath(res, "$..id")[0]

        return {"token": token, "member_id": id}
Exemplo n.º 6
0
    def test_update_name(self, data_info):
        global TestResult

        # 登录获取的memberid替换掉excel的member_id
        if "#id#" in data_info["data"]:
            data_info["data"] = data_info["data"].replace("#id#", str(self.member_id))
        # 登录获取的token替换掉excel的token
        if "#token#" in data_info["headers"]:
            data_info["headers"] = data_info["headers"].replace("#token#", self.token)

        logger.info('******************正在执行update模块用例******************')
        logger.info('正在执行第{}条用例:{}'.format(data_info['case_id'], data_info['title']))
        logger.info('请求的数据是{}'.format(data_info))
        # 拼接请求地址url
        url = "".join([env_data.host, data_info['url']])
        res = request_handler.visit(url,
                                    method=data_info['method'],
                                    json=json.loads(data_info['data']),
                                    headers=json.loads(data_info['headers']))
        try:
            for k, v in eval(data_info['expected']).items():
                self.assertTrue(v == res[k])
            if res['code'] == 0:
                logger.info("======================正在查询修改后的用户昵称=======================")
                sql_1 = "select reg_name from member where id = {};".format(self.member_id)
                after_data = self.db.query(sql_1)
                after_name = after_data['reg_name']
                logger.info("账户id为{}的昵称修改后为{}".format(self.member_id, after_name))
                # 比对修改成功后昵称是否修改正确
                logger.info("======================正在昵称信息比对比对=======================")
                self.assertTrue(after_name == json.loads(data_info['data'])['reg_name'])
            TestResult = 'PASS'
            logger.info("第{}条用例执行结果为{}".format(data_info['case_id'], TestResult))
        except AssertionError as e:
            logger.error("执行接口测试出现错误,错误是{}".format(e))
            TestResult = 'FAILED'
            logger.error("第{}条用例执行结果为{}".format(data_info['case_id'], TestResult))
        finally:
            logger.info('*************开始回写数据***************')
            self.exl.write_excel(env_data.update_sheet, data_info['case_id'] + 1, 9, str(res))
            logger.info('正在回写实际结果actual:{}'.format(str(res)))
            self.exl.write_excel(env_data.update_sheet, data_info['case_id'] + 1, 10, TestResult)
            logger.info('正在回写测试结果test_result:{}'.format(TestResult))
            logger.info('*************结束回写数据***************')
Exemplo n.º 7
0
    def test_regist(self, data_info):
        global test_result
        # 替换动态手机号,用于注册成功用例
        phone = self.phone_no()
        if '#phone#' in data_info['data']:
            data_info['data'] = data_info['data'].replace('#phone#', phone)
        logger.info('******************正在执行register模块用例******************')
        logger.info('正在执行第{}条用例:{}'.format(data_info['case_id'], data_info['title']))
        logger.info('请求的数据是{}'.format(data_info))
        # 请求地址拼接
        url = "".join([env_data.host, data_info['url']])

        res = request_handler.visit(url,
                                    method=data_info['method'],
                                    json=eval(data_info['data']),
                                    headers=json.loads(data_info['headers']))
        try:
            # 预期结果与返回结果比对
            for k, v in eval(data_info['expected']).items():
                self.assertTrue(v == res[k])
            # 对注册成功的进行数据校验,判断数据是否已经落库
            logger.info("********************正在查询数据库********************")
            sql = 'select * from member where mobile_phone={}'.format(phone)
            query_res = self.db.query(sql)
            if res['code'] == 0:
                self.assertTrue(query_res)
            # 若断言成功,则测试结果为pass
            test_result = 'PASS'
            logger.info("第{}条用例执行结果为{}".format(data_info['case_id'], test_result))
        except AssertionError as e:
            logger.error("执行接口测试出现错误,错误是{}".format(e))
            # 若断言成功,则测试结果为failed
            test_result = 'FAILED'
            logger.error("第{}条用例执行结果为{}".format(data_info['case_id'], test_result))
        finally:
            logger.info('*************开始回写数据***************')
            # 实际结果进行回写
            self.exl.write_excel(env_data.regist_sheet, data_info['case_id'] + 1, 9, str(res))
            logger.info('正在回写实际结果actual:{}'.format(str(res)))
            # 测试结果进行回写
            self.exl.write_excel(env_data.regist_sheet, data_info['case_id'] + 1, 10, test_result)
            logger.info('正在回写测试结果test_result:{}'.format(test_result))
            logger.info('*************结束回写数据***************')
 def test_login(self, data_info):
     global TestResult
     logger.info('******************正在执行login模块用例******************')
     logger.info('正在执行第{}条用例:{}'.format(data_info['case_id'],
                                        data_info['title']))
     logger.info('请求的数据是{}'.format(data_info))
     # 请求地址拼接
     url = "".join([env_data.host, data_info['url']])
     res = request_handler.visit(url,
                                 method=data_info['method'],
                                 json=eval(data_info['data']),
                                 headers=eval(data_info['headers']))
     try:
         # 预期结果与返回结果比对
         for k, v in eval(data_info['expected']).items():
             self.assertTrue(v == res[k])
         # 若断言成功,则测试结果为pass
         TestResult = 'PASS'
         logger.info("第{}条用例执行结果为{}".format(data_info['case_id'],
                                            TestResult))
     except AssertionError as e:
         logger.info("执行接口测试出现错误,错误是{}".format(e))
         # 若断言成功,则测试结果为failed
         TestResult = 'FAILED'
         logger.info("第{}条用例执行结果为{}".format(data_info['case_id'],
                                            TestResult))
     finally:
         logger.info('*************开始回写数据***************')
         # 实际结果进行回写
         self.exl.write_excel(handler.ConfHandler.login_sheet,
                              data_info['case_id'] + 1, 9, str(res))
         logger.info('正在回写实际结果actual:{}'.format(str(res)))
         # 测试结果进行回写
         self.exl.write_excel(handler.ConfHandler.login_sheet,
                              data_info['case_id'] + 1, 10, TestResult)
         logger.info('正在回写测试结果test_result:{}'.format(TestResult))
         logger.info('*************结束回写数据***************')
Exemplo n.º 9
0
    def test_invest(self, data_info):
        global testresult, loan_id, original_amt
        sum_amt, sum_log = 0, 0
        logger.info('******************正在执行invest模块用例******************')
        logger.info('正在执行第{}条用例:{}'.format(data_info['case_id'],
                                           data_info['title']))
        logger.info('请求的数据是{}'.format(data_info))

        # 正则替换data中的member_id及loan_id
        data_info['data'] = env_data.replace_data(data_info['data'])

        # 非竞标中的项目 2-竞标中 3-还款中 4-还款完成 5-审核不通过
        if "#shz" in data_info["data"]:  # 竞标中
            query_1 = "select id from loan where status = 1 limit 1"
            result = self.db.query(query_1)
            loan_id = result["id"]
            data_info["data"] = data_info["data"].replace("#shz", str(loan_id))
        elif "#hkz" in data_info["data"]:  # 还款中
            query_2 = "select id from loan where status = 3 limit 1"
            result = self.db.query(query_2)
            loan_id = result["id"]
            data_info["data"] = data_info["data"].replace("#hkz", str(loan_id))
        elif "#hwc" in data_info["data"]:  # 还款完成
            query_3 = "select id from loan where status = 4 limit 1"
            result = self.db.query(query_3)
            loan_id = result["id"]
            data_info["data"] = data_info["data"].replace("#hwc", str(loan_id))
        elif "#btg" in data_info["data"]:  # 审核不通过
            query_4 = "select id from loan where status = 5 limit 1"
            result = self.db.query(query_4)
            loan_id = result["id"]
            data_info["data"] = data_info["data"].replace("#btg", str(loan_id))
        # 输入其他用户创建的项目id
        elif "#other_id" in data_info["data"]:
            query_5 = "select id from loan where status = 2 and member_id !={} and (amount between 600 and 20000) limit 1".format(
                self.member_id)
            result = self.db.query(query_5)
            loan_id = result["id"]
            data_info["data"] = data_info["data"].replace(
                "#other_id", str(loan_id))
        # 投资金额输入大于可投标金额
        elif "#max_id" in data_info["data"]:
            query_6 = "select id,amount from loan where status = 2 and (amount between 200 and 20000) limit 1"
            result = self.db.query(query_6)
            max_id = result["id"]
            data_info["data"] = data_info["data"].replace(
                "#max_id", str(max_id))
            if "#pay_amt#" in data_info["expected"]:
                query_7 = "select SUM(amount) sumamt from invest where loan_id={}".format(
                    max_id)
                sum_amount = self.db.query(query_7)
                amt = sum_amount["sumamt"]
                data_info["expected"] = data_info["expected"].replace(
                    "#pay_amt#", str(float(result["amount"] - amt)))
        # 投资金额输入等于可投标金额,投标完成
        elif "#amount" in data_info["data"]:
            query_8 = "select amount from loan where id = {}".format(
                json.loads(data_info['data'])['loan_id'])
            amount = self.db.query(query_8)
            eq_amount = amount["amount"]
            data_info["data"] = data_info["data"].replace(
                "#amount", str(eq_amount))

        # 只有yes_or_no的标识为YES的才查询原始金额
        if data_info['yes_or_no'] == "YES":
            logger.info(
                "======================正在查询原始金额=======================")
            sql_1 = "select leave_amount from member where id = {};".format(
                self.member_id)
            original_data = self.db.query(sql_1)
            original_amt = original_data['leave_amount']

            # 判断账号是否余额充足,低于80000,则调充值接口,保证正常投资操作
            if original_amt < Decimal('80000'):
                original_amt = self.recharge()
                logger.info("账户id为{}的账户原始金额是{}".format(self.member_id,
                                                       original_amt))
                sum_amt = original_amt - Decimal(
                    json.loads(data_info['data'])['amount']) - sum_amt
            else:
                logger.info("账户id为{}的账户原始金额是{}".format(self.member_id,
                                                       original_amt))
                sum_amt = original_amt - Decimal(
                    json.loads(data_info['data'])['amount']) - sum_amt
        # 投资金额输入大于用户余额,余额不足
        if "#min_amt" in data_info["data"]:
            data_info["data"] = data_info["data"].replace(
                "#min_amt", str((original_amt + 10000)))

        logger.info(
            "======================正在查询原始投资流水记录条数=======================")
        sql_1 = "select count(*) record_num from financelog where pay_member_id = {};".format(
            self.member_id)
        original_data = self.db.query(sql_1)
        original_num1 = original_data['record_num']
        logger.info("账户id为{}的原始投资流水条数为{}".format(self.member_id,
                                                 original_num1))
        sum_log = original_num1 + 1 + sum_log

        # 正则替换headers的token
        data_info["headers"] = env_data.replace_data(data_info["headers"])

        # 请求地址url拼接
        url = "".join([env_data.host, data_info['url']])
        res = request_handler.visit(url,
                                    method=data_info['method'],
                                    json=json.loads(data_info['data']),
                                    headers=json.loads(data_info['headers']))
        try:
            # 对返回结果进行断言
            self.assertTrue(eval(data_info['expected'])["code"] == res["code"])
            self.assertTrue(eval(data_info['expected'])["msg"] == res["msg"])
            # 只有投资成功,code为0的才比对投资前后金额
            if res['code'] == 0:
                logger.info(
                    "======================正在查询投资后金额=======================")
                sql_amt = "select leave_amount from member where id = {}".format(
                    self.member_id)
                after_invest_data = self.db.query(sql_amt)
                after_invest_amt = after_invest_data['leave_amount']
                logger.info("账户id为{}的账户投资后的余额是{}".format(
                    self.member_id, after_invest_amt))
                logger.info("账户id为{}的账户原始金额-投资金额后的余额是{}".format(
                    self.member_id, sum_amt))
                logger.info(
                    "======================正在金额比对=======================")
                self.assertTrue(after_invest_amt == sum_amt)
                logger.info(
                    "======================金额比对通过=======================")
                logger.info(
                    "======================正在查询投资后流水记录=======================")
                sql_log = "select count(*)  record_num from financelog where pay_member_id = {};".format(
                    self.member_id)
                after_data = self.db.query(sql_log)
                after_num = after_data['record_num']
                logger.info("账户id为{}的投资后流水条数为{}".format(
                    self.member_id, after_num))
                logger.info(
                    "======================正在投资流水记录比对=======================")
                self.assertTrue(after_num == sum_log)
                logger.info(
                    "======================正在查询投资记录=======================")
                sql_invest = "select * from invest where member_id = {} and loan_id = {};".format(
                    self.member_id,
                    json.loads(data_info["data"])["loan_id"])
                after_invest = self.db.query(sql_invest)
                self.assertTrue(after_invest)

                # 投标完成,状态更新为3(还款中),生成还款计划
                sql_status = "select status  from loan where id = {};".format(
                    json.loads(data_info["data"])["loan_id"])
                after_invest = self.db.query(sql_status)
                if after_invest["status"] == 3:
                    sql_repay = "SELECT * from repayment where invest_id in (SELECT invest_id from invest where member_id = {} and loan_id={})".format(
                        self.member_id,
                        json.loads(data_info["data"])["loan_id"])
                    self.assertTrue(sql_repay)
            testresult = 'PASS'
            logger.info("第{}条用例执行结果为{}".format(data_info['case_id'],
                                               testresult))
        except AssertionError as e:
            logger.error("执行接口测试出现错误,错误是{}".format(e))
            testresult = 'FAILED'
            logger.error("第{}条用例执行结果为{}".format(data_info['case_id'],
                                                testresult))
        finally:
            logger.info('*************开始回写数据***************')
            self.exl.write_excel(env_data.inv_sheet, data_info['case_id'] + 1,
                                 10, str(res))
            logger.info('正在回写实际结果actual:{}'.format(str(res)))
            self.exl.write_excel(env_data.inv_sheet, data_info['case_id'] + 1,
                                 11, testresult)
            logger.info('正在回写测试结果test_result:{}'.format(testresult))
            logger.info('*************结束回写数据***************')
Exemplo n.º 10
0
    def test_withdraw(self, data_info):
        global testresult
        sum_amt, sum_num = 0, 0

        # 登录获取的memberid替换掉excel的member_id
        if "#id#" in data_info["data"]:
            data_info["data"] = data_info["data"].replace(
                "#id#", str(self.member_id))

        # 登录获取的token替换掉excel的token
        if "#token#" in data_info["headers"]:
            data_info["headers"] = data_info["headers"].replace(
                "#token#", self.token)

        logger.info('******************正在执行withdraw模块用例******************')
        logger.info('正在执行第{}条用例:{}'.format(data_info['case_id'],
                                           data_info['title']))
        logger.info('请求的数据是{}'.format(data_info))

        # 提现金额大于账户余额
        if '#max_id#' in data_info['data']:
            sql = "select id,leave_amount from member where leave_amount between 1000 and 50000 limit 1"
            m_data = self.db.query(sql)
            draw_amt = m_data['leave_amount'] + 1000
            data_info['data'] = data_info['data'].replace(
                '#max_id#', str(m_data['id']))
            data_info['data'] = data_info['data'].replace(
                '#amt#', str(draw_amt))

        # 只有yes_or_no的标识为YES的才查询原始金额
        if data_info['yes_or_no'] == "YES":
            logger.info(
                "======================正在查询原始金额=======================")
            sql_1 = "select leave_amount from member where id = {};".format(
                self.member_id)
            original_data = self.db.query(sql_1)
            original_amt = original_data['leave_amount']

            # 判断账号是否余额充足,低于500000,则调充值接口,保证正常提现操作
            if original_amt < Decimal('500000'):
                before_amt = self.recharge()
                logger.info("账户id为{}的账户原始金额是{}".format(self.member_id,
                                                       before_amt))
                sum_amt = before_amt - Decimal(
                    json.loads(data_info['data'])['amount']) - sum_amt
            else:
                logger.info("账户id为{}的账户原始金额是{}".format(self.member_id,
                                                       original_amt))
                sum_amt = original_amt - Decimal(
                    json.loads(data_info['data'])['amount']) - sum_amt
            # 只有yes_or_no的标识为YES的才查询提现流水记录条数
            logger.info(
                "======================正在查询原始提现流水记录条数=======================")
            sql_2 = "select count(*) record_num from financelog where pay_member_id = {};".format(
                self.member_id)
            original_data = self.db.query(sql_2)
            original_num = original_data['record_num']
            logger.info("账户id为{}的原始提现流水条数为{}".format(self.member_id,
                                                     original_num))
            sum_num = original_num + 1 + sum_num
        url = "".join([env_data.host, data_info['url']])
        res = request_handler.visit(url,
                                    method=data_info['method'],
                                    json=json.loads(data_info['data']),
                                    headers=json.loads(data_info['headers']))
        try:
            for k, v in eval(data_info['expected']).items():
                self.assertTrue(v == res[k])
            # 只有充值成功,code为0的才比对充值前后金额
            if res['code'] == 0:
                logger.info(
                    "======================正在查询提现后金额=======================")
                sql_3 = "select leave_amount from member where id = {}".format(
                    self.member_id)
                after_withdraw_data = self.db.query(sql_3)
                after_withdraw_amt = after_withdraw_data['leave_amount']
                logger.info("账户id为{}的账户提现后的金额是{}".format(
                    self.member_id, after_withdraw_amt))
                logger.info("账户id为{}的账户原始金额-提现金额后的余额是{}".format(
                    self.member_id, sum_amt))
                logger.info(
                    "======================正在金额比对=======================")
                self.assertTrue(after_withdraw_amt == sum_amt)
                logger.info(
                    "======================金额比对通过=======================")
                logger.info(
                    "======================正在查询提现后流水记录=======================")
                sql_4 = "select count(*)  record_num from financelog where pay_member_id = {};".format(
                    self.member_id)
                after_data = self.db.query(sql_4)
                after_num = after_data['record_num']
                logger.info("账户id为{}的提现后流水条数为{}".format(
                    self.member_id, after_num))
                logger.info(
                    "======================正在提现流水比对=======================")
                self.assertTrue(after_num == sum_num)
            testresult = 'PASS'
            logger.info("第{}条用例执行结果为{}".format(data_info['case_id'],
                                               testresult))
        except AssertionError as e:
            logger.error("执行接口测试出现错误,错误是{}".format(e))
            testresult = 'FAILED'
            logger.error("第{}条用例执行结果为{}".format(data_info['case_id'],
                                                testresult))
        finally:
            logger.info('*************开始回写数据***************')
            self.exl.write_excel(env_data.withdraw_sheet,
                                 data_info['case_id'] + 1, 10, str(res))
            logger.info('正在回写实际结果actual:{}'.format(str(res)))
            self.exl.write_excel(env_data.withdraw_sheet,
                                 data_info['case_id'] + 1, 11, testresult)
            logger.info('正在回写测试结果test_result:{}'.format(testresult))
            logger.info('*************结束回写数据***************')
Exemplo n.º 11
0
    def test_add_loan(self, data_info):
        global testresult
        sum_num = 0
        # 登录获取的memberid替换掉excel的member_id
        if "#id#" in data_info["data"]:
            data_info["data"] = data_info["data"].replace(
                "#id#", str(self.member_id))

        # 动态获取title替换掉excel中项目名称
        title = "烧烤项目{}".format(datetime.datetime.now().strftime("%y%m%d%H%M"))
        if "#title#" in data_info["data"]:
            data_info["data"] = data_info["data"].replace("#title#", title)

        # 登录获取的token替换掉excel的token
        if "#token#" in data_info["headers"]:
            data_info["headers"] = data_info["headers"].replace(
                "#token#", self.token)

        # 只有yes_or_no的标识为YES的才查询member_id的项目个数
        if data_info['yes_or_no'] == "YES":
            logger.info("================正在查询member_id下原项目个数===============")
            sql_1 = "select count(*) record_num from loan where member_id = {};".format(
                self.member_id)
            original_num = self.db.query(sql_1)
            logger.info("账户id为{}的原项目个数为{}".format(self.member_id,
                                                  original_num))
            # 预期充值成功后流水记录增加一条
            sum_num = original_num['record_num'] + 1 + sum_num

        logger.info('******************正在执行add_loan模块用例******************')
        logger.info('正在执行第{}条用例:{}'.format(data_info['case_id'],
                                           data_info['title']))
        logger.info('请求的数据是{}'.format(data_info))

        # 拼接请求地址url
        url = "".join([env_data.host, data_info['url']])
        res = request_handler.visit(url,
                                    method=data_info['method'],
                                    json=json.loads(data_info['data']),
                                    headers=json.loads(data_info['headers']))
        try:
            # 预期结果与返回结果比对
            for k, v in eval(data_info['expected']).items():
                self.assertTrue(v == res[k])
            # 只有充值成功,code为0的才比对充值前后金额
            if res['code'] == 0:
                # 对新增项目成功的进行数据校验,判断数据是否已经落库
                logger.info("********************正在查询数据库********************")

                # 只有新增成功,code为0的才比对member_id下的项目个数
                logger.info(
                    "==============正在查询新增成功后member_id下的项目个数============")
                sql_2 = "select count(*)  record_num from loan where member_id = {};".format(
                    self.member_id)
                after_num = self.db.query(sql_2)
                logger.info("账户id为{}新增项目成功后的项目个数为{}".format(
                    self.member_id, after_num))
                logger.info(
                    "======================正在项目个数比对=======================")
                self.assertTrue(after_num['record_num'] == sum_num)
            # 测试通过后,结果标记为pass
            testresult = 'PASS'
            logger.info("第{}条用例执行结果为{}".format(data_info['case_id'],
                                               testresult))
        except AssertionError as e:
            logger.error("执行接口测试出现错误,错误是{}".format(e))
            # 测试失败,结果标记为failed
            testresult = 'FAILED'
            logger.error("第{}条用例执行结果为{}".format(data_info['case_id'],
                                                testresult))
        finally:
            logger.info('*************开始回写数据***************')
            # 实际结果进行回写
            self.exl.write_excel(env_data.add_sheet, data_info['case_id'] + 1,
                                 10, str(res))
            logger.info('正在回写实际结果actual:{}'.format(str(res)))
            # 测试结果进行回写
            self.exl.write_excel(env_data.add_sheet, data_info['case_id'] + 1,
                                 11, testresult)
            logger.info('正在回写测试结果test_result:{}'.format(testresult))
            logger.info('*************结束回写数据***************')
Exemplo n.º 12
0
    def test_recharge(self, data_info):
        global testresult
        sum_amt, sum_num = 0, 0

        # 登录获取的memberid替换掉excel的member_id
        if "#id#" in data_info["data"]:
            data_info["data"] = data_info["data"].replace(
                "#id#", str(self.member_id))

        # 登录获取的token替换掉excel的token
        if "#token#" in data_info["headers"]:
            data_info["headers"] = data_info["headers"].replace(
                "#token#", self.token)

        logger.info('******************正在执行recharge模块用例******************')
        logger.info('正在执行第{}条用例:{}'.format(data_info['case_id'],
                                           data_info['title']))
        logger.info('请求的数据是{}'.format(data_info))

        # 只有yes_or_no的标识为YES的才查询原始金额
        if data_info['yes_or_no'] == "YES":
            logger.info(
                "======================正在查询原始金额=======================")
            sql = "select leave_amount from member where id = {};".format(
                self.member_id)
            before_amt = self.db.query(sql)
            logger.info("账户id为{}的账户原始金额是{}".format(self.member_id, before_amt))
            # 若充值成功,预期余额
            sum_amt = before_amt['leave_amount'] + Decimal(
                json.loads(data_info['data'])['amount']) + sum_amt

            # 只有yes_or_no的标识为YES的才查询充值流水记录条数
            logger.info(
                "======================正在查询原始充值流水记录条数=======================")
            sql_1 = "select count(*) record_num from financelog where income_member_id = {};".format(
                self.member_id)
            original_num = self.db.query(sql_1)
            logger.info("账户id为{}的原始充值流水条数为{}".format(self.member_id,
                                                     original_num))
            # 预期充值成功后流水记录增加一条
            sum_num = original_num['record_num'] + 1 + sum_num

        # 拼接请求地址url
        url = "".join([env_data.host, data_info['url']])
        res = request_handler.visit(url,
                                    method=data_info['method'],
                                    json=json.loads(data_info['data']),
                                    headers=json.loads(data_info['headers']))
        try:
            # 预期结果与返回结果比对
            for k, v in eval(data_info['expected']).items():
                self.assertTrue(v == res[k])
            # 只有充值成功,code为0的才比对充值前后金额
            if res['code'] == 0:
                logger.info(
                    "======================正在查询充值后金额=======================")
                sql_2 = "select leave_amount from member where id = {}".format(
                    self.member_id)
                after_amt = self.db.query(sql_2)
                logger.info("账户id为{}的账户充值后的金额是{}".format(
                    self.member_id, after_amt))
                logger.info("账户id为{}的账户原始金额与充值金额求和的金额是{}".format(
                    self.member_id, sum_amt))
                logger.info(
                    "======================正在金额比对=======================")
                self.assertTrue(after_amt['leave_amount'] == sum_amt)
                # 只有充值成功,code为0的才比对充值流水记录
                logger.info(
                    "======================正在查询充值后流水记录=======================")
                sql_no = "select count(*)  record_num from financelog where income_member_id = {};".format(
                    self.member_id)
                after_num = self.db.query(sql_no)
                logger.info("账户id为{}的充值后流水条数为{}".format(
                    self.member_id, after_num))
                logger.info(
                    "======================正在充值流水比对=======================")
                self.assertTrue(after_num['record_num'] == sum_num)
            # 测试通过后,结果标记为pass
            testresult = 'PASS'
            logger.info("第{}条用例执行结果为{}".format(data_info['case_id'],
                                               testresult))
        except AssertionError as e:
            logger.error("执行接口测试出现错误,错误是{}".format(e))
            # 测试失败,结果标记为failed
            testresult = 'FAILED'
            logger.error("第{}条用例执行结果为{}".format(data_info['case_id'],
                                                testresult))
        finally:
            logger.info('*************开始回写数据***************')
            # 实际结果进行回写
            self.exl.write_excel(env_data.recharge_sheet,
                                 data_info['case_id'] + 1, 10, str(res))
            logger.info('正在回写实际结果actual:{}'.format(str(res)))
            # 测试结果进行回写
            self.exl.write_excel(env_data.recharge_sheet,
                                 data_info['case_id'] + 1, 11, testresult)
            logger.info('正在回写测试结果test_result:{}'.format(testresult))
            logger.info('*************结束回写数据***************')
    def test_audit(self, data_info):
        global testresult, loan_id
        logger.info('******************正在执行audit模块用例******************')
        logger.info('正在执行第{}条用例:{}'.format(data_info['case_id'],
                                           data_info['title']))
        logger.info('请求的数据是{}'.format(data_info))
        # 审核中的项目
        if "#id#" in data_info["data"]:
            data_info["data"] = data_info["data"].replace(
                "#id#", str(self.loan_id))
            query = "select status from loan where id = {}".format(
                self.loan_id)
            loan_id = self.db.query(query)
            logger.info("项目id为{}审批前的状态为{}".format(self.loan_id,
                                                  loan_id["status"]))

        # 非审核中的项目 2-竞标中 3-还款中 4-还款完成 5-审核不通过
        if "#jbz#" in data_info["data"]:  # 竞标中
            query_1 = "select id,status from loan where status = 2 limit 1"
            result = self.db.query(query_1)
            loan_id = result["id"]
            data_info["data"] = data_info["data"].replace(
                "#jbz#", str(loan_id))
            logger.info("项目id为{}审批前的状态为{}".format(loan_id, result["status"]))
        elif "#hkz#" in data_info["data"]:  # 还款中
            query_2 = "select id,status from loan where status = 3 limit 1"
            result = self.db.query(query_2)
            loan_id = result["id"]
            data_info["data"] = data_info["data"].replace(
                "#hkz#", str(loan_id))
            logger.info("项目id为{}审批前的状态为{}".format(loan_id, result["status"]))
        elif "#hwc#" in data_info["data"]:  # 还款完成
            query_3 = "select id,status from loan where status = 4 limit 1"
            result = self.db.query(query_3)
            loan_id = result["id"]
            data_info["data"] = data_info["data"].replace(
                "#hwc#", str(loan_id))
            logger.info("项目id为{}审批前的状态为{}".format(loan_id, result["status"]))
        elif "#btg#" in data_info["data"]:  # 审核不通过
            query_4 = "select id,status from loan where status = 5 limit 1"
            result = self.db.query(query_4)
            loan_id = result["id"]
            data_info["data"] = data_info["data"].replace(
                "#btg#", str(loan_id))
            logger.info("项目id为{}审批前的状态为{}".format(loan_id, result["status"]))

        # 替换headers里面的token为admin token
        if "#adtoken#" in data_info["headers"]:
            data_info["headers"] = data_info["headers"].replace(
                "#adtoken#", self.admin_token)
        # 请求地址url拼接
        url = "".join([env_data.host, data_info['url']])
        res = request_handler.visit(url,
                                    method=data_info['method'],
                                    json=json.loads(data_info['data']),
                                    headers=json.loads(data_info['headers']))
        try:
            # 对返回结果进行断言
            self.assertTrue(eval(data_info['expected'])["code"] == res["code"])
            self.assertTrue(eval(data_info['expected'])["msg"] == res["msg"])
            # 校验项目状态
            if data_info['yes_or_no'] == "YES":
                logger.info(
                    "======================正在查询项目状态=======================")
                if res["code"] in (0, 1):
                    sql = "select status from loan where id = {}".format(
                        self.loan_id)
                    after_status = self.db.query(sql)
                    logger.info("项目id为{}审批后的状态为{}".format(
                        self.loan_id, after_status["status"]))
                    logger.info(
                        "======================正在状态比对=======================")
                    self.assertTrue(after_status["status"] == eval(
                        data_info['expected'])["status"])
                elif res["code"] == 2:
                    sql = "select status from loan where id = {}".format(
                        loan_id)
                    after_status = self.db.query(sql)
                    logger.info("项目id为{}审批后的状态为{}".format(
                        loan_id, after_status["status"]))
                    logger.info(
                        "======================正在状态比对=======================")
                    self.assertTrue(after_status["status"] == json.loads(
                        data_info['expected'])["status"])
            testresult = 'PASS'
            logger.info("第{}条用例执行结果为{}".format(data_info['case_id'],
                                               testresult))
        except AssertionError as e:
            logger.error("执行接口测试出现错误,错误是{}".format(e))
            testresult = 'FAILED'
            logger.error("第{}条用例执行结果为{}".format(data_info['case_id'],
                                                testresult))
        finally:
            logger.info('*************开始回写数据***************')
            self.exl.write_excel(env_data.audit_sheet,
                                 data_info['case_id'] + 1, 10, str(res))
            logger.info('正在回写实际结果actual:{}'.format(str(res)))
            self.exl.write_excel(env_data.audit_sheet,
                                 data_info['case_id'] + 1, 11, testresult)
            logger.info('正在回写测试结果test_result:{}'.format(testresult))
            logger.info('*************结束回写数据***************')