robotframework+python接口自动化的点滴记录

robotframework+python框架上写了两三天的接口自动化,做了一些笔记。

1.在断言的时候经常由于数据类型导致较验不通过,值得注意的是,在定义常量或者变量的时候,使用${}代表int类型,例如${2}就代表数字2,另一种直接写2,则是string类型的2,返回的时候会是“2”。

2.由于接口response中会出现字典格式,那在写期望值的时候,就要把一些字段拼成字典的形式,可以使用create dictionary这个基础关键字,写法是:

${expectResult}=  create dictionary  guideType  ${guideType}    options   ${options}   questionId   ${1}   questionName  ${questionName}

3.当接口需要测试几次,只是参数改变的时候,需要用到for循环,这里我用的是for-in-zip,例如:
参数定义在参数文件里
@{questionId} ${1}  ${2}  ${4}  ${6}  ${8}
@{optionId}    ${1}  ${2}  ${9}  ${13} ${18}
用例中写:
:FOR   ${questionId}  ${optionId}  IN ZIP   ${questionId}  ${optionId}
要测试的内容,for里要执行的语句前面都要写
4.还用了一种普通的for循环,写法如下:
:FOR    ${questionId} ${optionId}  IN
...    ${1}    ${1}
...    ${2}    ${2}
...    ${4}    ${9}
...    ${6}    ${13}
...    ${8}    ${18}
5. 嵌套FOR循环的写法:
:FOR  ${questionIdRange}  ${content}  IN ZIP  ${questionIdRange}  ${content}
    ${options}=   get options by question id with false   ${questionIdRange}
    内嵌FOR生成完整option  @{options}
    ${question}=  create dictionary  content  ${content}  options  ${options} 
*** Keywords ***
内嵌FOR生成完整option
[Arguments]    @{LIST}
: FOR   ${item}    IN   @{LIST}
       ${item}=    blablablabla
这里会把子循环里得到的list传到主循环的options里,主要看如何内嵌,忽略业务代码部分。

1.在循环体内,赋值语句的前后名称不能一样,否则在跑循环的第二次时就会报错:TypeError: not all arguments converted during string formatting

这样写是错的:

${设置计划接口_请求body}=  string format  ${设置计划接口_请求body}  ${cardId}  ${fundCode}  ${investmentPeriod}

这样写是对的:

${设置计划接口_请求body-new}=  string format  ${设置计划接口_请求body}  ${cardId}  ${fundCode}  ${investmentPeriod}

2.在循环体内,每次循环取值不一样的参数,则需要在body的时候使用%s,然后在用例中进行替换。

Body的写法:

*** Variables ***

${设置计划接口_请求body}

                    ...            {

                    ...                     "amount"   : "${amount}",

                    ...                     "cardId"   : "%s",

                    ...                     "fundCode" : "%s",

                    ...             "investmentPeriod" : "%s"

                    ...            }

用例中:

*** Test Cases ***

${设置计划接口_请求body-new}=  string format  ${设置发动码接口_请求body}  ${cardId}  ${fundCode}  ${investmentPeriod}

3.将金额的格式进行转变,例如将2000转为2,000.00

定义${amount}为2000

${expectAmount}=  Format number to String    {:,.2f}  ${amount}

如果最终的值需要显示成2,000.00 元,则直接在使用时后面加上元。如下:

${expectResult}=  create dictionary  amount ${expectAmount} 元  amountDesc  ${amountDesc}

4.将int型转成string。

数据库查到的值是int型的,但接口返回是string型,则可以通过如下方式转:

${planId-new}=  transfer to string  ${planId}

5.以上的string format,Format number to String,transfer to string都是封装好的关键字。

我们看其中的Format number to String的具体实现。

Format number to String

    [Arguments]   ${format}   ${number}

    ${number_string}=  format number   ${format}   ${number}

    [Return]  ${number_string}

再看一下format number方法怎么写的:

def format_number(format_str,number):

         return format_str.format(number)

实现的方式是先写了方法去调用原始的format方法,再去写个关键字调用自己写的方法。为什么要这样做?一方面是因为python的原始方法在robotframework中是不能直接用的,另一方面,如果原始方法发生变化,只需要改动自己封装的方法,便于维护。

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值