예제 #1
0
    def testinfo(self, case):
        #准备测试数据,替换用例中相应的手机号、密码、用户id、项目id
        method = case["method"]
        headers = eval(conf.get("env", "headers"))
        expected = eval(case["expected"])
        row = case["case_id"] + 1
        title = case["title"]
        case["data"] = ReplaceData.replace_data(case["data"])
        data = eval(case["data"])
        # 判断是否是用户信息接口,用户信息接口则加上请求头
        if case["interface"] == "info":
            headers["Authorization"] = ReplaceData.Authorization
            # 添加时间戳和签名到json请求体
            sign = HandleSign.generate_sign(ReplaceData.token)
            data.update(sign)
            case["url"] = ReplaceData.replace_data(case["url"])

        url = conf.get("env", "url") + case["url"]

        #发送请求,获取实际结果
        respons = self.request.send(url=url,
                                    method=method,
                                    headers=headers,
                                    json=data)
        res = respons.json()

        #获取用户id和token值,并保存为类属性
        if case["interface"] == "login":
            ReplaceData.token = jsonpath.jsonpath(res, "$..token")[0]
            ReplaceData.member_id = str(jsonpath.jsonpath(res, "$..id")[0])
            ReplaceData.Authorization = jsonpath.jsonpath(
                res, "$..token_type")[0] + " " + jsonpath.jsonpath(
                    res, "$..token")[0]

        #断言:比对预期结果和实际结果
        try:
            self.assertEqual(expected["code"], res["code"])
            self.assertIn(expected["msg"], res["msg"])
            #判断是否需要SQL校验
            if case["sql_check"]:
                #获取执行结果的用户可用余额
                info = jsonpath.jsonpath(res, "$..leave_amount")[0]
                # 获取预期结果的用户可用余额
                user_info = self.db.find_one(case["sql_check"].format(
                    ReplaceData.member_id))["leave_amount"]
                self.assertEqual(user_info, Decimal(str(info)))
        except AssertionError as e:
            print("预期结果:{}".format(expected))
            print("实际结果:{}".format(res))
            self.excel.write_data(row=row, column=8, value="未通过")
            log.error("用例未通过:{},错误原因:{}".format(title, e))
            raise e
        else:
            self.excel.write_data(row=row, column=8, value="通过")
            log.debug("用例通过:{}".format(title))
