Robot Framework_002——字符串和int类型间的

一、在robot文件中定义变量,并引用变量,变量默认是字符串类型

*** Variable ***
${var1}     tom1
${var2}     1234

***Test Cases***
case1
    Log To Console  Hello World!
    Log To Console  ${var1} ${var2}

查看运行结果:


变量引用结果.png

二、在测试例case1中定义局部变量,此变量只能在case1中引用,${cv1}默认是字符串类型

*** Variable ***
${var1}     tom1
${var2}     1234

***Test Cases***
case1
    Log To Console  Hello World!
    Log To Console  ${var1} ${var2}
    # ${c1}是局部变量
    ${cv1}    Set Variable    23
    Log To Console  ${cv1}

查看运行结果:


image.png

三、在测试例case1中把局部变量${cv1},改成全局变量,变量能在其他case中引用:

*** Variable ***
${var1}     tom1
${var2}     1234


***Test Cases***
case1
    Log To Console  Hello World!
    Log To Console  ${var1} ${var2}
    # ${c1}是局部变量
    ${cv1}    Set Variable    23
    Log To Console  ${cv1}
    # ${c1}设置为全局变量
    Set Global Variable     ${cv1}        
case2
    Log To Console  ${cv1}

查看运行结果:


image.png

四、字符串转换为int类型方法

demo.py

def abc(v1):
    return v1+5;
*** Settings ***
#引入demo.py文件
Library     demo.py

*** Variable ***
${var1}     tom1
${var2}     1234


***Test Cases***
case1
    Log To Console  Hello World!
    Log To Console  ${var1} ${var2}
    # ${c1}是局部变量
    ${cv1}    Set Variable    23
    Log To Console  ${cv1}
    # ${c1}是全局变量
    Set Global Variable     ${cv1}        
case2
    Log To Console  ${cv1}

case3
    ${res}  demo.abc    ${cv1}

运行结果


运行错误.png

方法一:使用Convert To Integer 命令string类型转换为int类型

*** Settings ***
Library     demo.py

*** Variable ***
${var1}     tom1
${var2}     1234

***Test Cases***
case1
    Log To Console  Hello World!
    Log To Console  ${var1} ${var2}
    # ${c1}是局部变量
    ${cv1}    Set Variable    23
    Log To Console  ${cv1}
    # ${c1}是全局变量
    Set Global Variable     ${cv1}        
case2
    Log To Console  ${cv1}

case3
    #${cv1}转换为int类型,并赋值给${t}
    ${t}    Convert To Integer   ${cv1}
    #${t}执行demo.py中的abc方法
    ${res}  demo.abc    ${t}
    Log To Console  ${res}

测试结果:


测试结果.png

方法二:使用双层嵌套$转换为int类型

*** Settings ***
Library     demo.py

*** Variable ***
${var1}     tom1
${var2}     1234


***Test Cases***
case1
    Log To Console  Hello World!
    Log To Console  ${var1} ${var2}
    # ${c1}是局部变量
    ${cv1}    Set Variable    23
    Log To Console  ${cv1}
    # ${c1}是全局变量
    Set Global Variable     ${cv1}        
case2
    Log To Console  ${cv1}

case3
    ${t}    Convert To Integer   ${cv1}
    ${res}  demo.abc    ${t}
    Log To Console  ${res}

case4
    #双层嵌套,${${cv1}}转换string类型为int类型
    ${res}  demo.abc    ${${cv1}}
    Log To Console  ${res}

方法二测试结果


使用${${cv1}}转换string类型为int类型.png

四、int类型转换为字符串方法

demo.py内容:

def abc(v1):
    return v1+5;

def abcstr(v1):
    return v1+'string'

直接引用int执行string命令,提示错误

*** Settings ***
Library     demo.py

*** Variable ***
${var1}     tom1
${var2}     1234


***Test Cases***
case1
    Log To Console  Hello World!
    Log To Console  ${var1} ${var2}
    # ${c1}是局部变量
    ${cv1}    Set Variable    23
    Log To Console  ${cv1}
    # ${c1}是全局变量
    Set Global Variable     ${cv1}        
case2
    Log To Console  ${cv1}

case3
    ${t}    Convert To Integer   ${cv1}
    ${res}  demo.abc    ${t}
    Log To Console  ${res}

case4
    ${res}  demo.abc    ${${cv1}}
    Log To Console  ${res}

case5
    ${str_res} demo.abcstr ${res}
    Log To Console  ${str_res}

运行结果:


image.png

解决方法:使用Convert To String命令,把int类型转换为string类型

*** Settings ***
Library     demo.py

*** Variable ***
${var1}     tom1
${var2}     1234


***Test Cases***
case1
    Log To Console  Hello World!
    Log To Console  ${var1} ${var2}
    # ${cv1}是局部变量
    ${cv1}    Set Variable    23
    Log To Console  ${cv1}
    # ${cv1}是全局变量
    Set Global Variable     ${cv1}        
case2
    Log To Console  ${cv1}

case3
    ${t}    Convert To Integer   ${cv1}
    ${res}  demo.abc    ${t}
    Log To Console  ${res}

case4
    ${res}  demo.abc    ${${cv1}}
    Log To Console  ${res}
    #转换${res}为string类型
    ${str}  Convert To String   ${res}
    ${str_res}  demo.abcstr  ${str}
    Log To Console  ${str_res}

运行结果:


image.png

你可能感兴趣的:(Robot Framework_002——字符串和int类型间的)