Beispiel #1
0
class TestRegister(unittest.TestCase):
    excel = ReadExcel(case_path, 'register')
    case_data = excel.read_excel()
    http = HandleRequest()
    db=Hande_DB()

    @data(*case_data)
    def test_register(self, case):
        # 准备测试数据
        # 拼接完整的接口地址
        url = conf.get('env', 'url') + '/member/register'
        # 请求方法
        method = case['method']
        if '#phone#' in case['data']:
            phone = self.random_phone()
            case['data'] = case['data'].replace('#phone#', phone)
        data = eval(case['data'])

        expected = eval(case['expected'])
        headers = eval(conf.get('env', 'headers'))
        row = case['case_id'] + 1
        # 2、发送请求
        response = self.http.send(url=url, method=method, json=data, headers=headers)
        res = response.json()

        # 3、比对预期结果与实际结果
        try:
            self.assertEqual(expected['code'], res['code'])
            self.assertEqual(expected['msg'], res['msg'])
            if case['check_sql']:
                print(case['check_sql'],type(case['check_sql']))
                sql=case['check_sql'].format(conf.get('env','phone'))
                count=self.db.count(sql)
                self.assertEqual(count,1)
        except AssertionError as e:
            self.excel.write_excel(row=row, column=8, value='未通过')
            mylog.info('用例:{}----->执行未通过'.format(case['title']))
            mylog.error(e)
            print("预期结果:{}".format(expected))
            print("实际结果:{}".format(res))
            raise e
        else:
            self.excel.write_excel(row=row, column=8, value='已通过')
            mylog.info('用例:{}----->执行已通过'.format(case['title']))

    @staticmethod
    def random_phone():
        phone = '13'
        for i in range(9):
            phone += str(random.randint(0, 9))
        return phone

    @classmethod
    def tearDownClass(cls) :
        cls.db.close()
Beispiel #2
0
class TestLogin(unittest.TestCase):
    excel = ReadExcel(case_path, 'login')
    case_data = excel.read_excel()
    http = HandleRequest()

    @data(*case_data)
    def test_login(self, case):
        # 准备测试数据
        url = conf.get('env', 'url') + case['url']
        method = case['method']
        if '#phone#' in case['data']:
            case['data'] = case['data'].replace('#phone#',
                                                conf.get('env', 'phone'))
        data = eval(case['data'])
        expected = eval(case['expected'])
        headers = eval(conf.get('env', 'headers'))
        row = case['case_id'] + 1
        # 发送请求
        response = self.http.send(url=url,
                                  method=method,
                                  json=data,
                                  headers=headers)
        res = response.json()
        # 比对预期结果与实际结果
        try:
            self.assertEqual(expected['code'], res['code'])
            self.assertEqual(expected['msg'], res['msg'])
        except AssertionError as e:
            self.excel.write_excel(row=row, column=8, value='未通过')
            mylog.info('用例:{}---->执行未通过'.format(case['title']))
            mylog.error(e)
            print('预期结果:{}'.format(expected))
            print('实际结果:{}'.format(res))
            raise e
        else:
            self.excel.write_excel(row=row, column=8, value='通过')
            mylog.info('用例:{}---->执行已通过'.format(case['title']))
