Esempio n. 1
0
class RegisterTest(unittest.TestCase):
    '这是测试注册接口的类'
    # 使用doexcel_study中的方法调用
    do_excel_regitser = DoExcel(contants.excel_file)  # 传入do_excel_study.xlsx
    cases_register = do_excel_regitser.read_excel(
        "register")  # 读取register_sheet

    @classmethod  # 为什么用类方法? 整个类只执行一次!
    def setUpClass(cls):  # 每个测试类里面去运行的操作都放到类方法里面
        cls.request = Request()  # 实例化对象

    def setUp(self):
        # self.write_register = DoExcel(contants.excel_file, "register") # 创建一个对象写入
        logger.info("开始执行用例")

    def tearDown(self):
        logger.info("用例执行结束")

    @classmethod
    def tearDownClass(cls):
        cls.request.session.close()  # 关闭session请求
        cls.mysql.close()  # 关闭数据库连接

    mysql = MysqlUtil()
    sql = "select max(mobilephone) from future.member"
    max = mysql.fetch_one(sql)[0]  # 执行SQL,并且返回最近的一条数据,是元祖,使用下标取第一个值
    # print(max)

    @data(*cases_register)
    def test_register(self, case):  # 测试注册
        logger.info("开始执行第{}条用例: {}".format(case.case_id, case.title))
        logger.debug('url:{}'.format(case.url))
        logger.debug('data:{}'.format(case.data))
        logger.debug('method:{}'.format(case.method))
        logger.debug('expected:{}'.format(case.expected))
        data_dict = json.loads(case.data)
        if data_dict['mobilephone'] == '#@mobilephone':
            # 取最大电话号码+1
            data_dict['mobilephone'] = int(self.max) + 1
        # resp = self.request.request(case.method, case.url, data_dict)

        data_dict = json.dumps(data_dict)  # 把字典转换成字符串传入context进行转换
        # print(data_dict, type(data_dict))
        register_data_new = Context.replace_new(data_dict)
        resp = self.request.request(case.method, case.url, register_data_new)

        try:
            # self.assertEqual(case.expected, resp.text)
            self.assertEqual(case.expected, json.loads(resp.text)['code'])
            self.do_excel_regitser.write_excel('register', case.case_id + 1,
                                               resp.text,
                                               'PASS')  # 读取sheet,写入结果
            logger.info("第{0}用例执行结果:PASS".format(case.case_id))
        except AssertionError as e:
            self.do_excel_regitser.write_excel('register', case.case_id + 1,
                                               resp.text, 'FAIL')
            logger.error("第{0}用例执行结果:FAIL".format(case.case_id))
            logger.error("断言出错了".format(e))
            raise e
