Robotframework 入门教程(六)

== resource&Library ==

在Setting表里使用Resource导入文件,resource文件语法和testcase语法大部分相同只是没有Test Cases表。支持多种文件格式,为了和testcae区分开,这里就用txt。新建pub.txt如下:
*** Settings ***
[Documentation]    demo resource

*** Variables ***
${var1}           value
@{list1}          a    b    c    d
&{dict1}          key1=sf    key2=${list1}

*** Keywords ***
Calculate and Check Equals
    [Arguments]    ${expression}=3+3    ${expected}=${6}
    ${res}    Evaluate    ${expression}
    Log to console    expression = ${expression}
    Log to console    result = ${res}
    Should Be Equal    ${res}    ${expected}

Varargs keyword
    [Arguments]    @{args}
    Log to console    --------
    : FOR    ${item}    IN    @{args}
    \    Log to console    ${item}

Kwargs keyword
    [Arguments]    &{kwargs}
    Log to console    --------
    : FOR    ${item}    IN    @{kwargs.items()}
    \    Log to console    ${item}

Calculate "${expression}" and Check Equals "${expected}"
    Calculate and Check Equals    ${expression}    ${expected}

在demo.robot导入该resource文件,里面的关键字和变量就可以直接使用了,修改demo.robot如下;
*** Settings ***
Documentation     Example test cases using the keyword-driven testing approach.
Resource          pub.txt

*** Test Cases ***
First Case
    [Documentation]    variable type
    Log to console    ${var1}
    Log to console    ${list1}[0]
    Log to console    ${list1[0]}
    Log to console    ${dict1}['key1']
    Log to console    ${dict1['key1']}
    Log to console    ${dict1.key1}

Second Case
    [Documentation]    Default values with user-keywords
    Calculate and Check Equals
    Calculate and Check Equals    1+5
    Calculate and Check Equals    7-2    ${5}
    Calculate and Check Equals    expression=6+3    expected=${9}

Third Case
    [Documentation]    Varargs with user-keywords
    Varargs keyword    a    f    ${var1}
    Varargs keyword    @{list1}    ${var1}
    Kwargs keyword    key1=value1    key2=789
    Kwargs keyword    &{dict1}
    Kwargs keyword    &{dict1}    mykey=tututu    &{dict1}

Fourth Case
    [Documentation]    data-driven case
    [Template]    Calculate "${expression}" and Check Equals "${expected}"
    2+4    ${6}
    9-2    ${7}
    6*4    ${24}

Fifth Case
    [Documentation]    builtIn library
    ${item}    Get Dictionary Keys    ${dict1}
    Log to console    ${item}
导入Library在Setting表里使用Library关键字。加入一行 “Library           Collections”,引入了库Collections,其提供的关键字就可以直接使用了。
*** Settings ***
Documentation     Example test cases using the keyword-driven testing approach.
Resource          pub.txt
Library           Collections
testcase表中加入一条case调用Collections中的 Get Dictionary Keys。
*** Test Cases ***
Fifth Case
    ${item}    Get Dictionary Keys    ${dict1}
    log    ${item}

支持的库可见官网 http://robotframework.org/#libraries
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Robotframework⼊门教程(⼀) 最近⼯作中⽤Robot Framework框架来做⾃动化,所以,花时间学习了⼀下,⽹上好的⽂档不多,⼤多都是复制粘贴如何安装环境。要么就是介绍了⼀堆RIDE 的界⾯,活活把这么强⼤的⼀个⼯具讲成了个photoshop,我⽬前⽤了1个多⽉,这个⼯具很强⼤,简单写两句。 ==安装== ⾸先其实RIDE是个好⼯具,但是安装起来真⼼费劲,当时我的MAC上安装的时候google了两天,⼀个问题接⼀个问题。 我这边呢就不⽤RIDE了,想⽤呢⾃⼰想辙安去。 安装命令: pip install robotframework ==第⼀个testcase== RF的特点什么的就不多说了,⽹上有的是,撸起袖⼦就是⼲。 新建demo.robot⽂件,内容如下。 *** Test Cases *** First Case Log Hello World! warn Second Case ${res} Evaluate 1+2+3 Should Be Equal ${res} 6 Third Case ${res} Evaluate 'i'*3 Length Should Be ${res} 运⾏ robot demo.robot。将得到如下 上⾯的例⼦有三条case,第⼀条调⽤内置关键字Log, 第⼀个参数是"Hello World! " ,第⼆个参数是"warn"。就是简单打印⼀条log,后⾯的是log的等级,默 认输出info以上的log到console,截图中可以看到first case中打印了 Hello World!等级为WARN。 第⼆条case,调⽤内置关键字Evaluate,参数是"1+2+3",结果赋值给变量res(RF的语法是⽤$和⼤括号代表标量变量,还有其他类型的变量以后再说)然后调 ⽤Should Be Equal校验结果是否等于6,因为默认是字符串格式,所以second case 执⾏结果是FAIL。 第三条case,和第⼆条类似,同样调⽤Evalutate 赋值给变量res为"iii",校验变量长度为3,执⾏结果为PASS。 最后三⾏显⽰这次执⾏结果⽣成的⽂件,output.xml,log.html,report.html。打开log⽂件如下,结果报告有什么优点就不说了,⾃⼰去看。output.xml先放放以后 再说。 前⾯说过RF中默认的都是格式是String,那么怎么给关键字传其他的类型,答案是⽤变量。修改second case如下: Second Case ${res} Evaluate 1+2+3 Should Be Equal ${res} ${6} 再次执⾏robot demo.robot 这次的结果

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值