示例#1
0
    def test_info(self, data, admin_login, investor_login):
        # 动态设置类属性中需要的数据
        setattr(MidHandler, "admin_member_id", str(admin_login["id"]))
        setattr(MidHandler, "investor_member_id", str(investor_login["id"]))
        setattr(MidHandler, "admin_token", admin_login["authorization"])
        setattr(MidHandler, "investor_token", investor_login["authorization"])

        # 使用正则表达式替换数据
        data = MidHandler.replace_data(json.dumps(data))
        data = json.loads(data)

        url = MidHandler.conf_data["ENV"]["BASE_URL"] + data["url"]
        method = data["method"]
        header = json.loads(data["header"])
        expected = json.loads(data["expected"])

        response = request(method=method, url=url, headers=header)

        response_data = response.json()

        try:
            for key, value in expected.items():
                assert jsonpath(response_data, key)[0] == value
        except AssertionError as e:
            MidHandler.log.error(e)
            MidHandler.log.info(
                "\ncaseid: {}, title: {}\nurl: {}\nmethod: {}\nheader: {}\ncase_data: {}\nresponse: {}\nresult: {}\n"
                .format(data["case_id"], data["title"], url, method, header,
                        data, response_data, "Failed"))
            raise e
        else:
            MidHandler.log.info(
                "\ncaseid: {}, title: {}\nurl: {}\nmethod: {}\nheader: {}\ncase_data: {}\nresponse: {}\nresult: {}\n"
                .format(data["case_id"], data["title"], url, method, header,
                        data, response_data, "Passed"))
示例#2
0
    def test_invest(self, data, db):
        # 注意: 要保证替换成功必须保证excel中需要替换的字符串与保存数据类中的属性名一致
        data = MidHandler.replace_data(json.dumps(data))
        # 由于上面进行替换的时候,用例数据转换成字符串了,因此我们需要再转成字典格式
        data = json.loads(data)

        request_url = MidHandler.conf_data["ENV"]["BASE_URL"] + data["url"]
        request_method = data["method"]
        request_header = json.loads(data["header"])
        request_data = data["data"]

        if "用户余额大于投资金额" in data["title"]:
            amount = int(json.loads(request_data)["amount"]) + 1
            db.update("update member set leave_amount={}; ".format(amount))

        if "项目可投金额小于用户投资金额" in data["title"]:
            amount = int(json.loads(request_data)["amount"]) - 1
            db.update("update loan set amount={}; ".format(int(amount)))

        # 投资非竞标中的项目,修改项目的状态
        if '审核中1' in data['title']:
            sql = 'update loan set status=1 where id={}'.format(getattr(MidHandler, 'loan_id'))
            db.update(sql)
        elif '审核不通过5' in data['title']:
            sql = 'update loan set status=5 where id={}'.format(getattr(MidHandler, 'loan_id'))
            db.update(sql)
        elif '还款中3' in data['title']:
            sql = 'update loan set status=3 where id={}'.format(getattr(MidHandler, 'loan_id'))
            db.update(sql)
        elif '还款完成4' in data['title']:
            sql = 'update loan set status=4 where id={}'.format(getattr(MidHandler, 'loan_id'))
            db.update(sql)

        response = request(url=request_url, method=request_method, headers=request_header,
                           json=json.loads(request_data))
        response_data = response.json()

        if data["extractor"]:
            extractors = json.loads(data["extractor"])
            for name, expression in extractors.items():
                value = jsonpath(response_data, expression)[0]
                setattr(MidHandler, name, value)

        expected = json.loads(data["expected"])

        try:
            assert expected["code"] == response_data["code"]
        except AssertionError as e:
            MidHandler.log.error(e)
            MidHandler.log.info(
                "\ncaseid: {}, title: {}\nurl: {}\nmethod: {}\nheader: {}\ncase_data: {}\nresponse: {}\nresult: {}\n".format(
                    data["case_id"], data["title"], request_url, request_method, request_header, request_data,
                    response_data, "Failed"))
            raise e
        else:
            MidHandler.log.info(
                "\ncaseid: {}, title: {}\nurl: {}\nmethod: {}\nheader: {}\ncase_data: {}\nresponse: {}\nresult: {}\n".format(
                    data["case_id"], data["title"], request_url, request_method, request_header, request_data,
                    response_data, "Passed"))