예제 #2
0
    def test_recharge(self, case):
        # 第一步,准备用例数据
        url = conf.get("env", "url") + case["url"]
        method = case["method"]
        # 替换参数中的用户ID
        # case["data"] = case["data"].replace("#member_id#", str(self.member_id))
        case["data"] = replace_data(case["data"])
        data = eval(case["data"])
        # 在请求体中加入时间戳和签名
        sign_info = HandleSign.generate_sign(getattr(CaseData, "token"))
        data.update(sign_info)

        headers = eval(conf.get("env", "headers"))
        headers["Authorization"] = getattr(CaseData, "token_value")

        expected = eval(case["expected"])
        row = case["case_id"] + 1
        if case["check_sql"]:
            # 查询当前用户的余额
            sql = "SELECT leave_amount FROM futureloan.member WHERE mobile_phone={}".format(
                conf.get("test_data", "phone"))
            start_money = self.db.find_one(sql)["leave_amount"]

        # 第二步,发送请求,获取结果
        response = self.request.send(url=url,
                                     method=method,
                                     json=data,
                                     headers=headers)
        res = response.json()
        if case["check_sql"]:
            # 查询当前用户的余额
            sql = "SELECT leave_amount FROM futureloan.member WHERE mobile_phone={}".format(
                conf.get("test_data", "phone"))
            end_money = self.db.find_one(sql)["leave_amount"]

        # 第三步,断言比对预期结果和实际结果
        try:
            self.assertEqual(expected["code"], res["code"])
            self.assertEqual(expected["msg"], res["msg"])
            # 判断是否需要sql校验
            if case["check_sql"]:
                self.assertEqual(end_money - start_money,
                                 Decimal(str(data["amount"])))

        except AssertionError as e:
            print("预期结果", expected)
            print("实际结果", res)
            self.excel.write_data(row=row, column=8, value="未通过")
            log.error("用例{}执行未通过".format(case["title"]))
            log.exception(e)
            raise e

        else:
            self.excel.write_data(row=row, column=8, value="通过")
            log.error("用例{}执行通过".format(case["title"]))
    def test_main_stream(self, case):
        # 第一步:准备用例数据
        url = conf.get("env", "base_url") + replace_data(case["url"])
        method = case["method"]
        if case["interface"] == "register":
            # 注册接口,则随机生成一个手机号码
            CaseData.mobilephone = self.random_phone()
        data = eval(replace_data(case["data"]))
        headers = eval(conf.get("env", "headers"))

        # 判断是否是登录接口,不是登录接口则需要添加token
        if case["interface"] != "login" and case["interface"] != "register":
            headers['Authorization'] = getattr(CaseData, 'token_value')
            # headers["Authorization"] = getattr(CaseData, "token_value")
            # 在请求体中加入,时间戳和签名
            sign_info = HandleSign.generate_sign(getattr(CaseData, "token"))
            data.update(sign_info)

        expected = eval(case["expected"])
        row = case["case_id"] + 1
        # 第二步:发送请求,获取结果
        print("请求参数:", data)
        response = self.request.send_request(url=url,
                                             method=method,
                                             json=data,
                                             headers=headers)
        res = response.json()
        print("预期结果", expected)
        print("实际结果", res)
        # 发送请求后,判断是否是登陆接口
        if case["interface"].lower() == "login":
            # 提取用户id保存为类属性
            CaseData.member_id = str(jsonpath.jsonpath(res, "$..id")[0])
            token = jsonpath.jsonpath(res, "$..token")[0]
            token_type = jsonpath.jsonpath(res, "$..token_type")[0]
            # 提取token,保存为类属性
            CaseData.token_value = token_type + " " + token
            CaseData.token = token
        # 判断是否是加标的用例,如果是的则请求标id
        if case["interface"] == "add":
            CaseData.loan_id = str(jsonpath.jsonpath(res, "$..id")[0])
        # 第三步:断言(比对预期结果和实际结果)
        try:
            self.assertEqual(expected["code"], res["code"])
            self.assertIn(expected["msg"], res["msg"])
        except AssertionError as e:
            self.excel.write_excel(row=row, column=8, value="未通过")
            log.error("用例:{},执行未通过".format(case["title"]))
            log.exception(e)
            raise e
        else:
            self.excel.write_excel(row=row, column=8, value="通过")
            log.info("用例:{},执行未通过".format(case["title"]))
예제 #4
0
    def testwithdraw(self, case):
        #准备测试数据,对登录接口用例替换相应的测试手机号、密码
        url = conf.get("env", "url") + case["url"]
        method = case["method"]
        headers = eval(conf.get("env", "headers"))
        expected = eval(case["expected"])
        row = case["case_id"] + 1
        title = case["title"]
        case["data"] = ReplaceData.replace_data(case["data"])
        data = eval(case["data"])
        # 判断是否是取现接口,取现接口则加上请求头,替换相应的用户id,添加时间戳和签名到json请求体
        if case["interface"] == "withdraw":
            headers["Authorization"] = ReplaceData.Authorization
            sign = HandleSign.generate_sign(ReplaceData.token)
            data.update(sign)

        # 判断是否需要进行sql校验
        if case["sql_check"]:
            #查询充值前用户的可用余额
            start_leave_amount = self.db.find_one(case["sql_check"].format(
                conf.get("test_data", "phone")))["leave_amount"]
        #发送请求,获取实际结果
        respons = self.request.send(url=url,
                                    method=method,
                                    headers=headers,
                                    json=data)
        res = respons.json()
        # 判断是否是登录接口
        if case["interface"] == "login":
            # 提取token,保存为类属性
            ReplaceData.token = jsonpath.jsonpath(res, "$..token")[0]
            token_type = jsonpath.jsonpath(res, "$..token_type")[0]
            ReplaceData.Authorization = token_type + " " + ReplaceData.token
            # 提取用户id保存为类属性
            ReplaceData.member_id = str(jsonpath.jsonpath(res, "$..id")[0])
        #断言,比对结果
        try:
            self.assertEqual(expected["code"], res["code"])
            self.assertEqual(expected["msg"], res["msg"])
            if case["sql_check"]:
                end_leave_amount = self.db.find_one(case["sql_check"].format(
                    conf.get("test_data", "phone")))["leave_amount"]
                self.assertEqual(Decimal(str(data["amount"])),
                                 start_leave_amount - end_leave_amount)
        except AssertionError as e:
            print("预期结果:{}".format(expected))
            print("实际结果:{}".format(res))
            self.excel.write_data(row=row, column=8, value="未通过")
            log.error("用例未通过:{},错误原因:{}".format(title, e))
            raise e
        else:
            self.excel.write_data(row=row, column=8, value="通过")
            log.debug("用例通过:{}".format(title))
