Beispiel #1
0
    def test_register(self, case):
        logger.info('开始测试:{0}'.format(case.title))

        #参数化方式一:使用动态值进行参数化
        if case.data.find('register_mobile') > -1:
            #self.mysql = do_mysql.DoMysql()
            # #使用手机号最大号码+1获取登录手机号码
            # sql = 'select max(mobilephone) from future.member;'
            # max_phone = self.mysql.fetch_one(sql)[0]
            # max_phone = int(max_phone)+1
            # print("最大手机号码",max_phone)
            # case.data=case.data.replace('register_mobile',str(max_phone))#替换参数值
            #使用随机数生成电话号码
            num_start = [
                '134', '135', '136', '137', '138', '139', '150', '151', '152',
                '158', '159', '157', '182', '187', '188', '147', '130', '131',
                '132', '155', '156', '185', '186', '133', '153', '180', '189'
            ]
            start = random.choice(num_start)
            end = ''.join(random.sample(string.digits, 8))
            random_phone = start + end
            logger.debug('注册号码:{0}'.format(random_phone))

            case.data = case.data.replace('register_mobile',
                                          str(random_phone))  # 替换参数值
            #self.mysql.close()#关闭数据库
            # 数据库校验,注册之前查询member表用户个数
            if case.sql is not None:
                sql = case.sql
                member = self.mysql.fetch_one(sql)
                before = member['count(*)']
                logger.debug('注册前用户数:{0}'.format(before))
Beispiel #2
0
 def test_invest(self, case):
     logger.info('开始测试:{0}'.format(case.title))
     # 请求之前,判断是否需要执行SQL
     if case.sql is not None:
         sql = eval(case.sql)['sql1']
         member = self.mysql.fetch_one(sql)
         before = member['leaveamount']
         logger.debug("before:{0}".format(before))
Beispiel #3
0
 def test_add(self,case):
     logger.info('开始测试:{0}'.format(case.title))
     # 数据库校验,注册之前查询member表用户个数
     if case.sql is not None:
         sql = case.sql
         member = self.mysql.fetch_one(sql)
         before = member['count(*)']
         logger.debug("before:{0}".format(before))
Beispiel #4
0
class InvestTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, 'invest')
    cases = excel.get_cases()

    @classmethod
    def setUpClass(cls):
        logger.info('准备测试前置!')
        cls.http_request = http_request.HttpRequest2()
        cls.mysql = do_mysql.DoMysql()

    @data(*cases)
    def test_invest(self, case):
        logger.info('开始测试:{0}'.format(case.title))
        # 请求之前,判断是否需要执行SQL
        if case.sql is not None:
            sql = eval(case.sql)['sql1']
            member = self.mysql.fetch_one(sql)
            before = member['leaveamount']
            logger.debug("before:{0}".format(before))

        # 在请求之前替换参数化的值
        case.data = context.replace(case.data)
        # print("请求data:",case.data)
        resp = self.http_request.request(case.method, case.url,
                                         eval(case.data))
        actual_code = resp.json()['code']
        # 断言
        try:
            if case.expected:
                self.assertEqual(str(case.expected), actual_code)
            else:
                self.assertEqual(case.expected, actual_code)
            self.excel.write_result(case.case_id + 1, resp.text, 'PASS')
            # 判断加标成功之后,查询数据库,取到loan_id
            if resp.json()['msg'] == "加标成功":
                sql = "SELECT id from future.loan WHERE MemberID = 635 ORDER BY CreateTime DESC LIMIT 1"
                loan_id = self.mysql.fetch_one(sql)['id']
                logger.debug('新加的标的ID:{0}'.format(loan_id))
                # 保存到类属性里面
                setattr(Context, "loan_id", str(loan_id))
        except Exception as e:
            self.excel.write_result(case.case_id + 1, resp.text, 'FAIL')
            logger.error('断言出错:{0}'.format(e))
            raise e
        # 成功之后,判断是否需要执行SQL
        if case.sql is not None:
            sql = eval(case.sql)['sql1']
            member = self.mysql.fetch_one(sql)
            after = member['count(*)']
            logger.debug('after:{0}'.format(after))
            invest_amount = int(eval(case.data)["amount"])  # 投资金额
            # 数据库校验
            self.assertEqual(before - invest_amount, after)