示例#3
0
def db():
    """
    管理数据库连接的fixture
    好处是不用每次使用的时候都建立一次连接,并且关闭数据库。
    """
    db_conn = MidHandler.db_class()
    yield db_conn
    db_conn.close()
    def test_register(self, data):
        # 获取测试数据
        request_url = MidHandler.conf_data["ENV"]["BASE_URL"] + data["url"]
        request_method = data["method"]
        case = data["data"]
        request_headers = json.loads(data["header"])
        expected = json.loads(data["expected"])

        # 生成一个未注册过的手机号码
        phone = MidHandler.generate_new_phone()

        if "#new_phone#" in case:
            case = case.replace("#new_phone#", phone)
示例#5
0
    def test_audit(self, data, admin_login, add_loan, loan_login, db):
        # 动态设置类属性中需要的数据
        setattr(MidHandler, "admin_token", admin_login["authorization"])
        setattr(MidHandler, "loan_token", loan_login["authorization"])
        setattr(MidHandler, "loan_id", str(add_loan["id"]))

        # 使用正则表达式替换数据
        data = MidHandler.replace_data(json.dumps(data))
        data = json.loads(data)

        url = MidHandler.conf_data["ENV"]["BASE_URL"] + data["url"]
        method = data["method"]
        header = json.loads(data["header"])

        case = data["data"]
        expected = json.loads(data["expected"])

        # 修改项目的状态,不符合要求的项目状态,无法审核成功
        if '项目状态为2' in data['title']:
            sql = 'update loan set status=2 where id={}'.format(getattr(MidHandler, "loan_id"))
            db.update(sql)
        elif '项目状态为5' in data['title']:
            sql = 'update loan set status=5 where id={}'.format(getattr(MidHandler, "loan_id"))
            db.update(sql)
        elif '项目状态为3' in data['title']:
            sql = 'update loan set status=3 where id={}'.format(getattr(MidHandler, "loan_id"))
            db.update(sql)
        elif '项目状态为4' in data['title']:
            sql = 'update loan set status=4 where id={}'.format(getattr(MidHandler, "loan_id"))
            db.update(sql)

        response = request(url=url, method=method, json=json.loads(case), headers=header)
        response_data = response.json()

        try:
            for key, value in expected.items():
                assert jsonpath(response_data, key)[0] == value
        except AssertionError as e:
            MidHandler.log.error(e)
            MidHandler.log.info(
                "\ncaseid: {}, title: {}\nurl: {}\nmethod: {}\nheader: {}\ncase_data: {}\nresponse: {}\nresult: {}\n".format(
                    data["case_id"], data["title"], url, method, header, data,
                    response_data, "Failed"))
            raise e
        else:
            MidHandler.log.info(
                "\ncaseid: {}, title: {}\nurl: {}\nmethod: {}\nheader: {}\ncase_data: {}\nresponse: {}\nresult: {}\n".format(
                    data["case_id"], data["title"], url, method, header, data,
                    response_data, "Passed"))
    def test_login(self, data):
        data = MidHandler.replace_data(json.dumps(data))
        data = json.loads(data)
        url = MidHandler.conf_data["ENV"]["BASE_URL"] + data["url"]
        method = data["method"]
        headers = MidHandler.conf_data["ENV"]["HEADER"]
        request_data = data["data"]
        expected = json.loads(data["expected"])

        # -----  使用了正则表达式替换用例数据后,以下代码都可以省略了--------------
        # if "#new_phone#" in request_data:
        #     request_data = request_data.replace("#new_phone#", MidHandler.generate_new_phone())
        #
        # if "#admin_phone#" in request_data:
        #     request_data = request_data.replace("#admin_phone#", MidHandler.security_data["admin_phone"])
        #
        # if "#admin_pwd#" in request_data:
        #     request_data = request_data.replace("#admin_pwd#", MidHandler.security_data["admin_pwd"])
        #
        # if "#investor_phone#" in request_data:
        #     request_data = request_data.replace("#investor_phone#", MidHandler.security_data["investor_phone"])
        #
        # if "#investor_pwd#" in request_data:
        #     request_data = request_data.replace("#investor_pwd#", MidHandler.security_data["investor_pwd"])
        # -----  使用了正则表达式替换用例数据后,以下代码都可以省略了--------------

        response = request(url=url,
                           method=method,
                           headers=headers,
                           json=json.loads(request_data))
        actual = response.json()

        try:
            for key, value in expected.items():
                assert actual[key] == value
        except AssertionError as e:
            test_result = "Failed"
            MidHandler.log.error(e)
            MidHandler.log.info(
                "\ncaseid: {}, title: {}\nurl: {}\nmethod: {}\nheader: {}\ncase_data: {}\nresponse: {}\nresult: {}\n"
                .format(data["case_id"], data["title"], url, method, headers,
                        request_data, response, test_result))
            raise e
        else:
            test_result = "Passed"
            MidHandler.log.info(
                "\ncaseid: {}, title: {}\nurl: {}\nmethod: {}\nheader: {}\ncase_data: {}\nresponse: {}\nresult: {}\n"
                .format(data["case_id"], data["title"], url, method, headers,
                        request_data, response, test_result))