예제 #5
0
    def test_audit(self, case):
        # 拼接完整的接口地址
        url = conf.get_str("env", "url") + case["url"]
        # 请求的方法
        method = case["method"]
        # 请求参数
        # 替换用例参数
        case["data.txt"] = replace_data(case["data.txt"])
        data = eval(case["data.txt"])
        # 获取sign和时间戳
        sign_data = HandleSign.generate_sign(getattr(TestData, "token"))
        # 将sign加入到请求参数中
        data.update(sign_data)
        # 请求头
        headers = eval(conf.get_str("env", "headers"))
        headers["Authorization"] = getattr(TestData, "token_data")

        # 预期结果
        expected = eval(case["expected"])
        # 该用例在表单的中所在行
        row = case["case_id"] + 1

        # ------第二步:发送请求到接口,获取实际结果--------
        response = self.http.send(url=url, method=method, json=data, headers=headers)
        result = response.json()
        # 如果审核通过的项目返回ok,说明该项目已审核
        if case["title"] == "审核通过" and result["msg"] == "OK":
            pass_loan_id = getattr(TestData, "loan_id")
            # 将该项目的id保存起来
            setattr(TestData, "pass_loan_id", pass_loan_id)

        # -------第三步:比对预期结果和实际结果-----
        try:
            self.assertEqual(expected["code"], result["code"])
            self.assertEqual((expected["msg"]), result["msg"])
            if case["check_sql"]:
                sql = replace_data(case["check_sql"])
                # 获取这个标的用户id
                status = self.db.get_one(sql)[0]
                # 进行断言
                self.assertEqual(expected["status"], status)
        except AssertionError as e:
            self.excel.write_data(row=row, column=8, value="未通过")
            my_log.info("用例:{}--->执行未通过".format(case["title"]))
            print("预取结果:{}".format(expected))
            print("实际结果:{}".format(result))
            raise e
        else:
            self.excel.write_data(row=row, column=8, value="通过")
            my_log.info("用例:{}--->执行通过".format(case["title"]))
예제 #6
0
    def test_withdraw(self,case):
        """
        将登录放在setupclass中实现,获取到登录后的token_value作为该接口的入参
        :param case:
        :return:
        """
        url = conf.get('env','base_url') + case['url']
        method = case['method']

        headers = eval(conf.get('env', 'headers'))
        headers['Authorization'] = getattr(CaseData,'token_value')

        case['data'] = replace_data(case['data'])
        data = eval(case['data'])
        # 在请求体中加入,时间戳和签名
        sign_info = HandleSign.generate_sign(getattr(CaseData, "token"))
        data.update(sign_info)

        print('测试用例中data的值是:',data)
        expected = eval(case['expected'])
        row = case['case_id'] + 1

        if case['check_sql']:
            sql = 'select * from futureloan.member where id={}'.format(getattr(CaseData,'member_id'))
            result = self.db.find_one(sql)
            start_amount = result['leave_amount']
            # print('执行充值前的金额是:',start_amount)
        response = self.request.send_request(url=url,method=method,json=data,headers=headers)
        res = response.json()
        print('提现-测试用例执行结果:',res)

        if case['check_sql']:
            sql = 'select * from futureloan.member where id={}'.format(getattr(CaseData, 'member_id'))
            result = self.db.find_one(sql)
            end_amount = result['leave_amount']
            # print('执行充值后的金额是:', end_amount)
        try:
            self.assertEqual(res['code'],expected['code'])
            # self.assertEqual(res['msg'],expected['msg'])
            if case['check_sql']:
                self.assertEqual(start_amount-end_amount,Decimal(str(data['amount'])))
        except Exception as e:
            self.excel.write_excel(row=row,column=8,value='未通过')
            log.error('测试用例{}执行未通过'.format(case['title']))
            log.exception(e)
            raise e
        else:
            self.excel.write_excel(row=row,column=8,value='通过')
            log.info('测试用例{}执行通过'.format(case['title']))