class AuditTest(unittest.TestCase):
    '这是测试审核接口的类'
    # 使用doexcel_study中的方法调用
    do_excel = DoExcel(contants.excel_file)  # 传入do_excel_study.xlsx
    cases_audit = do_excel.read_excel("audit")  # 读取audit_sheet

    @classmethod  # 为什么用类方法? 整个类只执行一次!
    def setUpClass(cls):  # 每个测试类里面去运行的操作都放到类方法里面
        cls.request = Request()  # 实例化对象
        cls.mysql = MysqlUtil()

    def setUp(self):
        # self.write_register = DoExcel(contants.excel_file, "audit") # 创建一个对象写入
        logger.info("开始执行用例")

    def tearDown(self):
        logger.info("用例执行结束")

    @classmethod
    def tearDownClass(cls):
        cls.request.session.close()  # 关闭session请求
        cls.mysql.close()  # 关闭数据库连接

    @data(*cases_audit)
    def test_audit(self, case):  # 测试注册
        logger.info("开始执行第{}条用例: {}".format(case.case_id, case.title))
        logger.debug('url:{}'.format(case.url))
        logger.debug('data:{}'.format(case.data))
        logger.debug('method:{}'.format(case.method))
        logger.debug('expected:{}'.format(case.expected))

        audit_data_new = Context.replace_new(case.data)
        resp = self.request.request(case.method, case.url, audit_data_new)

        # resp = self.request.request(case.method, case.url, case.data)
        try:
            # self.assertEqual(json.loads(case.expected)['msg'], json.loads(resp.text)['msg'])
            self.assertEqual(
                json.loads(case.expected)['code'],
                resp.json()['code'], "member_list error")
            self.do_excel.write_excel('audit', case.case_id + 1, resp.text,
                                      'PASS')  # 读取sheet,写入结果
            logger.info("第{0}用例执行结果:PASS".format(case.case_id))

            # 判断是否加标成功,如果成功就按照借款人ID去数据库查询最新的加标的记录
            if resp.json()['msg'] == '加标成功':
                loan_member_id = getattr(Context, 'loan_member_id')
                sql = "select id from future.loan where memberid='{0}' " \
                      "order by createTime desc limit 1".format(loan_member_id)
                loan_id = self.mysql.fetch_one(sql)[0]
                setattr(Context, 'loan_id',
                        str(loan_id))  # 和excel中的名字保持一致, 记得转成字符串str,后续要通过正则替换

        except AssertionError as e:
            self.do_excel.write_excel('audit', case.case_id + 1, resp.text,
                                      'FAIL')
            logger.error("第{0}用例执行结果:FAIL".format(case.case_id))
            logger.error("断言出错了".format(e))
            raise e
Esempio n. 3
0
class GenerateRepaymentsApiTest(unittest.TestCase):
    '这是测试回款计划接口的类'
    # 使用doexcel_study中的方法调用
    do_excel = DoExcel(contants.excel_file)  # 传入do_excel_study.xlsx
    cases_generateRepayments = do_excel.read_excel(
        "generateRepayments_api")  # 读取generateRepayments

    @classmethod  # 为什么用类方法? 整个类只执行一次!
    def setUpClass(cls):  # 每个测试类里面去运行的操作都放到类方法里面
        cls.request = Request()  # 实例化对象

    def setUp(self):
        # self.write_recharge = DoExcel(contants.excel_file, "generateRepayments_api") # 创建一个对象写入
        logger.info("开始执行用例")

    def tearDown(self):
        logger.info("用例执行结束")

    @classmethod
    def tearDownClass(cls):
        cls.request.session.close()  # 关闭session请求

    @data(*cases_generateRepayments)
    def test_generateRepayments(self, case):  # 测试注册
        logger.info("开始执行第{}条用例: {}".format(case.case_id, case.title))
        logger.debug('url:{}'.format(case.url))
        logger.debug('data:{}'.format(case.data))
        logger.debug('method:{}'.format(case.method))
        logger.debug('expected:{}'.format(case.expected))

        generateRepayments_api_data_new = Context.replace_new(
            case.data)  # 调用类的方法替换参数
        resp = self.request.request(case.method, case.url,
                                    generateRepayments_api_data_new)

        try:
            self.assertEqual(
                json.loads(case.expected)['msg'],
                json.loads(resp.text)['msg'])
            self.do_excel.write_excel('generateRepayments_api',
                                      case.case_id + 1, resp.text,
                                      'PASS')  # 读取sheet,写入结果
            logger.info("第{0}用例执行结果:PASS".format(case.case_id))

        except AssertionError as e:
            self.do_excel.write_excel('generateRepayments_api',
                                      case.case_id + 1, resp.text, 'FAIL')
            logger.error("第{0}用例执行结果:FAIL".format(case.case_id))
            logger.error("断言出错了".format(e))
            raise e