Beispiel #3
0
class TestRecharge(unittest.TestCase):
    excel = ReadExcel(data_path, 'recharge')
    case_data = excel.read_excel()
    http = HandleRequest()
    db = Hande_DB()

    @classmethod
    def setUpClass(cls):
        # 准备测试数据
        login_url = conf.get('env', 'url') + '/member/login'
        data = {
            'mobile_phone': conf.get('env', 'phone'),
            'pwd': conf.get('env', 'pwd')
        }
        headers = eval(conf.get('env', 'headers'))
        # 发送请求
        response = cls.http.send(url=login_url,
                                 method='post',
                                 json=data,
                                 headers=headers)
        res = response.json()
        #保存member_id
        member_id = jsonpath.jsonpath(res, '$..id')[0]
        setattr(CaseData, 'member_id', str(member_id))
        token_type = jsonpath.jsonpath(res, '$..token_type')[0]
        token = jsonpath.jsonpath(res, '$..token')[0]
        # 拼接token认证数据
        token_data = token_type + ' ' + token
        setattr(CaseData, 'token_data', token_data)

    @data(*case_data)
    def test_recharge(self, case):
        # 准备测试数据
        recharge_url = conf.get('env', 'url') + case['url']
        case['data'] = replace_data(case['data'])
        recharge_data = eval(case['data'])
        method = case['method']
        headers = eval(conf.get('env', 'headers'))
        headers['Authorization'] = getattr(CaseData, 'token_data')
        expected = eval(case['expected'])
        row = case['case_id'] + 1

        # 发送请求前查询一下账号余额
        if case['check_sql']:
            sql = case['check_sql'].format(conf.get('env', 'phone'))
            before_amount = self.db.get_one(sql)[0]

        # 发送请求,获取响应结果
        response = self.http.send(url=recharge_url,
                                  method=method,
                                  json=recharge_data,
                                  headers=headers)
        res = response.json()
        #比对预期结果与实际结果
        try:
            self.assertEqual(expected['code'], res['code'])
            self.assertEqual(expected['msg'], res['msg'])
            if case['check_sql']:
                sql = case['check_sql'].format(conf.get('env', 'phone'))
                after_amount = self.db.get_one(sql)[0]
                self.assertEqual((after_amount - before_amount),
                                 Decimal(str(recharge_data['amount'])))

        except AssertionError as e:
            self.excel.write_excel(row=row, column=8, value='未通过')
            mylog.info('用例:{}---->测试未通过'.format(case['title']))
            print('预期结果:{}'.format(expected))
            print('实际结果:{}'.format(res))
            mylog.error(e)
            raise e
        else:
            self.excel.write_excel(row=row, column=8, value='已通过')
            mylog.info('用例:{}---->测试已通过'.format(case['title']))

    @classmethod
    def tearDownClass(cls):
        cls.db.close()
Beispiel #4
0
class TestInvest(unittest.TestCase):
    excel=ReadExcel(data_path,'invest')
    cases=excel.read_excel()
    http=HandleRequest()
    db=Hande_DB()

    @data(*cases)
    def test_invest(self,case):
        pass
        # 准备用例数据
        url=conf.get('env','url')+case['url']
        # 测试数据
        case['data']=replace_data(case['data'])
        data=eval(case['data'])
        # 请求方法
        method=case['method']
        # 请求头
        headers = eval(conf.get('env', 'headers'))
        if case['interface']!='login':
            headers['Authorization']=getattr(CaseData,'token_data')
        # 预期结果
        expected=eval(case['expected'])
        # 行号
        row=case['case_id']+1

        # 发送请求
        response=self.http.send(url=url,method=method,json=data,headers=headers)
        res=response.json()

        #提取member_id和token
        if case['interface']=='login' and res['msg']=='OK':
            member_id=jsonpath.jsonpath(res,'$..id')[0]
            setattr(CaseData,'member_id',str(member_id))
            token_type=jsonpath.jsonpath(res,'$..token_type')[0]
            token = jsonpath.jsonpath(res, '$..token')[0]
            token_data=token_type+' '+token
            setattr(CaseData,'token_data',token_data)
            #提取项目id
        elif case['interface']=='add'and res['msg']:
            loan_id=jsonpath.jsonpath(res,'$..id')[0]
            setattr(CaseData,'loan_id',str(loan_id))

        # 断言
        try:
            self.assertEqual(expected['code'],res['code'])
            self.assertEqual(expected['msg'], res['msg'])
            if case['check_sql']:
                sql=replace_data(case['check_sql'])
                status=self.db.get_one(sql)[0]
                self.assertEqual(expected['status'],status)
        except AssertionError as e:
            self.excel.write_excel(row=row,column=8,value='未通过')
            mylog.info('用例:{}---->执行未通过'.format(case['title']))
            mylog.error(e)
            print('预期结果:{}'.format(expected))
            print('实际结果:{}'.format(res))
            raise e
        else:
            self.excel.write_excel(row=row,column=8,value='通过')
            mylog.info('用例:{}---->执行已通过'.format(case['title']))

    @classmethod
    def tearDownClass(cls):
        cls.db.close()