Beispiel #5
0
class RechargeTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, 'recharge')
    cases = excel.get_cases()

    @classmethod
    def setUpClass(cls):
        logger.info('准备测试前置!')
        cls.http_request = http_request.HttpRequest2()
        cls.mysql = DoMysql()

    # @classmethod
    # def setUp(cls):
    #     cls.mysql = DoMysql()

    @data(*cases)
    def test_recharge(self, case):
        logger.info('开始测试:{0}'.format(case.title))
        # 请求之前,判断是否需要执行SQL
        if case.sql is not None:
            sql = eval(case.sql)['sql1']
            member = self.mysql.fetch_one(sql)
            before = member['leaveamount']
            logger.debug('充值前余额:{0}'.format(before))

        # 在请求之前替换参数化的值
        case.data = context.replace(case.data)
        resp = self.http_request.request(case.method, case.url,
                                         eval(case.data))
        print(resp.text)
        actual_code = resp.json()['code']
        # 断言
        try:
            if case.expected:
                self.assertEqual(str(case.expected), actual_code)
            else:
                self.assertEqual(case.expected, actual_code)
            self.excel.write_result(case.case_id + 1, resp.text, 'PASS')

        except Exception as e:
            self.excel.write_result(case.case_id + 1, resp.text, 'FAIL')
            logger.error('断言出错:{0}'.format(e))
            raise e
        # 成功之后,判断是否需要执行SQL
        if case.sql is not None:
            sql = eval(case.sql)['sql1']
            member = self.mysql.fetch_one(sql)
            after = member['leaveamount']
            logger.debug('充值后余额:{0}'.format(after))
            recharge_amount = int(eval(case.data)["amount"])  # 充值金额
            logger.debug('充值金额:{0}'.format(recharge_amount))
            #数据库校验
            self.assertEqual(before + recharge_amount, after)
Beispiel #6
0
class AddTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, 'add')
    cases = excel.get_cases()

    @classmethod
    def setUpClass(cls):
        logger.info('准备测试前置!')
        cls.http_request = http_request.HttpRequest2()
        cls.mysql = DoMysql()

    @data(*cases)
    def test_add(self,case):
        logger.info('开始测试:{0}'.format(case.title))
        # 数据库校验,注册之前查询member表用户个数
        if case.sql is not None:
            sql = case.sql
            member = self.mysql.fetch_one(sql)
            before = member['count(*)']
            logger.debug("before:{0}".format(before))

        # #参数化方式二,结合配置文件进行参数化
        # case.data = eval(case.data)#转成字典
        # if  case.data.__contains__('mobilephone') and case.data['mobilephone']== 'normal_user':
        #     case.data['mobilephone']=config.get('data','normal_user')
        # if case.data.__contains__('pwd') and case.data['pwd']== 'normal_pwd':
        #     case.data['pwd']=config.get('data','normal_pwd')
        # if case.data.__contains__('memberId') and case.data['memberId']== 'loan_memberId':
        #     case.data['memberId']=config.get('data','loan_memberId')

        #参数化方式三:正则表达式替换
        case.data = eval(context.replace(case.data))

        resp = self.http_request.request(case.method,case.url,case.data)

        actual_code = resp.json()['code']
        # 断言
        try:
            if case.expected:
                self.assertEqual(str(case.expected), actual_code)
            else:
                self.assertEqual(case.expected, actual_code)
            self.excel.write_result(case.case_id + 1, resp.text, 'PASS')
        except Exception as e:
            self.excel.write_result(case.case_id + 1, resp.text, 'FAIL')
            logger.error('断言出错:{0}'.format(e))
            raise e
            # 数据库校验
            # 成功之后,判断是否需要执行SQL
        if case.sql is not None:
            sql = case.sql
            member = self.mysql.fetch_one(sql)
            after = member['count(*)']
            logger.debug('after:{0}'.format(after))
            self.assertEqual(before + 1, after)
Beispiel #7
0
 def request(self, method, url, data=None, json=None):
     # url拼接
     url = config.get("api", "pre_url") + url
     logger.debug('请求url:{0}'.format(url))
     logger.debug('请求data:{0}'.format(data))
     method = method.upper()  # 将method强制转成全大写
     if method == 'GET':
         resp = self.session.request(method=method, url=url, params=data)
     elif method == 'POST':
         if json:
             resp = self.session.request(method=method, url=url, json=json)
         else:
             resp = self.session.request(method=method, url=url, data=data)
     else:
         resp = None
         logger.error('UN-support method')
     logger.debug('请求response:{0}'.format(resp.text))
     return resp
Beispiel #8
0
                before = member['count(*)']
                logger.debug('注册前用户数:{0}'.format(before))

        resp = self.http_request.request(case.method, case.url,
                                         eval(case.data))
        actual = resp.text
        # 断言
        try:
            self.assertEqual((case.expected), actual)
            self.excel.write_result(case.case_id + 1, actual, 'PASS')

        except Exception as e:
            self.excel.write_result(case.case_id + 1, actual, 'FAIL')
            logger.error('断言出错:{0}'.format(e))
            raise e
        #数据库校验
        # 成功之后,判断是否需要执行SQL
        if case.sql is not None:
            sql = case.sql
            member = self.mysql.fetch_one(sql)
            after = member['count(*)']
            logger.debug('注册后用户数:{0}'.format(after))
            self.assertEqual(before + 1, after)
        logger.info('结束测试:{0}'.format(case.title))

    @classmethod
    def tearDownClass(cls):
        logger.info('测试后置处理!')
        cls.mysql.close()
        cls.http_request.close()