class LogInTest(unittest.TestCase):
    '这是测试登陆接口的类'
    # 使用doexcel_study中的方法调用
    do_excel_login = DoExcel(contants.excel_file)  # 传入do_excel_study.xlsx
    cases_login = do_excel_login.read_excel("login")  # 读取login_sheet

    @classmethod  # 为什么用类方法? 整个类只执行一次!
    def setUpClass(cls):  # 每个测试类里面去运行的操作都放到类方法里面
        cls.request = Request()  # 实例化对象

    def setUp(self):
        # self.write_register = DoExcel(contants.excel_file, "login") # 创建一个对象写入
        logger.info("开始执行用例")

    def tearDown(self):
        logger.info("用例执行结束")

    @classmethod
    def tearDownClass(cls):
        cls.request.session.close()  # 关闭session请求
        # cls.mysql.close()  # 关闭数据库连接

    @data(*cases_login)
    def test_login(self, case):  # 测试注册
        logger.info("开始执行第{}条用例: {}".format(case.case_id, case.title))
        logger.debug('url:{}'.format(case.url))
        logger.debug('data:{}'.format(case.data))
        logger.debug('method:{}'.format(case.method))
        logger.debug('expected:{}'.format(case.expected))

        login_data_new = Context.replace(case.data, login_information)
        resp = self.request.request(case.method, case.url, login_data_new)

        try:
            # if case.expected == '20102':
            #     self.request.request = mock.Mock(return_value='20102')
            #     self.assertEqual(case.expected, self.request.request)
            # else:
            # self.assertEqual(json.loads(case.expected)['msg'], json.loads(resp.text)['msg'])

            self.assertEqual(json.loads(case.expected), json.loads(resp.text))
            self.do_excel_login.write_excel('login', case.case_id + 1, resp.text, 'PASS')  # 读取sheet,写入结果
            logger.info("第{0}用例执行结果:PASS".format(case.case_id))
        except AssertionError as e:
            self.do_excel_login.write_excel('login', case.case_id + 1, resp.text, 'FAIL')
            logger.error("第{0}用例执行结果:FAIL".format(case.case_id))
            logger.error("断言出错了".format(e))
            raise e
class MemberListTest(unittest.TestCase):
    '这是测试登陆接口的类'
    # 使用doexcel_study中的方法调用
    do_excel = DoExcel(contants.excel_file)  # 传入do_excel_study.xlsx
    cases_member_list = do_excel.read_excel("member_list")  # 读取login_sheet

    @classmethod  # 为什么用类方法? 整个类只执行一次!
    def setUpClass(cls):  # 每个测试类里面去运行的操作都放到类方法里面
        cls.request = Request()  # 实例化对象

    def setUp(self):
        # self.write_register = DoExcel(contants.excel_file, "member_list") # 创建一个对象写入
        logger.info("开始执行用例")

    def tearDown(self):
        logger.info("用例执行结束")

    @classmethod
    def tearDownClass(cls):
        cls.request.session.close()  # 关闭session请求

    @data(*cases_member_list)
    def test_member_list(self, case):  # 测试注册
        logger.info("开始执行第{}条用例: {}".format(case.case_id, case.title))
        logger.debug('url:{}'.format(case.url))
        logger.debug('data:{}'.format(case.data))
        logger.debug('method:{}'.format(case.method))
        logger.debug('expected:{}'.format(case.expected))

        member_list_data_new = Context.replace_new(case.data)  # 调用类的方法替换参数
        resp = self.request.request(case.method, case.url,
                                    member_list_data_new)

        try:
            self.assertEqual(
                json.loads(case.expected)['code'],
                resp.json()['code'], "member_list error")
            self.do_excel.write_excel('member_list', case.case_id + 1,
                                      resp.text, 'PASS')  # 读取sheet,写入结果
            logger.info("第{0}用例执行结果:PASS".format(case.case_id))
        except AssertionError as e:
            self.do_excel.write_excel('member_list', case.case_id + 1,
                                      resp.text, 'FAIL')
            logger.error("第{0}用例执行结果:FAIL".format(case.case_id))
            logger.error("断言出错了".format(e))
            raise e
