Exemplo n.º 1
0
    def test_case(self, case):
        global result  #声明全局变量
        method = case['Method']
        url = case['Url']
        param = eval(case['Params'])
        # print(param)

        expected = eval(case['ExpectedResult'])
        #发起测试
        # print('-------正在测试{}模块里面第{}条测试用例:{}'.format(case['Module'],case['CaseId'],case['Title']))
        self.my_log.info('正在执行{}模块第{}条用例:{}'.format(case['Module'],
                                                    case['CaseId'],
                                                    case['Title']))
        self.my_log.info('参数是:{}'.format(param))
        res = HttpRequest()  #实例化
        resp = res.http_request(method, url, param, cookies=None)
        print('实际结果:{}'.format(resp.json()))  #http发送请求拿到的实际返回值
        #对比结果
        try:
            self.assertEqual(expected, resp.json())
            result = 'pass'
            self.my_log.info('该条测试用例通过')
            # print('该条测试用例通过')
        except AssertionError as e:
            result = 'failed'
            self.my_log.error('该条测试用例不通过:'.format(e))
            # print('该条测试用例不通过:{}'.format(e))
        finally:
            final_result = result
            self.my_log.info('******开始写入数据******')
            self.do_exl.write_back(
                case['CaseId'] + 1, 8,
                resp.text)  #写实际结果   #03:不同的表单写回,表单名就不能放在初始化函数中
            self.do_exl.write_back(case['CaseId'] + 1, 9, final_result)  #写测试结果
            self.my_log.info('******写入数据完毕******')
Exemplo n.º 2
0
    def test_case2(self, case_2):
        global result1  #定义为全局变量
        global cookies
        url = case_2['Url']
        param = eval(case_2['Params'])
        method = case_2['Method']
        # print(case_2['ExpectedResult'])
        expected = eval(case_2['ExpectedResult'])
        # if case_2['Params'].find('normal_phone') != -1:
        #     param['mobilephone']=getattr(GetData,'normal_phone')

        # 调用正则函数替换mobilephone,pwd:
        param = eval(get_data.replace(case_2['Params']))

        self.my_log.info('正在执行{}模块第{}条用例:{}'.format(case_2['Module'],
                                                    case_2['CaseId'],
                                                    case_2['Title']))
        self.my_log.info('参数是:{}'.format(param))
        # 拿到请求之前的账户余额
        if case_2['Sql'] != None:
            sql = eval(case_2['Sql'])['sql']
            before_LeaveAmount = DoSql().do_sql(sql)[0]

        resp = HttpRequest().http_request(method, url, param,
                                          cookies=cookies)  #执行请求
        print('实际结果是:{}'.format(resp.json()))  #发起请求的实际结果
        if resp.cookies:  # 为真,即存在cookies继续执行 --- 判断请求的cookies是否为空  ---- 其实就是判断第一个登陆用例的cookies(resp的cookies)
            cookies = resp.cookies  #将默认的cookies替换为 登陆的cookies
            # 对比结果
        try:
            # 拿到充值之后的账户余额
            if case_2['Sql'] != None:
                after_LeaveAmount = DoSql().do_sql(eval(
                    case_2['Sql'])['sql'])[0]
                recharge_amount = param['amount']
                expected_amount = before_LeaveAmount + recharge_amount
                self.assertEqual(expected_amount, after_LeaveAmount)
            if case_2['ExpectedResult'].find(
                    'expect_amount') > -1:  #判断是否替换leaveamount,存在就替换
                case_2['ExpectedResult'] = case_2['ExpectedResult'].replace(
                    'expect_amount', str(expected_amount))  #要接收替换的字符串
                # print(case_2['ExpectedResult'])

            self.assertEqual(eval(case_2['ExpectedResult']), resp.json())  #对比
            result1 = 'pass'
            self.my_log.info('该条测试用例通过')
        except AssertionError as e:
            self.my_log.error('该条测试用例不通过:{}'.format(e))
            result1 = 'failed'
        finally:
            final_result = result1
            self.my_log.info('******开始写入数据******')
            self.do_exl.write_back(case_2['CaseId'] + 1, 9, resp.text)
            self.do_exl.write_back(case_2['CaseId'] + 1, 10, final_result)
            self.my_log.info('******写入数据完毕******')