Beispiel #5
0
class TestAdd(unittest.TestCase):
    excel=ReadExcel(file_path,'add')
    cases=excel.read_excel()
    http=HandleRequest()
    db=Hande_DB()


    @data(*cases)
    def test_add(self,case):
        # 准备测试数据
        url=conf.get('env','url')+case['url']
        case['data']=replace_data(case['data'])
        data=eval(case['data'])
        headers=eval(conf.get('env','headers'))
        if case['interface'] !='登录':
            headers['Authorization']=getattr(CaseData,'token_data')
        method=case['method']
        expected=eval(case['expected'])
        row=case['case_id']+1

        # 请求前查看该用户有几条标记录
        if case['check_sql']:
            sql=case['check_sql'].format(getattr(CaseData,'admin_member_id'))
            before_count=self.db.count(sql)



        # 发送请求
        response=self.http.send(url=url,method=method,json=data,headers=headers)
        res=response.json()
        # 如果是登录接口,提取出id和token值
        if case['interface']=='登录':
            id=jsonpath.jsonpath(res,'$..id')[0]
            setattr(CaseData,'admin_member_id',str(id))
            token_type=jsonpath.jsonpath(res,'$..token_type')[0]
            token = jsonpath.jsonpath(res, '$..token')[0]
            token_data=token_type+' '+token
            setattr(CaseData,'token_data',token_data)


        # 断言
        try:
            self.assertEqual(expected['code'], res['code'])
            self.assertEqual(expected['msg'], res['msg'])

            if case['check_sql']:
                sql =case['check_sql'].format(getattr(CaseData,'admin_member_id'))
                after_count = self.db.count(sql)
                self.assertEqual((after_count-before_count),1)


        except AssertionError as e:
            self.excel.write_excel(row=row, column=8, value='未通过')
            mylog.info('用例:{}---->测试未通过'.format(case['title']))
            print('预期结果:{}'.format(expected))
            print('实际结果:{}'.format(res))
            mylog.error(e)
            raise e
        else:
            self.excel.write_excel(row=row, column=8, value='已通过')
            mylog.info('用例:{}---->测试已通过'.format(case['title']))


    @classmethod
    def tearDownClass(cls):
        cls.db.close()
Beispiel #6
0
class TestWithdraw(unittest.TestCase):
    excel = ReadExcel(data_path, 'withdraw')
    case_data = excel.read_excel()
    http = HandleRequest()
    db=Hande_DB()

    @data(*case_data)
    def test_withdraw(self, case):
        # 准备测试数据
        withdraw_url = conf.get('env', 'url') + case['url']
        case['data']=replace_data(case['data'])
        withdraw_data = eval(case['data'])
        method = case['method']
        headers = eval(conf.get('env', 'headers'))
        if case['interface'] !='登录':
            headers['Authorization'] =CaseData.token_data
        row = case['case_id'] + 1
        expected=eval(case['expected'])
        # 发送请求前查询账号余额
        if case['check_sql']:
            sql =case['check_sql'].format(conf.get('env','phone'))
            before_amount = self.db.get_one(sql)[0]

        # 发送请求,获取响应结果
        response = self.http.send(url=withdraw_url, method=method, json=withdraw_data, headers=headers)
        res = response.json()

        # 判断是登录接口还是提现接口
        if case['interface'] == '登录':
            member_id = jsonpath.jsonpath(res, '$..id')[0]

            token_type = jsonpath.jsonpath(res, '$..token_type')[0]
            token = jsonpath.jsonpath(res, '$..token')[0]
            token_data = token_type + ' ' + token
            setattr(CaseData, 'member_id', str(member_id))
            setattr(CaseData,'token_data',token_data)

        # 比对预期结果与实际结果
        try:
            self.assertEqual(expected['code'], res['code'])
            self.assertEqual(expected['msg'], res['msg'])

            if case['check_sql']:
                sql =case['check_sql'].format(conf.get('env','phone'))
                after_amount = self.db.get_one(sql)[0]
                self.assertEqual((before_amount-after_amount),Decimal(str(withdraw_data['amount'])))


        except AssertionError as e:
            self.excel.write_excel(row=row, column=8, value='未通过')
            mylog.info('用例:{}---->测试未通过'.format(case['title']))
            print('预期结果:{}'.format(expected))
            print('实际结果:{}'.format(res))
            mylog.error(e)
            raise e
        else:
            self.excel.write_excel(row=row, column=8, value='已通过')
            mylog.info('用例:{}---->测试已通过'.format(case['title']))


    @classmethod
    def tearDownClass(cls):
        cls.db.close()