class RechargeTest(unittest.TestCase):
    '这是测试充值接口的类'
    # 使用doexcel_study中的方法调用
    do_excel = DoExcel(contants.excel_file)  # 传入do_excel_study.xlsx
    cases_recharge = do_excel.read_excel("recharge")  # 读取register_sheet

    @classmethod  # 为什么用类方法? 整个类只执行一次!
    def setUpClass(cls):  # 每个测试类里面去运行的操作都放到类方法里面
        cls.request = Request()  # 实例化对象

    def setUp(self):
        # self.write_recharge = DoExcel(contants.excel_file, "recharge") # 创建一个对象写入
        logger.info("开始执行用例")

    def tearDown(self):
        logger.info("用例执行结束")

    @classmethod
    def tearDownClass(cls):
        cls.request.session.close()  # 关闭session请求
        cls.mysql.close()  # 关闭数据库连接

    mysql = MysqlUtil_double(return_dict=True)

    @data(*cases_recharge)
    def test_recharge(self, case):  # 测试注册
        logger.info("开始执行第{}条用例: {}".format(case.case_id, case.title))
        logger.debug('url:{}'.format(case.url))
        logger.debug('data:{}'.format(case.data))
        logger.debug('method:{}'.format(case.method))
        logger.debug('expected:{}'.format(case.expected))

        # recharge_data_new = Context.replace(case.data, recharge_information)
        recharge_data_new = Context.replace_new(case.data)  # 调用类的方法替换参数
        resp = self.request.request(case.method, case.url, recharge_data_new)

        try:
            self.assertEqual(json.loads(case.expected)['msg'], json.loads(resp.text)['msg'])
            if json.loads(resp.text)['msg'] == '登录成功':
                sql = "'select * from future.member where mobilephone = '{0}'".format(json.loads(recharge_data_new)['mobilephone'])
                results = self.mysql.fetch_all(sql)
                member = results[0]  # 获取到这一条数据,是一个字典
                # 首先判断是否有成功插入数据
                # self.assertEqual(1, len(results))
                # old_recharge = member['LeaveAmount']
                # print(old_recharge)
                with open(contants.recharge_test_file, 'w') as r:  # 登陆成功后,写入member初始leaveamount的值
                    r.write(str(member['LeaveAmount']))
            if resp.json()['msg'] == '充值成功':
                sql2 = 'select * from future.member where mobilephone = {0}' \
                    .format(json.loads(recharge_data_new)['mobilephone'])
                results2 = self.mysql.fetch_all(sql2)
                member2 = results2[0]
                updated_recharge = member2['LeaveAmount']  # 充值成功后获取最新的leaveamount值
                with open(contants.recharge_test_file, 'r') as r:
                    origin_recharge = r.read()
                import decimal
                origin_recharge_decimal = decimal.Decimal(origin_recharge)
                add = (updated_recharge - origin_recharge_decimal)
                setup_amount = json.loads(recharge_data_new)['amount']  # 取出设置的金额转成十进制数字
                setup_amount_decimal = decimal.Decimal(setup_amount)
                self.assertEqual(setup_amount_decimal, add)  # 与设置的提现金额进行比对
            self.do_excel.write_excel('recharge', case.case_id + 1, resp.text, 'PASS')  # 读取sheet,写入结果
            logger.info("第{0}用例执行结果:PASS".format(case.case_id))
        except AssertionError as e:
            self.do_excel.write_excel('recharge', case.case_id + 1, resp.text, 'FAIL')
            logger.error("第{0}用例执行结果:FAIL".format(case.case_id))
            logger.error("断言出错了".format(e))
            raise e
