在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中是不能直接用的,另一方面,如果原始方法发生变化,只需要改动自己封装的方法,便于维护。

实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。