Example #1
0
    def test_invest(self, case):
        expected = case.expected
        # 取出data并进行参数化
        case_data = HandleParams.replace_params(case.data)
        # url
        url = do_yaml.read_yaml('api', 'url') + case.url

        # 发起请求
        res = self.do_requests.send(url=url,
                                    method=case.request_method,
                                    data=case_data)
        # 结果转为字典
        result = res.json()
        msg = case.title
        success_msg = do_yaml.read_yaml('msg', 'success_result')
        fail_msg = do_yaml.read_yaml('msg', 'fail_result')
        # 获取行号
        row = case.case_id + 1

        try:
            # assertEqual第三个参数为用例执行失败之后的提示信息
            self.assertEqual(expected, result['code'], msg=msg)
        except AssertionError as e:
            # 输出日志
            do_logs.info("{}用例执行有误".format(msg))
            # 写入用例执行结果
            self.excel.write_data(row=row,
                                  column=do_yaml.read_yaml(
                                      'excel', 'result_col'),
                                  value=fail_msg)
            do_logs.error('具体异常为{}'.format(e))
            raise e
        else:
            do_logs.info('{}用例执行通过'.format(msg))
            # 写入用例执行结果
            self.excel.write_data(row=row,
                                  column=do_yaml.read_yaml(
                                      'excel', 'result_col'),
                                  value=success_msg)
            # 执行用例通过后,判断返回报文中有没有token,有的话取出加到请求头中
            if 'token_info' in res.text:
                token = result['data']['token_info']['token']
                self.do_requests.add_headers(
                    {"Authorization": "Bearer " + token})
            # 加标成功之后取出loan_id,如果用例中有check_sql,则要进行数据校验
            check_sql = case.check_sql
            if check_sql:
                check_sql = HandleParams.replace_params(check_sql)
                loan_id = self.do_mysql.run_sql(check_sql)
                # 创建动态属性的方式来处理接口依赖
                setattr(HandleParams, 'loan_id', loan_id['id'])

        finally:
            # 将实际结果写入
            self.excel.write_data(row=row,
                                  column=do_yaml.read_yaml(
                                      'excel', 'actual_col'),
                                  value=res.text)
Example #2
0
    def test_register(self, case):
        expected = case.expected
        # 取出data并进行参数化
        case_data = HandleParams.replace_params(case.data)
        # url
        url = do_yaml.read_yaml('api', 'url') + case.url

        # 发起请求
        res = self.do_requests.send(url=url,
                                    method=case.request_method,
                                    data=case_data)
        # 结果转为字典
        result = res.json()
        msg = case.title
        success_msg = do_yaml.read_yaml('msg', 'success_result')
        fail_msg = do_yaml.read_yaml('msg', 'fail_result')
        # 获取行号
        row = case.case_id + 1

        try:
            # assertEqual第三个参数为用例执行失败之后的提示信息
            self.assertEqual(expected, result['code'], msg=msg)
        except AssertionError as e:
            # 输出日志
            do_logs.info("{}用例执行有误".format(msg))
            # 写入用例执行结果
            self.excel.write_data(row=row,
                                  column=do_yaml.read_yaml(
                                      'excel', 'result_col'),
                                  value=fail_msg)
            do_logs.error('具体异常为{}'.format(e))
            raise e
        else:
            do_logs.info('{}用例执行通过'.format(msg))
            # 写入用例执行结果
            self.excel.write_data(row=row,
                                  column=do_yaml.read_yaml(
                                      'excel', 'result_col'),
                                  value=success_msg)
        finally:
            # 将实际结果写入
            self.excel.write_data(row=row,
                                  column=do_yaml.read_yaml(
                                      'excel', 'actual_col'),
                                  value=res.text)
Example #3
0
    def test_recharge(self, case):
        expected = case.expected
        # 取出data并进行参数化
        case_data = HandleParams.replace_params(case.data)
        # url
        url = do_yaml.read_yaml('api', 'url') + case.url

        # 充值前,去数据库获取充值前金额
        check_sql = case.check_sql
        if check_sql:
            check_sql = HandleParams.replace_params(check_sql)
            mysql_data = self.do_mysql.run_sql(check_sql)
            old_amount = round(float(mysql_data['leave_amount']),
                               2)  # 取出当前金额,保留两位小数

        # 发起请求
        res = self.do_requests.send(url=url,
                                    method=case.request_method,
                                    data=case_data)
        # 结果转为字典
        result = res.json()
        msg = case.title
        success_msg = do_yaml.read_yaml('msg', 'success_result')
        fail_msg = do_yaml.read_yaml('msg', 'fail_result')
        # 获取行号
        row = case.case_id + 1

        try:
            # assertEqual第三个参数为用例执行失败之后的提示信息
            self.assertEqual(expected, result['code'], msg=msg)
            # 充值成功之后再去数据库取当前金额对比
            if check_sql:
                new_amount = round(
                    float(self.do_mysql.run_sql(check_sql)['leave_amount']), 2)
                recharge_amount = json.loads(case_data)['amount']
                actual_amount = round(new_amount - old_amount, 2)
                self.assertEqual(recharge_amount, actual_amount, msg=msg)
        except AssertionError as e:
            # 输出日志
            do_logs.info("{}用例执行有误".format(msg))
            # 写入用例执行结果
            self.excel.write_data(row=row,
                                  column=do_yaml.read_yaml(
                                      'excel', 'result_col'),
                                  value=fail_msg)
            do_logs.error('具体异常为{}'.format(e))
            raise e
        else:
            do_logs.info('{}用例执行通过'.format(msg))
            # 写入用例执行结果
            self.excel.write_data(row=row,
                                  column=do_yaml.read_yaml(
                                      'excel', 'result_col'),
                                  value=success_msg)
            # 执行用例通过后,判断返回报文中有没有token,有的话取出加到请求头中
            if 'token_info' in res.text:
                self.do_requests.add_headers({"Authorization": "Bearer " + \
                                                               result['data']['token_info']['token']})
        finally:
            # 将实际结果写入
            self.excel.write_data(row=row,
                                  column=do_yaml.read_yaml(
                                      'excel', 'actual_col'),
                                  value=res.text)