Esempio n. 7
0
class RegisterTest(unittest.TestCase):
    '这是测试注册接口的类'
    # 使用doexcel_study中的方法调用
    do_excel = DoExcel(contants.excel_file)  # 传入do_excel_study.xlsx
    cases_register = do_excel.read_excel("register")  # 读取register_sheet

    @classmethod  # 为什么用类方法? 整个类只执行一次!
    def setUpClass(cls):  # 每个测试类里面去运行的操作都放到类方法里面
        cls.request = Request()  # 实例化对象

    def setUp(self):
        # write_register = self.do_excel.write_excel("register")  # 创建一个对象写入
        logger.info("开始执行用例")

    def tearDown(self):
        logger.info("用例执行结束")

    @classmethod
    def tearDownClass(cls):
        cls.request.session.close()  # 关闭session请求
        cls.mysql.close()  # 关闭数据库连接

    # 测试一下放在setup里面效果 是不行的
    mysql = MysqlUtil_double(return_dict=True)
    sql = "select max(mobilephone) as max_phone from future.member"
    max = mysql.fetch_one(sql)['max_phone']  # 执行SQL,并且返回最近的一条数据,是元祖,使用下标取第一个值
    # print(max)

    @data(*cases_register)
    def test_register(self, case):  # 测试注册
        logger.info("开始执行第{}条用例: {}".format(case.case_id, case.title))
        logger.debug('url:{}'.format(case.url))
        logger.debug('data:{}'.format(case.data))
        logger.debug('method:{}'.format(case.method))
        logger.debug('expected:{}'.format(case.expected))
        data_dict = json.loads(case.data)
        if data_dict['mobilephone'] == '#@mobilephone':
            # 取最大电话号码+1
            data_dict['mobilephone'] = int(self.max) + 1
        # resp = self.request.request(case.method, case.url, data_dict)

        data_dict = json.dumps(data_dict)  # 把字典转换成字符串传入context进行转换
        # print(data_dict, type(data_dict))
        register_data_new = Context.replace_new(data_dict)
        resp = self.request.request(case.method, case.url, register_data_new)

        try:
            self.assertEqual(case.expected, resp.text, 'register error')
            if resp.json()['msg'] == '注册成功':
                sql = 'select * from future.member where mobilephone = {0}'\
                    .format(json.loads(data_dict)['mobilephone'])
                results = self.mysql.fetch_all(sql)
                # 首先判断是否有成功插入数据
                self.assertEqual(1, len(results))
                member = results[0]  # 获取到这一条数据,是一个字典
                self.assertEqual(0, member['LeaveAmount'])  # 判断注册成功余额应该是0
                # print(member['LeaveAmount'],type(member['LeaveAmount']))
                self.assertEqual(1, member['Type'])  # 判断注册用户类型是1
                # print(member['Type'])
                self.assertNotEqual(
                    json.loads(register_data_new)['pwd'],
                    member['Pwd'])  # 判断密码是否加密
                if 'regname' in json.loads(register_data_new).keys():
                    self.assertEqual(
                        json.loads(register_data_new)['regname'],
                        member['RegName'])
                else:
                    self.assertEqual('小蜜蜂', member['RegName'])
            # 一致就写入Excel的结果为PASS,并且
            self.do_excel.write_excel('register', case.case_id + 1, resp.text,
                                      'PASS')  # 读取sheet,写入结果
            logger.info("第{0}用例执行结果:PASS".format(case.case_id))
        except AssertionError as e:
            self.do_excel.write_excel('register', case.case_id + 1, resp.text,
                                      'FAIL')
            logger.error("第{0}用例执行结果:FAIL".format(case.case_id))
            logger.error("断言出错了".format(e))
            raise e