예제 #7
0
 def test_recharge(self, case):
     # 第一步:准备用例数据
     url = conf.get("env", "url") + case["url"]
     method = case["method"]
     # 替换参数中的用户id
     case["data"] = replace_data(case["data"])
     data = eval(case["data"])
     # 在请求体中加入,时间戳和签名
     sign_info = HandleSign.generate_sign(getattr(CaseDate,"token"))
     data.update(sign_info)
     headers = eval(conf.get("env", "headers"))
     headers["Authorization"] = getattr(CaseDate,"token_value")
     # 在请求头中加入setupclass中提取出来的token
     expected = eval(case["expected"])
     row = case["case_id"] + 1
     # 第二步:发送请求,获取结果
     # 发送请求之前,获取用余额
     if case["check_sql"]:
         sql = "SELECT leave_amount FROM futureloan.member WHERE mobile_phone={}".format(
             conf.get("test_data", "phone"))
         # 查询当前用户的余额
         start_money = self.db.find_one(sql)["leave_amount"]
     print("当前请求的参数为:",data)
     response = self.request.send(url=url, method=method, json=data, headers=headers)
     res = response.json()
     # 发送请求之后,获取用余额
     if case["check_sql"]:
         sql = "SELECT leave_amount FROM futureloan.member WHERE mobile_phone={}".format(
             conf.get("test_data", "phone"))
         # 查询当前用户的余额
         end_money = self.db.find_one(sql)["leave_amount"]
     # 第三步:断言(比对预期结果和实际结果)
     try:
         self.assertEqual(expected["code"], res["code"])
         self.assertEqual(expected["msg"], res["msg"])
         # 判断示范需要进行sql校验
         if case["check_sql"]:
             self.assertEqual(end_money - start_money, Decimal(str(data["amount"])))
     except AssertionError as e:
         print("预期结果:", expected)
         print("实际结果:", res)
         self.excel.write_data(row=row, column=8, value="未通过")
         log.error("用例:{},执行未通过".format(case["title"]))
         log.exception(e)
         raise e
     else:
         self.excel.write_data(row=row, column=8, value="通过")
         log.info("用例:{},执行未通过".format(case["title"]))
예제 #8
0
 def setUp(self):
     # ---------每个审核的用例执行之前都加一个项目,将项目id保存起来----------
     url = conf.get_str("env", "url") + "/loan/add"
     data = {"member_id": getattr(TestData, "member_id"),
             "title": "借钱实现财富自由",
             "amount": 2000,
             "loan_rate": 12.0,
             "loan_term": 3,
             "loan_date_type": 1,
             "bidding_days": 5}
     # 获取sign和时间戳
     sign_data = HandleSign.generate_sign(getattr(TestData, "token"))
     # 将sign加入到请求参数中
     data.update(sign_data)
     headers = eval(conf.get_str("env", "headers"))
     headers["Authorization"] = getattr(TestData, "token_data")
     # 发送请求加标
     response = self.http.send(url=url, method="post", json=data, headers=headers)
     json_data = response.json()
     # 1、提取标id
     loan_id = jsonpath.jsonpath(json_data, "$..id")[0]
     # 2、保存为TestDate的属性
     setattr(TestData, "loan_id", str(loan_id))