Exemplo n.º 3
0
    def test_case2(self, case_2):
        global result1
        # url_login='******'
        # param_login={'mobilephone':'18688775656','pwd':'123456'}
        # method_login='******'
        # # 获取到登录的cookies
        # cookies=HttpRequest().http_request(method_login,url_login,param_login,cookies=None).cookies
        url = case_2['Url']
        param = eval(case_2['Params'])
        method = case_2['Method']
        expected = eval(case_2['ExpectedResult'])

        # if case_2['Params'].find('normal_phone') != -1:
        #     param['mobilephone']=getattr(GetData,'normal_phone')

        # 调用正则函数替换mobilephone,pwd:
        param = eval(get_data.replace(case_2['Params']))
        # print(param)

        self.my_log.info('正在执行{}模块第{}条用例:{}'.format(case_2['Module'],
                                                    case_2['CaseId'],
                                                    case_2['Title']))
        self.my_log.info('参数是:{}'.format(param))

        resp = HttpRequest().http_request(
            method, url, param, cookies=getattr(GetData,
                                                'cookies'))  #根据反射取值cookies
        print('实际结果是:{}'.format(resp.json()))  #发起请求的实际结果
        if resp.cookies:  #为真,即存在cookies
            setattr(GetData, 'cookies', resp.cookies)  #根据反射重新赋值cookies

        # 对比结果
        try:
            self.assertEqual(expected['code'], resp.json()['code'])  #对比code
            result1 = 'pass'
            self.my_log.info('该条测试用例通过')
        except AssertionError as e:
            self.my_log.error('该条测试用例不通过:{}'.format(e))
            result1 = 'failed'
        finally:
            final_result = result1
            self.my_log.info('******开始写入数据******')
            self.do_exl.write_back(case_2['CaseId'] + 1, 8, resp.text)
            self.do_exl.write_back(case_2['CaseId'] + 1, 9, final_result)
            self.my_log.info('******写入数据完毕******')
Exemplo n.º 4
0
 def setUp(self):
     '''准备测试数据,测试前的准备工作'''
     self.do_exl = DoExcel(project_path.case_path, 'Invest')
     self.my_log = MyLog()
     self.http = HttpRequest()
Exemplo n.º 5
0
class RunCase(unittest.TestCase):
    def setUp(self):
        '''准备测试数据,测试前的准备工作'''
        self.do_exl = DoExcel(project_path.case_path, 'Invest')
        self.my_log = MyLog()
        self.http = HttpRequest()

    @data(*invest_data)
    def test_case(self, case):
        global result
        # 取到request里需要的参数
        url = case['Url']
        method = case['Method']
        expected = eval(case['ExpectedResult'])  #转换为原本的类型
        param = eval(case['Params'])

        # if case['Params'].find('normal_phone') != -1:
        #     param['mobilephone'] = getattr(GetData, 'normal_phone')
        # 调用正则函数替换mobilephone,pwd,memberid
        param = eval(get_data.replace(case['Params']))

        # 判断是否要执行sql语句,存在sql就执行
        if case['Sql'] != None:
            sql = eval(case['Sql'])['sql']
            print(eval(case['Sql']))
            print(type(eval(case['Sql'])))
            loan_id = DoSql().do_sql(sql, 1)[0]
            setattr(GetData, 'LOANID', loan_id)  #将新的loanid赋值给loan_id
        # 替换loan_id  #请求之前替换
        if 'loan_id' in case['Params']:
            # if case['Params'].find('loan_id') != -1:
            #     param=eval(case['Params'].replace('loan_id',str(getattr(GetData,'LOANID'))))  #replace 只能对字符串进行操作,要str强转
            param['id'] = getattr(GetData, 'LOANID')  #通过对字典key重新赋值来替换
        # else:
        #     param=eval(case['Params'])
        # print(param)
        # 拿到本次竞标前标的剩余金额:
        if case['Sql'] != None:
            before_LeaveAmount = DoSql().do_sql(eval(case['Sql'])['sql'], 1)[0]
            # print(before_LeaveAmount)

        # 拿到投资金额:
        # for i in bidloan_data:
        #     if i['Title'] == '竞标成功':
        #         invest_amount=eval(i['Params'])['amount']  #先将params转为字典再根据key取值
        #         print(invest_amount)

        # 准备测试
        self.my_log.info('开始执行{}模块第{}条用例:{}'.format(case['Module'],
                                                    case['CaseId'],
                                                    case['Title']))
        self.my_log.info('参数是:{}'.format(param))

        res = self.http.http_request(method,
                                     url,
                                     param,
                                     cookies=getattr(GetData, 'cookies'))
        print('实际结果是:{}'.format(res.json()))

        # 判断是否有cookies,有就将cookies重新根据反射赋值
        if res.cookies:
            setattr(GetData, 'cookies', res.cookies)
        try:
            self.assertEqual(expected['code'], res.json()['code'])
            # 拿到投资后的剩余金额:
            if case['Sql'] != None:
                sql = eval(case['Sql'])['sql']
                after_LeaveAmount = DoSql().do_sql(sql, 1)[0]
                # 期望余额与实际余额比较
                invest_amount = param['amount']
                expected_amount = before_LeaveAmount - invest_amount
                self.assertEqual(expected_amount, after_LeaveAmount)

            result = 'pass'
            self.my_log.info('该条测试用例通过')
        except AssertionError as e:
            result = 'failed'
            self.my_log.error('该条用例不通过:{}'.format(e))
            # raise e
        finally:
            final_result = result
            self.my_log.info('******开始写入数据******')
            self.do_exl.write_back(case['CaseId'] + 1, 9, res.text)  #写入实际结果
            self.do_exl.write_back(case['CaseId'] + 1, 10,
                                   final_result)  #写入测试结果
            self.my_log.info('******写入数据完毕******')