Esempio n. 8
0
class AddTest(unittest.TestCase):
    '这是测试管理员新增项目接口的类'
    # 使用doexcel_study中的方法调用
    do_excel = DoExcel(contants.excel_file)  # 传入do_excel_study.xlsx
    cases_add = do_excel.read_excel("add")  # 读取add_sheet

    @classmethod  # 为什么用类方法? 整个类只执行一次!
    def setUpClass(cls):  # 每个测试类里面去运行的操作都放到类方法里面
        cls.request = Request()  # 实例化对象

    def setUp(self):
        # self.write_register = DoExcel(contants.excel_file, "add") # 创建一个对象写入
        logger.info("开始执行用例")

    def tearDown(self):
        logger.info("用例执行结束")

    @classmethod
    def tearDownClass(cls):
        cls.request.session.close()  # 关闭session请求
        cls.mysql.close()  # 关闭数据库连接

    mysql = MysqlUtil_double(return_dict=True)

    @data(*cases_add)
    def test_add(self, case):  # 测试注册
        logger.info("开始执行第{}条用例: {}".format(case.case_id, case.title))
        logger.debug('url:{}'.format(case.url))
        logger.debug('data:{}'.format(case.data))
        logger.debug('method:{}'.format(case.method))
        logger.debug('expected:{}'.format(case.expected))

        add_data_new = Context.replace(case.data, add_information)
        resp = self.request.request(case.method, case.url, add_data_new)

        try:
            self.assertEqual(json.loads(case.expected)['msg'], json.loads(resp.text)['msg'])

            if resp.json()['msg'] == '加标成功':
                sql = "select * from future.loan where memberid='{0}' " \
                      "order by createTime desc limit 1".format(json.loads(add_data_new)['memberId'])
                results = self.mysql.fetch_all(sql)
                # 首先判断是否有成功插入数据
                self.assertEqual(1, len(results))
                add_loan = results[0]  # 获取到这一条数据,是一个字典
                # print(add_loan)
                self.assertEqual(1, add_loan['Status'])  # 判断加标成功状态是1
                self.assertEqual(json.loads(add_data_new)['title'], add_loan['Title'])  # 判断加标标题
                self.assertEqual(int(json.loads(add_data_new)['amount']), add_loan['Amount'])  # 判断加标金额
                # self.assertEqual(int(json.loads(add_data_new)['loanRate']), add_loan['LoanRate'])  # 判断加标LoanRate
                self.assertEqual(int(json.loads(add_data_new)['loanTerm']), add_loan['LoanTerm'])  # 判断加标LoanTerm
                self.assertEqual(int(json.loads(add_data_new)['loanDateType']), add_loan['LoanDateType'])  # 判断加标LoanDataType
                self.assertEqual(int(json.loads(add_data_new)['repaymemtWay']), add_loan['RepaymemtWay'])  # 判断加标RepaymentWay
                # self.assertEqual(json.loads(add_data_new)['biddingDays'], add_loan['BiddingDays'])  # 判断加标BiddingDays
            self.do_excel.write_excel('add', case.case_id + 1, resp.text, 'PASS')  # 读取sheet,写入结果
            logger.info("第{0}用例执行结果:PASS".format(case.case_id))
        except AssertionError as e:
            self.do_excel.write_excel('add', case.case_id + 1, resp.text, 'FAIL')
            logger.error("第{0}用例执行结果:FAIL".format(case.case_id))
            logger.error("断言出错了".format(e))
            raise e