예제 #9
0
    def test_update(self, case):
        url = conf.get('env', 'base_url') + case['url']
        method = case['method']
        case['data'] = replace_data(case['data'])
        data = eval(case['data'])
        # 在请求体中加入,时间戳和签名
        sign_info = HandleSign.generate_sign(getattr(CaseData, "token"))
        data.update(sign_info)
        # print('data中reg_name的值是',data['reg_name'],type(data['reg_name']))
        headers = eval(conf.get('env', 'headers'))
        headers['Authorization'] = getattr(CaseData, 'token_value')
        expected = eval(case["expected"])
        row = case["case_id"] + 1

        response = self.request.send_request(url=url,
                                             method=method,
                                             json=data,
                                             headers=headers)
        res = response.json()
        print('更新昵称接口返回的结果:', res)
        try:
            self.assertEqual(res['code'], expected['code'])
            if case['check_sql']:
                sql = 'select * from futureloan.member where mobile_phone={}'.format(
                    conf.get('test_data', 'mobile_phone'))
                res_name = self.db.find_one(sql)['reg_name']
                # print('执行后结果昵称是:',res_name,type(res_name))
                self.assertEqual(res_name, data['reg_name'])
        except Exception as e:
            self.excel.write_excel(row=row, column=8, value='未通过')
            log.error('测试用例{}执行未通过'.format(case['title']))
            log.exception(e)
            raise e
        else:
            self.excel.write_excel(row=row, column=8, value='通过')
            log.info('测试用例执行通过')
예제 #10
0
    def test_add(self, case):
        """
        将登录放在测试用例excel的第一行,根据接口类型,判断如果是登录,那么获取其响应结果中的token_value
        :param case:
        :return:
        """
        url = conf.get('env', 'base_url') + case['url']
        method = case['method']

        headers = eval(conf.get('env', 'headers'))

        if case['interface'] != 'login':
            headers['Authorization'] = getattr(CaseData, 'token_value')

        case['data'] = replace_data(case['data'])
        data = eval(case['data'])

        if case['interface'] != 'login':
            sign_info = HandleSign.generate_sign(getattr(CaseData, "token"))
            data.update(sign_info)
            # 在请求体中加入,时间戳和签名

        print('测试用例中data的值是:', data)
        expected = eval(case['expected'])
        row = case['case_id'] + 1

        if case['check_sql']:
            case['check_sql'] = replace_data(case['check_sql'])
            sql = case['check_sql']
            result = self.db.find_count(sql)
            start_sum = result
            print('执行添加项目前的总项目数是:', start_sum)

        response = self.request.send_request(url=url,
                                             method=method,
                                             json=data,
                                             headers=headers)
        res = response.json()
        print('执行接口后的结果是:', res)

        if case['interface'] == 'login':
            token_type = jsonpath.jsonpath(res, '$..token_type')[0]
            token = jsonpath.jsonpath(res, '$..token')[0]
            member_id = str(jsonpath.jsonpath(res, '$..id')[0])

            token_value = token_type + ' ' + token
            CaseData.token = token
            CaseData.token_value = token_value
            CaseData.member_id = member_id

        if case['check_sql']:
            case['check_sql'] = replace_data(case['check_sql'])
            sql = case['check_sql']
            result = self.db.find_count(sql)
            end_sum = result
            print('执行添加项目后的总项目数是:', end_sum)

        try:
            self.assertEqual(res['code'], expected['code'])
            # self.assertEqual(res['msg'],expected['msg'])
            if case['check_sql']:
                self.assertEqual(end_sum - start_sum, 1)
        except Exception as e:
            self.excel.write_excel(row=row, column=8, value='未通过')
            log.error('测试用例{}执行未通过'.format(case['title']))
            log.exception(e)
            raise e
        else:
            self.excel.write_excel(row=row, column=8, value='通过')
            log.info('测试用例{}执行通过'.format(case['title']))
