extract(中文意思索引)

用例数据设计
用例数据中新增一栏extract 表示提取响应数据并保存到对应的类属性中
在这里插入图片描述
在这里插入图片描述jsonpath
对于单接口测试如果依赖接口只需要在测试开始执行一次,那么可以将依赖接口的请求放在类级前置方法中,然后通过全局变量或者当前用例类属性来传递依赖数据

对于单接口测试如果依赖接口需要在每个用例前执行,可以将依赖接口的请求放在方法级前置方法中,然后通过用例对象属性来传递依赖数据

对于多接口的业务流测试,可以将下一个接口需要依赖的数据通过当前用例类属性来传递依赖数据

    def test_01register_normal_user(self):
        '''
        1.注册普通融资用户
        :return:
        '''
        item = {'title':'注册融资用户',
                'method':'post',
                'url':'register',
                'request_data':'{"headers": {"X-Lemonban-Media-Type": "lemonban.v1"},'
                               '"json": {"mobile_phone":$phone_number$,"pwd":"12345678"}}',
                'status_code':200,
                'res_type':'json',
                'expect_data':'{"code":0,"msg":"OK"}',
                'sql':'select id from member where mobile_phone=$phone_number$',
                'extract': '[["normal_mobile_phone","$..mobile_phone"]]'
                }
        self.flow(item)

1/首先在test_invest_flow中新建一个函数extract_data
2/判断如果有extrac,则将extract中的json数据规则转换成python对象
3.需要将转换的语句try一下,判断规则是否正确
4.因为extract是一个列表,一个用例可能会有一个或者多个参数需要传递,所以要动态的获取,所以要用到for循环
将转换后的python对象,循环;
将列表的第一个索引命名为类属性名name;“normal_mobile_phone”,
将列表的第二个索引命名为提取表达式exp;" . . m o b i l e p h o n e " 5. 从 获 得 的 响 应 中 取 值 m o b i l e p h o n e ( s e l f . r e s p o n s e . j s o n ( ) 获 得 响 应 ) , 需 要 用 到 j s o n p a t h 来 提 取 例 如 : j s o n p a t h ( d a t a , ′ ..mobile_phone" 5.从获得的响应中取值mobile_phone(self._response.json()获得响应),需要用到jsonpath来提取 例如: jsonpath(data,' ..mobilephone"5.mobilephoneself.response.json()jsonpathjsonpath(data,.store.book.*.author’)
6.如果获取到值,通过setattr()绑定到类属性中
7.否则抛出异常

    def extract_data(self):
        '''
        根据提取表达式提取对应的数据
        :return:
        '''
        if self._case.get('extract'):    #如果有extract,则执行下面的语句
            try:
                rules = json.loads(self._case.get('extract'))
                # 返回python对象,相当于'extract': '[["normal_mobile_phone","$..mobile_phone"]]
                                                           # 因为有可能规则会写错了,所以可以try
            except Exception as e :
                logger.warning('用例【{}】的extract字段数据:{}格式不正确'.format(self._case['title'],self._case['extract']))
                raise e
            for rule in rules:
                #获得类属性名
                name = rule[0]
                 #提取表达式
                exp = rule[1]
        #根据jsonpath去响应中提取值
                value = jsonpath(self._response.json(),exp)
                #如果能提取到值
                if value:
                    #把 值绑定到对应的类属性中
                    setattr(self.__class__,name,value[0])  #value是一个列表
                else:    #提取不到值,说明jsonpath 写错了,或者是响应有问题
                    raise ValueError('用例【{}】的提取表达式{}提取不到数据'.format(self._case['title'],self._case['extract']))

上面这个是第一个方法
如何断言。。。。。。。。

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
上面的res_type是等于json的

    def extract_data(self):
        '''
        根据提取表达式提取对应的数据
        :return:
        '''
        if self._case.get('extract'):
            if self._case['res_type'].lower()  ==   'json':
                self.extract_data_from_json()
            elif self._case['res_type'].lower()  ==  'html':
                self.extract_data_from_html()
            elif self._case['res_type'].lower()  ==  'xml':
                self.extract_data_from_xml()
            else:
                raise ValueError('res_type类型不正确,只支持json,html,xml')
    def extract_data_from_json(self):
        '''
        根据提取表达式提取对应的数据,并绑定要类属性中
        :return:
        '''
        if self._case.get('extract'):    #如果有extract,则执行下面的语句
            try:
                rules = json.loads(self._case.get('extract'))
                # 返回python对象,相当于'extract': '[["normal_mobile_phone","$..mobile_phone"]]
                                                           # 因为有可能规则会写错了,所以可以try
            except Exception as e :
                logger.warning('用例【{}】的extract字段数据:{}格式不正确'.format(self._case['title'],self._case['extract']))
                raise e
            for rule in rules:
                #获得类属性名
                name = rule[0]
                 #提取表达式
                exp = rule[1]
        #根据jsonpath去响应中提取值
                value = jsonpath(self._response.json(),exp)
                #如果能提取到值
                if value:
                    #把 值绑定到对应的类属性中
                    setattr(self.__class__,name,value[0])  #value是一个列表
                else:    #提取不到值,说明jsonpath 写错了,或者是响应有问题
                    raise ValueError('用例【{}】的提取表达式{}提取不到数据'.format(self._case['title'],self._case['extract']))


如何调用???
最好在断言响应结果后面调用,因为如果断言数据库失败,就提取不到响应结果了,提取不到响应结果,后面的统统不能成功了

@ddt
class SuperInvestFlow(BaseCase):
    name = '参数化的投资业务流'
    cases = get_data_from_excel(BaseCase.settings.TEST_DATA_FILE, 'invest_flow')

    @list_data(cases)
    def test_invest_flow(self, item):
        self.flow(item)

终极中级

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值