Esempio n. 9
0
class TestInvest(unittest.TestCase):
    '这是测试投资流程接口的类'
    do_excel = DoExcel(contants.excel_file)  # 传入do_excel_study.xlsx
    cases_invest = do_excel.read_excel("invest")  # 读取invest_sheet

    @classmethod  # 为什么用类方法? 整个类只执行一次!
    def setUpClass(cls):  # 每个测试类里面去运行的操作都放到类方法里面
        print("\m这是一个类方法")
        cls.request = Request()  # 实例化对象
        cls.mysql = MysqlUtil()

    def setUp(self):  # 每个测试方法里面去运行的操作都放到类方法里面
        logger.debug("这是一个setUp")
        logger.info("开始执行用例")

    def tearDown(self):
        logger.info("用例执行结束")

    @classmethod
    def tearDownClass(cls):
        cls.request.session.close()  # 关闭session请求
        cls.mysql.close()  # 关闭数据库连接

    @data(*cases_invest)
    def test_invest(self, case):  # 测试注册
        logger.info("开始执行第{}条用例: {}".format(case.case_id, case.title))
        logger.debug('url:{}'.format(case.url))
        logger.debug('data:{}'.format(case.data))
        logger.debug('method:{}'.format(case.method))
        logger.debug('expected:{}'.format(case.expected))

        # 查找参数化的测试数据,动态替换
        data_new = Context.replace_new(case.data)  # Str测试数据
        # 使用封装好的request 来完成请求
        resp = self.request.request(case.method, case.url, data_new)
        try:
            # self.assertEqual(str(case.expected), resp.json()['code'], "invest error")
            self.assertEqual(
                json.loads(case.expected)['code'],
                resp.json()['code'], "invest error")
            # self.assertEqual(case.expected, json.loads(resp.text)['code'], "invest error")
            # 一致就写入Excel的结果为PASS,并且
            self.do_excel.write_excel('invest', case.case_id + 1, resp.text,
                                      "Pass")  # 写入测试实际结果
            logger.info('第{}条用例执行结果:PASS'.format(case.case_id))

            # 判断是否加标成功,如果成功就按照借款人ID去数据库查询最新的加标的记录
            if resp.json()['msg'] == '加标成功':
                loan_member_id = getattr(Context, 'loan_member_id')
                sql = "select id from future.loan where memberid='{0}' " \
                      "order by createTime desc limit 1".format(loan_member_id)
                loan_id = self.mysql.fetch_one(sql)[0]
                setattr(Context, 'loan_id',
                        str(loan_id))  # 和excel中的名字保持一致, 记得转成字符串str,后续要通过正则替换

        except AssertionError as e:
            self.do_excel.write_excel('invest', case.case_id + 1, resp.text,
                                      "Failed")  # 写入测试实际结果
            logger.error('第{}条用例执行结果:FAIL'.format(case.case_id))
            logger.error("断言出错了".format(e))
            raise e