예제 #11
0
    def test_withdraw(self, case):
        # ------第一步:准备用例数据------------
        # 拼接完整的接口地址
        url = conf.get_str("env", "url") + case["url"]
        # 请求的方法
        method = case["method"]
        # 请求参数
        # 判断是否有用户id需要替换
        case["data.txt"] = replace_data(case["data.txt"])
        data = eval(case["data.txt"])

        # 请求头
        headers = eval(conf.get_str("env", "headers"))
        if case["interface"] != "login":
            headers["Authorization"] = getattr(TestData, "token_data")
            # 获取sign和时间戳
            sign_data = HandleSign.generate_sign(getattr(TestData, "token"))
            # 将sign加入到请求参数中
            data.update(sign_data)
        # 预期结果
        expected = eval(case["expected"])
        # 该用例在表单的中所在行
        row = case["case_id"] + 1

        # ------第二步:发送请求到接口,获取实际结果--------
        # 判断是否需要sql校验
        if case["check_sql"]:
            sql = case["check_sql"].format(conf.get_str("test_data", 'user'))
            # 获取取充值之前的余额
            start_money = self.db.get_one(sql)[0]
        # 发送请求,获取结果
        response = self.http.send(url=url,
                                  method=method,
                                  json=data,
                                  headers=headers)
        result = response.json()

        if case["interface"] == "login":
            # -------如果是登录接口,从响应结果中提取用户id和token-------------
            # 1、用户id
            member_id = jsonpath.jsonpath(result, "$..id")[0]
            setattr(TestData, "member_id", str(member_id))
            # 2、提取token
            token_type = jsonpath.jsonpath(result, "$..token_type")[0]
            token = jsonpath.jsonpath(result, "$..token")[0]
            setattr(TestData, "token", token)
            token_data = token_type + " " + token
            setattr(TestData, "token_data", token_data)

        # -------第三步:比对预期结果和实际结果-----
        try:
            self.assertEqual(expected["code"], result["code"])
            self.assertEqual((expected["msg"]), result["msg"])
            # 判断是否需要数据库校验
            if case["check_sql"]:
                sql = case["check_sql"].format(
                    conf.get_str("test_data", 'user'))
                # 获取取充值之前的余额
                end_money = self.db.get_one(sql)[0]
                recharge_money = decimal.Decimal(str(data["amount"]))
                my_log.info("取现之前金额为{}\n,取现金额为:{}\n,取现之后金额为{},".format(
                    start_money, recharge_money, end_money))
                # 进行断言(开始的金额减去结束的金额)
                self.assertEqual(recharge_money, start_money - end_money)

        except AssertionError as e:
            self.excel.write_data(row=row, column=8, value="未通过")
            my_log.info("用例:{}--->执行未通过".format(case["title"]))
            print("预取结果:{}".format(expected))
            print("实际结果:{}".format(result))
            raise e
        else:
            self.excel.write_data(row=row, column=8, value="通过")
            my_log.info("用例:{}--->执行通过".format(case["title"]))