示例#7
0
import requests
import pytest
from middleware.handler import MidHandler

# 准备数据
data = MidHandler.update_tests_data()


@pytest.mark.parametrize('info', data)
def test_login(info):
    actual_method = info['method']
    actual_file = eval(info['file'])
    actual_data = eval(info['paras'])
    resp = requests.request(
        url=MidHandler.yaml_config['turing']['url'],
        method=actual_method,
        files=actual_file,
        data=actual_data,
    )

    print("\n  json responds is :", resp.text, "\n")
    return resp.json()
    def test_withdraw(self, data, investor_login, db):
        # 动态替换用例数据
        setattr(MidHandler, "investor_token", investor_login["authorization"])
        setattr(MidHandler, "investor_member_id", investor_login["id"])
        data = MidHandler.replace_data(json.dumps(data))
        data = json.loads(data)

        request_url = MidHandler.conf_data["ENV"]["BASE_URL"] + data["url"]
        request_method = data["method"]
        request_header = json.loads(data["header"])
        request_data = data["data"]

        if "小于用户可用余额, 提现成功" in data["title"]:
            amount = json.loads(request_data)["amount"] + 100
            db.update("update member set leave_amount={} where id={};".format(
                amount, getattr(MidHandler, "investor_member_id")))

        if " 等于用户可用余额,提现成功" in data["title"]:
            db.update("update member set leave_amount={} where id={};".format(
                json.loads(request_data)["amount"],
                getattr(MidHandler, "investor_member_id")))

        leave_amount = \
        db.query_one("select leave_amount from member where id={};".format(getattr(MidHandler, "investor_member_id")))[
            "leave_amount"].quantize(Decimal('0.00'))

        response = request(url=request_url,
                           method=request_method,
                           headers=request_header,
                           json=json.loads(request_data))
        response_data = response.json()

        # leave_amount_after = db.query_one("select leave_amount from member where id={};".format(member_id))["leave_amount"]
        if "提现成功" in data["title"]:
            leave_amount_after = Decimal(
                jsonpath(response_data,
                         "$.data.leave_amount")[0]).quantize(Decimal('0.00'))

        expected = json.loads(data["expected"])

        try:
            for key, value in expected.items():
                assert jsonpath(response_data, key)[0] == value
            if "提现成功" in data["title"]:
                assert leave_amount - Decimal(
                    json.loads(request_data)["amount"]).quantize(
                        Decimal('0.00')) == leave_amount_after
        except AssertionError as e:
            MidHandler.log.error(e)
            MidHandler.log.info(
                "\ncaseid: {}, title: {}\nurl: {}\nmethod: {}\nheader: {}\ncase_data: {}\nresponse: {}\nresult: {}\n"
                .format(data["case_id"], data["title"], request_url,
                        request_method, request_header, request_data,
                        response_data, "Failed"))
            raise e
        else:
            MidHandler.log.info(
                "\ncaseid: {}, title: {}\nurl: {}\nmethod: {}\nheader: {}\ncase_data: {}\nresponse: {}\nresult: {}\n"
                .format(data["case_id"], data["title"], request_url,
                        request_method, request_header, request_data,
                        response_data, "Passed"))
示例#9
0
def db():
    """管理数据库链接的夹具"""
    db_conn = MidHandler.db_class()
    yield db_conn
    db_conn.close()