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

你可能感兴趣的:(RF)