예제 #1
0
    def test_register(self, case):
        # 筛选用例的请求数据中做了#register__phone#标记的数据
        if "#register_phone#" in case.request_data:
            while True:
                # 生成随机号码
                mobile_phone = rand_phone("133")
                # 查询数据库有无该随机号码
                count = self.db.find_count(
                    "SELECT Id FROM member WHERE MobilePhone={}".format(
                        mobile_phone))
                # 数据库中无此随机号码,就不用继续随机生成,直接使用该随机号码
                if count == 0:
                    break
            # 将用例中的#register__phone#替换成随机生成的手机号码
            case.request_data = case.request_data.replace(
                "#register_phone#", mobile_phone)

        # 从数据库中查询一个已注册号码给用例
        elif "#exists_phone#" in case.request_data:
            # 从数据库获取第一条号码,给用例参数
            mobile_phone = self.db.find_one(
                "SELECT MobilePhone FROM member LIMIT 1")[0]
            # 用从数据库获取的号码替换掉请求数据中的标记#exists_phone
            case.request_data = case.request_data.replace(
                "#exists_phone#", mobile_phone)

        # 替换各号段的手机号码
        case.request_data = data_replace(case.request_data)

        # 拼接url地址
        url = conf.get("env", "url") + case.url
        # 行数等于用例编号+1
        self.row = case.case_id + 1
        response = self.request.request(method=case.method,
                                        url=url,
                                        data=eval(case.request_data))

        # 该打印的内容会显示在报告中
        print("请求数据--> {}".format(case.request_data))
        print("期望结果--> {}".format(case.expected_data))
        print("服务器响应数据--> {}".format(response.json()))

        res = response.json()

        try:
            self.assertEqual(eval(case.expected_data), res)
        except AssertionError as e:
            result = 'FAIL'
            my_log.exception(e)  # 将异常信息记录到日志
            raise e
        else:
            result = 'PASS'
            my_log.info("预期结果:{}, 实际结果:{}, 断言结果:{}".format(
                eval(case.expected_data), res, result))
        finally:
            # 向Excel回写服务器返回结果
            self.wb.write_data(row=self.row, column=9, value=str(res))
            # 向Excel回写断言结果
            self.wb.write_data(row=self.row, column=10, value=result)
class WithdrawTestCase(unittest.TestCase):

    # 拼接完整的excel路径,然后读取excel数据
    wb = ReadExcel(os.path.join(DATA_DIR, file_name), "withdraw")
    cases = wb.read_column_data(read_column)

    @classmethod
    def setUpClass(cls):
        my_log.info(
            "============================== 准备开始执行取现接口的测试 =============================="
        )
        cls.request = HTTPRequest2()
        cls.db = ExecuteMysql()

    @classmethod
    def tearDownClass(cls):
        my_log.info(
            "============================== 取现接口测试执行完毕 =============================="
        )
        cls.request.close()
        cls.db.close()

    @data(*cases)  # 拆包,拆成几个参数
    def test_withdraw(self, case):
        # 筛选用例的请求数据中做了#register__phone#标记的数据
        if "#register_phone#" in case.request_data:
            while True:
                # 生成随机号码
                mobile_phone = rand_phone()
                # 查询数据库有无该随机号码
                count = self.db.find_count(
                    "SELECT Id FROM member WHERE MobilePhone={}".format(
                        mobile_phone))
                # 数据库中无此随机号码,就不用继续随机生成,直接使用该随机号码
                if count == 0:
                    break
            # 将用例中的#register__phone#替换成随机生成的手机号码
            case.request_data = case.request_data.replace(
                "#register_phone#", mobile_phone)

        # 选取请求的电话号为已注册的测试用例数据
        if "#exists_phone#" in case.request_data:
            # 从数据库获取第一条号码,给用例参数
            mobile_phone = self.db.find_one(
                "SELECT MobilePhone FROM member LIMIT 1")[0]
            # 用从数据库获取的号码替换掉请求数据中的标记#exists_phone
            case.request_data = case.request_data.replace(
                "#exists_phone#", mobile_phone)

        case.request_data = data_replace(case.request_data)

        # 判断是否需要校验数据库
        if case.check_mysql:
            # 将登录手机号替换掉sql语句中的标记#login_phone#
            case.check_mysql = data_replace(case.check_mysql)
            # 调用查询数据方法,传入sql语句,返回元组,下标0取值,decimal
            before_money = self.db.find_one(case.check_mysql)[0]

        # 拼接url地址,发送请求
        url = conf.get("env", "url") + case.url
        self.row = case.case_id + 1
        response = self.request.request(method=case.method,
                                        url=url,
                                        data=eval(
                                            case.request_data))  # 将str转换成dict
        # 该打印的内容会显示在报告中
        print("请求数据--> {}".format(case.request_data))
        print("期望结果---> {}".format(case.expected_data))
        print("服务器响应数据--> {}".format(response.json()))

        # res = response.json()返回json格式,自动转换成Python的dict类型,只取部分字段进行断言
        res = {
            "status": response.json()["status"],
            "code": response.json()["code"],
            "msg": response.json()["msg"]
        }

        try:
            self.assertEqual(eval(case.expected_data), res)

            if case.check_mysql:
                # case.request_data是str类型,先转换为dict再来取值,float
                money = eval(case.request_data)["amount"]
                # 将float类型转换成decimal类型,与数据库查询的结果数据类型一致,并设置保留2位小数
                money = Decimal.from_float(money).quantize(Decimal("0.00"))
                after_money = self.db.find_one(case.check_mysql)[0]
            if "取现成功" in response.json()["msg"]:
                # 该打印的内容会显示在报告中
                print("取现前余额为:{}, 本次取现:{}, 取现后余额:{}".format(
                    before_money, money, after_money))
                self.assertEqual(before_money - money, after_money)

        except AssertionError as e:
            result = 'FAIL'
            my_log.exception(e)  # 将异常信息记录到日志
            raise e
        else:
            result = 'PASS'
            my_log.debug("预期结果:%s, 实际结果:%s, 测试通过" %
                         (eval(case.expected_data), res))

        finally:
            self.wb.write_data(row=self.row, column=10, value=str(res))
            self.wb.write_data(row=self.row, column=11, value=result)
        print("服务器响应数据--> {}".format(response.json()))

        # res = response.json()返回json格式,自动转换成Python的dict类型,只取部分字段进行断言
        res = {"status": response.json()["status"], "code": response.json()["code"]}

        try:
            self.assertEqual(eval(case.expected_data), res)

            if case.check_mysql:
                if "竞标成功" in response.json()["msg"]:
                    # case.request_data是str类型,先转换为dict再来取值,float
                    money = eval(case.request_data)["amount"]
                    # 将float类型转换成decimal类型,与数据库查询的结果数据类型一致,并设置保留2位小数
                    money = Decimal.from_float(money).quantize(Decimal("0.00"))
                    after_money = self.db.find_one(case.check_mysql)[0]
                    # 该打印的内容会显示在报告中
                    print("投资前余额为:{}, 本次投资:{}, 投资后余额:{}".format(before_money, money, after_money))
                    self.assertEqual(before_money - money, after_money)

        except AssertionError as e:
            result = 'FAIL'
            my_log.exception(e)     # 将异常信息记录到日志
            raise e
        else:
            result = 'PASS'
            my_log.debug("预期结果:%s, 实际结果:%s, 测试通过" % (eval(case.expected_data), res))

        finally:
            self.wb.write_data(row=self.row, column=10, value=str(res))
            self.wb.write_data(row=self.row, column=11, value=result)