예제 #12
0
    def testinvest(self, case):
        #准备测试数据,替换用例中相应的手机号、密码、用户id、项目id
        url = conf.get("env", "url") + case["url"]
        method = case["method"]
        headers = eval(conf.get("env", "headers"))
        expected = eval(case["expected"])
        row = case["case_id"] + 1
        title = case["title"]
        case["data"] = ReplaceData.replace_data(case["data"])
        data = eval(case["data"])
        # 判断是否是投资接口,投资接口则加上请求头
        if case["interface"] != "login":
            headers["Authorization"] = ReplaceData.Authorization
            # 添加时间戳和签名到json请求体
            sign = HandleSign.generate_sign(ReplaceData.token)
            data.update(sign)
            #判断是否需要sql校验
            if case["sql_check"]:
                # 判断SQL校验是普通用例还是生成了回款计划的用例
                if case["title"] == "投资金额等于项目剩余可投金额":
                    sql = eval(case["sql_check"])
                    # 查询执行用例前当前用户的可用余额数
                    start_leave_amount = self.db.find_one(sql[0].format(
                        ReplaceData.member_id))["leave_amount"]
                    # 查询执行用例前该项目中当前用户的投资记录总数
                    start_invest = self.db.find_count(sql[1].format(
                        ReplaceData.loan_id, ReplaceData.member_id))
                    # 查询执行用例前当前该项目中当前用户的流水记录总数
                    start_financelog = self.db.find_count(sql[2].format(
                        ReplaceData.member_id))
                    # 查询执行用例前当前该项目中当前用户的回款记录总数
                    start_repayment = self.db.find_count(sql[3].format(
                        ReplaceData.loan_id, ReplaceData.member_id))
                else:
                    # 查询执行用例前该项目中当前用户的投资记录总数
                    start_invest = self.db.find_count(case["sql_check"].format(
                        getattr(ReplaceData, "loan_id"),
                        getattr(ReplaceData, "member_id")))

        #发送请求,获取实际结果
        respons = self.request.send(url=url,
                                    method=method,
                                    headers=headers,
                                    json=data)
        res = respons.json()

        #获取管理员用户id和token值,并保存为类属性
        if case["interface"] == "login":
            ReplaceData.member_id = str(jsonpath.jsonpath(res, "$..id")[0])
            ReplaceData.token = jsonpath.jsonpath(res, "$..token")[0]
            ReplaceData.Authorization = jsonpath.jsonpath(
                res, "$..token_type")[0] + " " + jsonpath.jsonpath(
                    res, "$..token")[0]
        # 获取加标项目id,并保存为类属性
        if case["interface"] == "add":
            ReplaceData.loan_id = str(jsonpath.jsonpath(res, "$..id")[0])

        #断言:比对预期结果和实际结果
        try:
            self.assertEqual(expected["code"], res["code"])
            self.assertIn(expected["msg"], res["msg"])
            #判断是否需要SQL校验
            if case["sql_check"]:
                #判断SQL校验是普通用例还是生成了回款计划的用例
                if case["title"] == "投资金额等于项目剩余可投金额":
                    sql = eval(case["sql_check"])
                    # 查询执行用例前当前用户的可用余额数
                    end_leave_amount = self.db.find_one(sql[0].format(
                        ReplaceData.member_id))["leave_amount"]
                    self.assertEqual(
                        end_leave_amount,
                        start_leave_amount - Decimal(str(data["amount"])))
                    # 查询执行用例前该项目中当前用户的投资记录总数
                    end_invest = self.db.find_count(sql[1].format(
                        ReplaceData.loan_id, ReplaceData.member_id))
                    self.assertEqual(1, end_invest - start_invest)
                    # 查询执行用例前当前该项目中当前用户的流水记录总数
                    end_financelog = self.db.find_count(sql[2].format(
                        ReplaceData.member_id))
                    self.assertEqual(1, end_financelog - start_financelog)
                    # 查询执行用例前当前该项目中当前用户的回款记录总数
                    end_repayment = self.db.find_count(sql[3].format(
                        ReplaceData.loan_id, ReplaceData.member_id))
                    self.assertEqual(3, end_repayment - start_repayment)
                else:
                    # 查询执行用例前该项目中当前用户的投资记录总数
                    end_invest = self.db.find_count(case["sql_check"].format(
                        ReplaceData.loan_id, ReplaceData.member_id))
                    self.assertEqual(1, end_invest - start_invest)
        except AssertionError as e:
            print("预期结果:{}".format(expected))
            print("实际结果:{}".format(res))
            self.excel.write_data(row=row, column=8, value="未通过")
            log.error("用例未通过:{},错误原因:{}".format(title, e))
            raise e
        else:
            self.excel.write_data(row=row, column=8, value="通过")
            log.debug("用例通过:{}".format(title))