Esempio n. 10
0
class WithDrawTest(unittest.TestCase):
    '这是测试提现接口的类'
    # 使用doexcel_study中的方法调用
    do_excel = DoExcel(contants.excel_file)  # 传入do_excel_study.xlsx
    cases_withdraw = do_excel.read_excel("withdraw")  # 读取withdraw_sheet

    @classmethod  # 为什么用类方法? 整个类只执行一次!
    def setUpClass(cls):  # 每个测试类里面去运行的操作都放到类方法里面
        cls.request = Request()  # 实例化对象
        # cls.origin_withdraw = None
        # cls.updated_withdraw = None

    def setUp(self):
        # self.write_withdraw = DoExcel(contants.excel_file, "withdraw") # 创建一个对象写入
        logger.info("开始执行用例")

    def tearDown(self):
        logger.info("用例执行结束")

    @classmethod
    def tearDownClass(cls):
        cls.request.session.close()  # 关闭session请求
        cls.mysql.close()  # 关闭数据库连接

    mysql = MysqlUtil_double(return_dict=True)

    @data(*cases_withdraw)
    def test_withdraw(self, case):  # 测试注册
        logger.info("开始执行第{}条用例: {}".format(case.case_id, case.title))
        logger.debug('url:{}'.format(case.url))
        logger.debug('data:{}'.format(case.data))
        logger.debug('method:{}'.format(case.method))
        logger.debug('expected:{}'.format(case.expected))

        withdraw_data_new = Context.replace_new(case.data)
        resp = self.request.request(case.method, case.url, withdraw_data_new)

        try:
            self.assertEqual(
                json.loads(case.expected)['msg'],
                json.loads(resp.text)['msg'])
            if resp.json()['msg'] == '登录成功':
                sql = 'select * from future.member where mobilephone = {0}'\
                    .format(json.loads(withdraw_data_new)['mobilephone'])
                results = self.mysql.fetch_all(sql)
                # # 首先判断是否有成功插入数据
                # self.assertEqual(1, len(results))
                member = results[0]  # 获取到这一条数据,是一个字典
                # global origin_withdraw
                # origin_withdraw = member['LeaveAmount']
                # print('origin_withdraw', origin_withdraw)
                with open(contants.withdraw_test_file,
                          'w') as r:  # 登陆成功后,写入member初始leaveamount的值
                    r.write(str(member['LeaveAmount']))
                # setattr(Context, 'origin_withdraw', origin_withdraw)  # 设置反射不行
            if resp.json()['msg'] == '取现成功':
                # global origin_withdraw
                # origin_withdraw = getattr(Context, 'origin_withdraw')
                sql2 = 'select * from future.member where mobilephone = {0}' \
                    .format(json.loads(withdraw_data_new)['mobilephone'])
                results2 = self.mysql.fetch_all(sql2)
                member2 = results2[0]  # 获取到这一条数据,是一个字典
                updated_withdraw = member2[
                    'LeaveAmount']  # 提现成功后获取最新的leaveamount值
                with open(contants.withdraw_test_file, 'r') as r:
                    origin_withdraw = r.read()
                # print('origin_withdraw1', origin_withdraw, type(origin_withdraw))
                import decimal
                origin_withdraw_decimal = decimal.Decimal(origin_withdraw)
                # print('origin_withdraw11', origin_withdraw11, type(origin_withdraw11))
                abs = (origin_withdraw_decimal - updated_withdraw)
                # print('abs', abs)
                # print('updated_withdraw', updated_withdraw, type(updated_withdraw))
                # self.assertEqual(10.00, abs)
                setup_amount = json.loads(withdraw_data_new)[
                    'amount']  # 取出设置的金额转成十进制数字
                # print(setup_amount,type(setup_amount))
                setup_amount_decimal = decimal.Decimal(setup_amount)
                # print(setup_amount_decimal)
                self.assertEqual(setup_amount_decimal, abs)  # 与设置的提现金额进行比对

                # print('withdraw_data_new', withdraw_data_new, type(withdraw_data_new))
                # first = decimal.Decimal(withdraw_data_new)  # 有问题
                # print('first', first, type(first))
            self.do_excel.write_excel('withdraw', case.case_id + 1, resp.text,
                                      'PASS')  # 读取sheet,写入结果
            logger.info("第{0}用例执行结果:PASS".format(case.case_id))
        except AssertionError as e:
            self.do_excel.write_excel('withdraw', case.case_id + 1, resp.text,
                                      'FAIL')
            logger.error("第{0}用例执行结果:FAIL".format(case.case_id))
            logger.error("断言出错了".format(e))
            raise e
Esempio n. 11
0
 def setup(self):
     self.write = DoExcel('../datas/luckytest.xlsx', 'login')  # 创建一个对象写入
     print("开始执行用例")
Esempio n. 12
0
# coding: utf-8
# 当前项目的名称: python13-api-test
# 新文件名称:api_test
# 当前登录名:LuckyLu
# 创建日期:2019/1/17 15:45
# 文件IDE名称:PyCharm

import unittest

from common.do_excel_study import DoExcel  # 导入excel
from ddt import ddt, data
from test_cases.study.api_method import ApiMethod  # 导入api接口

cases = DoExcel('../datas/luckytest.xlsx', 'login').read_excel()


@ddt
class TestApiMethod(unittest.TestCase):
    '这是测试接口的类'

    def setup(self):
        self.write = DoExcel('../datas/luckytest.xlsx', 'login')  # 创建一个对象写入
        print("开始执行用例")

    def tearDown(self):
        print("用例执行结束")

    # def test_register(self):  # 测试注册
    #     pass

    @data(*cases)