class GetListTestCase(unittest.TestCase):

    # 拼接完整的excel路径,然后读取excel数据
    wb = ReadExcel(os.path.join(DATA_DIR, file_name), "getList")
    cases = wb.read_column_data(read_column)

    @classmethod
    def setUpClass(cls):
        my_log.info(
            "============================== 准备开始执行获取各种列表接口的测试 ==============================="
        )
        cls.request = HTTPRequest2()
        cls.db = ExecuteMysql()

    @classmethod
    def tearDownClass(cls):
        my_log.info(
            "============================== 获取各种列表接口测试执行完毕 =============================="
        )
        cls.request.close()
        cls.db.close()

    @data(*cases)  # 拆包,拆成几个参数
    def test_get_list(self, case):
        if "#nomemberid#" in case.request_data:
            memberid = self.db.find_one("SELECT MAX(Id) FROM member")[0] + 1
            case.request_data = case.request_data.replace(
                "#nomemberid#", str(memberid))

        if "#noloanid#" in case.request_data:
            loanid = self.db.find_one("SELECT MAX(Id) FROM loan")[0] + 1
            case.request_data = case.request_data.replace(
                "#noloanid#", str(loanid))

        case.request_data = data_replace(case.request_data)

        # 拼接url地址,发送请求
        url = conf.get("env", "url") + case.url
        self.row = case.case_id + 1
        response = self.request.request(method=case.method,
                                        url=url,
                                        data=eval(
                                            case.request_data))  # 将str转换成dict

        if case.check_mysql:
            if "MemberId=#memberid6#" in case.check_mysql:
                case.check_mysql = data_replace(case.check_mysql)
                loanid = self.db.find_one(case.check_mysql)[0]
                setattr(ConText, "loanid", str(loanid))

        # 该打印的内容会显示在报告中
        print("请求数据--> {}".format(case.request_data))
        print("期望结果---> {}".format(case.expected_data))
        print("服务器响应数据--> {}".format(response.json()))

        # res = response.json()返回json格式,自动转换成Python的dict类型,只取部分字段进行断言
        res = {
            "status": response.json()["status"],
            "code": response.json()["code"]
        }

        try:
            self.assertEqual(eval(case.expected_data), res)

        except AssertionError as e:
            result = 'FAIL'
            my_log.exception(e)  # 将异常信息记录到日志
            raise e
        else:
            result = 'PASS'
            my_log.debug("预期结果:%s, 实际结果:%s, 测试通过" %
                         (eval(case.expected_data), res))

        finally:
            self.wb.write_data(row=self.row, column=10, value=str(res))
            self.wb.write_data(row=self.row, column=11, value=result)