예제 #13
0
    def test_invest(self, case):
        """
        将登录放在测试用例excel的第一行,根据接口类型,判断如果是登录,那么获取其响应结果中的token_value
        :param case:
        :return:
        """
        url = conf.get('env', 'base_url') + case['url']
        method = case['method']

        headers = eval(conf.get('env', 'headers'))
        if case['interface'] != 'login':
            headers['Authorization'] = getattr(CaseData, 'token_value')

        case['data'] = replace_data(case['data'])
        data = eval(case['data'])
        if case['interface'] != 'login':
            sign_info = HandleSign.generate_sign(getattr(CaseData, "token"))
            data.update(sign_info)
        # 在请求体中加入,时间戳和签名

        print('测试用例中data的值是:', data)
        expected = eval(case['expected'])
        row = case['case_id'] + 1

        # 关于sql校验:需要校验投资用户可用余额减少(sql1)、需要校验流水记录表中增加了一条记录
        if case['check_sql']:
            sql1 = "select * from futureloan.member where id={}".format(
                getattr(CaseData, 'member_id'))
            start_amount = self.db.find_one(sql1)['leave_amount']
            print('执行投资前的用户余额时:', start_amount)
            sql2 = "select * from futureloan.financeLog where pay_member_id={}".format(
                getattr(CaseData, 'member_id'))
            start_sum = self.db.find_count(sql2)
            print("执行投资前流水记录表中的数量是:", start_sum)
            sql3 = "SELECT * FROM futureloan.invest WHERE member_id={} and loan_id={}".format(
                CaseData.member_id, CaseData.loan_id)
            start_invest = self.db.find_count(sql3)
            print('投资前的投资记录条数:', start_invest)

        response = self.request.send_request(url=url,
                                             method=method,
                                             json=data,
                                             headers=headers)
        res = response.json()
        print('执行接口后的结果是:', res)

        if case['interface'] == 'login':
            token_type = jsonpath.jsonpath(res, '$..token_type')[0]
            token = jsonpath.jsonpath(res, '$..token')[0]
            member_id = str(jsonpath.jsonpath(res, '$..id')[0])

            token_value = token_type + ' ' + token
            CaseData.token_value = token_value
            CaseData.member_id = member_id
            CaseData.token = token
        if case['interface'] == 'add':
            loan_id = str(jsonpath.jsonpath(res, '$..id')[0])
            CaseData.loan_id = loan_id

        if case['check_sql']:
            sql = "select * from futureloan.member where id={}".format(
                getattr(CaseData, 'member_id'))
            end_amount = self.db.find_one(sql)['leave_amount']
            print('执行投资后的用户余额时:', end_amount)
            sql2 = "select * from futureloan.financeLog where pay_member_id={}".format(
                getattr(CaseData, 'member_id'))
            end_sum = self.db.find_count(sql2)
            print("执行投资后流水记录表中的数量是:", end_sum)
            sql3 = "SELECT * FROM futureloan.invest WHERE member_id={} and loan_id={}".format(
                CaseData.member_id, CaseData.loan_id)
            end_invest = self.db.find_count(sql3)
            print('投资后的投资记录条数:', end_invest)

        try:
            self.assertEqual(res['code'], expected['code'])
            # self.assertEqual(res['msg'],expected['msg'])
            if case['check_sql']:
                self.assertEqual(start_amount - end_amount,
                                 Decimal(str(data['amount'])))
                self.assertEqual(end_sum - start_sum, 1)
                self.assertEqual(end_invest - start_invest, 1)
                # 关于sql校验:除了以上校验外,如果达到满标(即投资额=用户可用余额,那么在回款计划表中会新增一条记录)
                # if end_amount == data['amount']:
                #     sql3 = 'select * from futureloan.repayment where invest_id={}'.format(getattr(CaseData, 'member_id'))
                #     repay_amount = self.db.find_count(sql3)
                #     self.assertEqual(repay_amount,1)
                if "满标" in case["title"]:
                    # 获取当前标所有的投资记录id
                    sql4 = "SELECT id FROM futureloan.invest WHERE loan_id={}".format(
                        CaseData.loan_id)
                    invest_ids = self.db.find_all(sql4)
                    # 遍历该标所有的投资记录,id
                    for invest in invest_ids:
                        sql5 = "SELECT * FROM futureloan.repayment WHERE invest_id={}".format(
                            invest["id"])
                        # 获取当前这条投资记录,生成对应的回款
                        count = self.db.find_count(sql5)
                        # 断言查询到的条数的布尔值是否为True(0的布尔值是Flase,只要不是0条,这个断言就会通过)
                        self.assertTrue(count)
        except Exception as e:
            self.excel.write_excel(row=row, column=8, value='未通过')
            log.error('测试用例{}执行未通过'.format(case['title']))
            log.exception(e)
            raise e
        else:
            self.excel.write_excel(row=row, column=8, value='通过')
            log.info('测试用例{}执行通过'.format(case['title']))