Exemple #1
0
    def test_recharge(self, item):
        case_id = item["case_id"]
        title = item["title"]
        url = cf.get_value(conf_env, "prefix_url") + item["url"]
        method = item["method"]
        expected = item["expected"]
        data = item["data"]

        new_data = Context.recharge_parameterization(data)  # 将数据中进行参数替换

        check_sql = item["check_sql"]
        if check_sql:
            new_sql = Context.recharge_parameterization(check_sql)
            result = self.handler_mysql.select(new_sql)
            amount_before = float(result["LeaveAmount"])
            amount_before = round(amount_before, 2)

        msg = "测试" + title
        success_msg = cf.get_value("case", "success")
        fail_msg = cf.get_value("case", "fail")

        ret = self.request.send_request(method, url, new_data)

        try:
            self.assertIn(str(expected), ret.text, msg)

            # 校验充值金额
            data_dict = json.loads(data)
            recharge_amount = data_dict.get("amount")

            if check_sql:
                result = self.handler_mysql.select(new_sql)
                amount_after = float(result["LeaveAmount"])
                amount_after = round(amount_after, 2)

                self.assertEqual(round(amount_after - amount_before, 2),
                                 recharge_amount, "数据库充值金额校验失败")

            do_excel.write_value(case_id + 1, cf.get_int("case", "actual_col"),
                                 ret.text)
            do_excel.write_value(case_id + 1, cf.get_int("case", "result_col"),
                                 success_msg)
            logger.debug("{}, 执行结果为:{}".format(msg, success_msg))
        except AssertionError as e:
            do_excel.write_value(case_id + 1, cf.get_int("case", "actual_col"),
                                 ret.text)
            do_excel.write_value(case_id + 1, cf.get_int("case", "result_col"),
                                 fail_msg)
            logger.error("{}, 执行结果为:{}, 异常结果为:{}".format(msg, fail_msg, e))
            raise e
Exemple #2
0
    def test_recharge(self, one_case):
        # data = one_case['data']           #需要进行参数化
        new_data = Context.recharge_parameterization(one_case['data'])  #需要实现
        new_url = do_config.get_value('api', 'prefix_url') + one_case['url']

        check_sql = one_case['check_sql']
        if check_sql:
            check_sql = Context.recharge_parameterization(check_sql)
            mysql_data = self.handle_mysql.to_run(
                check_sql)  #decimal.Decimal类型
            amount_before_recharge = float(mysql_data['LeaveAmount'])
            amount_before_recharge = round(amount_before_recharge, 2)
            dict_data = json.loads(new_data, encoding='utf-8')
            recharge_number = dict_data['amount']

        #向服务器发起请求
        res = self.do_request.to_request(method=one_case['method'],
                                         url=new_url,
                                         data=new_data)
        #期望值
        expect_result = one_case['expected']
        msg = "测试" + one_case['title']
        success_msg = do_config.get_value('msg', 'success_result')
        fail_msg = do_config.get_value('msg', 'fail_result')
        case_id = one_case['case_id']
        code = res.json().get('code')

        try:
            self.assertEqual(str(expect_result), code, msg=msg)
            # self.assertIn(str(expect_result), res.text, msg=msg)
            if check_sql:
                mysql_data = self.handle_mysql.to_run(
                    check_sql)  # decimal.Decimal类型
                amount_after_recharge = float(mysql_data['LeaveAmount'])
                amount_after_recharge = round(amount_after_recharge, 2)
                self.assertEqual(
                    recharge_number,
                    (amount_after_recharge - amount_before_recharge),
                    msg=msg)
            do_excel.write_result(case_id + 1, res.text, success_msg)
            do_logger.debug('{},执行结果为:{}'.format(msg, success_msg))
        except AssertionError as e:
            do_excel.write_result(case_id + 1, res.text, fail_msg)
            do_logger.error('{}, 执行结果为:{}具体异常为:{}'.format(msg, fail_msg, e))
            raise e