接口测试-工作心得记录七(使用assertpy库断言和利用assertpy库改写接口response的check方法)

背景:刚发完一个小版本空下来的这两天在看testerhome论坛的时候看到之前孙高飞一个帖子,测试框架的数据管理其中说到了assertJ(他是写java)的断言库真的看的特别好用,后来就找了一下发现python有一个assertpy库虽然没有assertJ功能强大,但是断言方面真的是功能强大并且好用,拿出来分享一个,说一下我的思路。

1.assertpy库

大体上先说一下assertpy这个库断言。基本的数据类型,int,string,list,dict等都是可以,并且支持流线式的连续断言,和sqlchemy写sql一样,很方便一个语句基本都断言到了并且细节方面也比unittest的断言要多的多的,更适合定制化。显示github地址拿出来:https://github.com/ActivisionGameScience/assertpy。里面有分享详细的demo,大家照着写2个就会了,很好用。举个一个官方文档一个例子:

assert_that('foo').is_length(3).starts_with('f').ends_with('oo')

ps:大家重点看一个dict,list和string类型的断言,因为接口能遇到的也就这3个。

2.断言类的重写

说完了asserpy库就简单说一下应用,其实目前我这边断言没有那么细,基本就是断言code和contains(就是assertin)而且我目前做了二次断言封装所以导入asserpy库替换一个就行了,直接上图:





上面2个图红框就是对应unittest的对应的断言。当然其实你要是用unittest的断言也可以完成测试任务,但是我觉得明明有更好的断言库,以后测试不仅仅就是接口,未来大数据,AI基本就是测试的未来,各种复杂的情况这种丰富的断言库觉得就是主流,提早用用也可以有一些更好的心得和使用想法。

3.用assertpy改造check方法

check方法主要是校验response的key,之前一直想加一个参数如果需要校验key对应value时就打开(比如说get请求返回的各种发布模板),不需要时候默认值0只校验key。昨天我调试了一下,最后结合线上case试了一下没有问题,就拿出来分享一下。

def check_response_hope_key(self,response={},hope_response={},value=0):

        #当value=0时只校验key

        if value==0:

            for n1 in hope_response:

            #print "n1:",n1

                #如果值是字典类型

                if isinstance(hope_response[n1],dict):

                    #print "dict"

                    if  not Check_Response_Hope().check_response_hope_key(response=response.get(n1), hope_response=hope_response[n1]):

                        MailFile().checkfail(response=response.get(n1), hope=hope_response[n1])

                        return False

                        raise '{},{}'.format(hope_response[n1],response[n1])

                elif isinstance(hope_response[n1],list):

                #print "list"

                    for hope_index,hope_listValue in enumerate(hope_response[n1]):

                        #print "hope_index:",hope_index

                        #print "hope_listValue:",hope_listValue

                        for response_index,response_listValue in enumerate(response[n1]):

                            #print "response_index:",response_index

                            #print "response_listValue:",response_listValue

                            if isinstance(hope_listValue,dict):

                                Check_Response_Hope().check_response_hope_key(response=response[n1][response_index], hope_response=hope_response[n1][response_index])

                            else:

                                try:

                                    print "hope_listValue:",hope_listValue,type(hope_listValue)

                                    print "response_listValue:",response_listValue,type(response_listValue)

                                    assert_that(hope_response[n1][hope_index]).is_equal_to(response[n1][hope_index])

                                except AssertionError as ex:

                                    MailFile().checkfail(response=response[n1][hope_index], hope=hope_response[n1][hope_index])

                                    raise Exception('Expected <%s> to be not equal to <%s>, but was not.' % (hope_response[n1][hope_index],response[n1][hope_index]))

                else:

                #当时sring类型

                    try:

                        assert_that(response).contains_key(n1)

                        print "n1:",n1   

                    except (AssertionError,TypeError) as ex:

                        MailFile().checkfail(response=response, hope=n1)

                        raise Exception('%s <%s> is not dict-like: missing keys()'% (response,n1))

        

        

        #校验key和value

        else:

            try:

                assert_that(hope_response).is_equal_to(response)

            except AssertionError as ex:

                MailFile().checkfail(response=response,hope=hope_response)

                raise Exception('Expected <%s> to be not equal to <%s>, but was not.' % (response,hope_response))

                                   

        return True       


图片效果不好,就吧代码粘贴过来了。校验都是用的assertpy的方法。增加了发送邮件的功能,大家可以参考一下。

思路来源:https://testerhome.com/topics/6554。写的很好大家可以借鉴一下。











  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值