Beispiel #7
0
class TestAudit(unittest.TestCase):
    excel = ReadExcel(file_path, 'audit')
    audit_data = excel.read_excel()
    http = HandleRequest()
    db=Hande_DB()

    @classmethod
    def setUpClass(cls):
        url = conf.get('env', 'url') + '/member/login'
        data = {"mobile_phone": conf.get('env', 'admin_phone'),
                "pwd": conf.get('env', 'admin_pwd')
        }
        headers = eval(conf.get('env', 'headers'))
        response = cls.http.send(url=url, method='post', json=data, headers=headers)
        res = response.json()

        # 提取token值和member_id
        id = jsonpath.jsonpath(res, '$..id')[0]
        setattr(CaseData, 'member_id', str(id))
        token_type = jsonpath.jsonpath(res, '$..token_type')[0]
        token = jsonpath.jsonpath(res, '$..token')[0]
        token_data = token_type +' ' + token
        setattr(CaseData, 'token_data', token_data)

    def setUp(self):
        url = conf.get('env', 'url') + '/loan/add'
        data= {
            "member_id":getattr(CaseData,'member_id'),
            "title":"升职加薪",
            "amount":5000,
            "loan_rate":18,
            "loan_term":12,
            "loan_date_type":1,
            "bidding_days":6
        }
        headers=eval(conf.get('env','headers'))
        headers['Authorization']=getattr(CaseData,'token_data')

        response=self.http.send(url=url,method='post',json=data,headers=headers)
        res=response.json()
        # 提取id
        loan_id=jsonpath.jsonpath(res,'$..id')[0]
        setattr(CaseData,'loan_id',str(loan_id))



    @data(*audit_data)
    def test_audit(self,case):
        # 准备测试数据
        # 准备url地址
        url=conf.get('env','url')+case['url']
        # 请求方法
        method=case['method']
        # 请求数据
        case['data']=replace_data(case['data'])
        data=eval(case['data'])
        print('data:',data)
        #配置文件获取headers
        headers=eval(conf.get('env','headers'))
        headers['Authorization']=getattr(CaseData,'token_data')

        # 预期结果
        expected=eval(case['expected'])

        row=case['case_id']+1
        # 发送请求
        response=self.http.send(url=url,method=method,json=data,headers=headers)
        res=response.json()
        print('audit_res:',res)

        if case['title']=='审核通过' and res['msg']=='OK':
            pass_loan_id=getattr(CaseData,'loan_id')
            setattr(CaseData,'pass_loan_id',pass_loan_id)

        # 断言
        try:
            self.assertEqual(expected['code'],res['code'])
            self.assertEqual(expected['msg'], res['msg'])
            if case['check_sql']:
                sql=replace_data(case['check_sql'])
                status=self.db.get_one(sql)[0]
                self.assertEqual(expected['status'],status)

        except AssertionError as e:
            self.excel.write_excel(row=row,column=8,value='未通过')
            mylog.info('用例:{}---->执行未通过'.format(case['title']))
            mylog.error(e)
            print("预期结果:{}".format(expected))
            print("实际结果:{}".format(res))
            raise e
        else:
            self.excel.write_excel(row=row,column=8,value='已通过')
            mylog.info('用例:{}---->执行已通过'.format(case['title']))