全面学习robotframework框架二:整体理解框架结构

robotframework支持的文件格式
HTML
TSV
Plain TEXT
reStructuredText

后文将以 Plain TEXT格式,以“空格”分割,以“.robot”为文件后缀的方式来进行相关说明

robotframework目录文件结构

robotframework是以project为单位进行管理的
一个project可以包含多个Test Suite
一个Test Suite可以包含多个测试用例
一个Test Suite有四部分组成:Settings、Variables、Test Cases、Keywords

如何去执行这些脚本:

1、执行整个项目所有用例,pybot 项目路径,例如:

pybot D:\robot

2、执行某个suite用例,pybot suite路径,例如:

pybot D:\robot\testsuit.txt

3、执行某个测试用例,pybot –测试用例名 in 该测试用例所在suite,例如

pybot --testcase1_login in D:\robot\testsuit.txt

4、将测试结果输出到固定路径,pybot –outputdir 报告路径 用例路径,例如:

pybot --ouputdir D:\ropot D:robot\testsuit.txt

5、执行某个tag的测试用例,pybot –include [tag name] [项目路径],例如:

pybot --include nomal D:\robot
查看报告文件

用例执行完毕后会生成三个文件分别是:log.html、output.xml、report.html


报告截图

output.xml:记录的测试结果是XML文件,根据特定的需要可以编写脚本读取XML文件并生成特定的测试报告
log.html:会记录Robotframework运行的每一步操作,主要用于编写测试脚本的过程查看
report,html:为测试报告,整理性的展示测试用例的运行情况

整理结构如下:

总体结果及关键字说明

*** Settings ***
Documentation           这个是当前Test Suite说明文字
Library                 当前Test Suite需要使用的库
Resource                当前Test Suite需要加载使用的资源,可能是参数也可能是用例文件
Metadata                定义元数据
Variables               引用变量文件
Suite Setup             Test Suite执行前的动作
Suite Teardown          Test Suite执行后的动作
Test Setup              Test Case执行前的动作
Test Teardown           Test Case执行后的动作
Force Tags              Test Suite下的所有测试用例都会被打上这个tag
Default Tags            Test Suite的用例如果没有打上tag,就会用这个默认tag,如果打了tag,就会用自己的tag
Test Timeout            设置每一个测试用例的超时时间,只要超过这个时间就会失败,并停止案例运行
...                     这是防止某些情况导致案例一直卡住不动,也不停止也不是失败
Test Template           数据驱动模板(很有用的一个参数)


*** Variables ***
${SCALAR_VARS}          创建scalar参数
@{LIST_VARS}            创建list参数
&{DICT_VARS}            a=创建dictionary参数


*** Test Cases ***
Test_01
    [Documentation]     测试用例说明……
    [Template]          数据驱动模板,每条用例只有一个模板
    [Tags]              测试用例标签
    [Setup]             测试用例执行前的动作
    [Teardown]          测试用例执行后的动作
    [Timeout]           测试用例的超时时间
    My Keyword One  

Test_02
    [Documentation]     。。。
    [Template]          。。。
    [Tags]              。。。
    [Setup]             。。。
    [Teardown]          。。。
    [Timeout]           。。。
    My Keyword Two


*** Keywords ***
My Keyword One
    [Documentation]     关键字描述
    [Arguments]         自定义参数设置
    [Return]            将返回值抛出
    [Timeout]           关键字流程执行超时时间
    [Tags]              标签
    [Teardown]          关键字流程结束动作
    log                 ${SCALAR_VARS}
    log Mang            @{LIST_VARS}
    log                 ${DICT_VARS}

My Keyword Two
    log                 ${SCALAR_VARS}
    log Mang            @{LIST_VARS}
    log                 ${DICT_VARS}

下面是几个简单的案例:
practice_setup_and_teardown.robot

*** Settings ***
Documentation     test
Suite Setup       suitestart
Suite Teardown    suitestop
Test Setup        testsetup
Test Teardown     teststop

*** Variables ***
${a}              hello world 1
${b}              hello world 2

*** Test Cases ***
testcase1
    [Documentation]    testcase1
    log    ${a}
testcase2
    log    ${b}

*** Keywords ***
suitestart
    Log    suitstart
suitestop
    Log    suitstop
testsetup
    Log    teststart
teststop
    Log    teststop


practice_scalar.robot

*** Settings ***
Documentation       RobotFramework脚本的scalar标量练习
Force Tags          robot-3.0
Default Tags        owner-damao

*** Variables ***
# 创建scalar变量      
${NAME}             Robot Framework
${VERSION}=         3.0
${ROBOT}            ${NAME}  ${VERSION}
${NULL_VAR}         # 空字符串被赋值给了${NULL_VAR}
${EXAMPLE}          This value is joined together with a space
...                 1122333333333333333333333333333333
${LONG_WORDS}       1111111111111111

# 创建列表变量

@{NAMES}        Matti       Teppo
@{NAMES2}       @{NAMES}    Seppo
@{NOTHING}
@{MANY}         one         two      three      four
...             five        six      seven

# 创建字典变量
&{USER 1}       name=Matti    address=xxx         phone=123
&{USER 2}       name=Teppo    address=yyy         phone=456
# &{MANY}         first=1       second=${2}         ${3}=thirds
&{EVEN MORE}    first=override      empty=
...             =empty        key\=here=value


*** Test Cases ***
测试打印scalar变量
    [Documentation]     打印scalar变量
    To Test Scalar


*** Keywords ***
[Documentation]     创建关键字
To Test Scalar
    [Documentation]     打印标量
    log  ${NAME}
    log  ${ROBOT}
    log  ${NULL_VAR}
    log  ${EXAMPLE}
    log  ${LONG_WORDS}
   

practice_List.robot

*** Settings ***
Documentation       RobotFramework脚本"列表"参数的练习
Force Tags          robot-3.0
Default Tags        owner-damao

*** Variables ***
@{list_data}        1    2    3    4    5      
@{list_data2}       a    b    c    d    e    f    
...                 q    w    e                     # 列表元素太长时使用... 分割

*** Test Cases ***
test_01
    [Documentation]     打印列表数据
    Print List Variables

test_02
    [Documentation]     获取列表的长度
    Get List Length

*** Keywords ***
Print List Variables
    [Documentation]     打印列表数据
    log Many    @{list_data}
    log     @{list_data}[2]
    log Many    @{list_data2}  # 打印列表元素需要使用到log Many
    log     @{list_data2}[3]

Get List Length
    [Documentation]     获取列表的长度
    ${length}    BuiltIn.Get Length    ${list_data2}   # 在获取列表的长度时需要注意使用${列表参数}
    log    ${length}

practice_dict.robot

*** Settings ***
Documentation       RobotFramework脚本“字典”参数的练习
Force Tags          robot-3.0
Default Tags        owner-damao


*** Variables ***
&{dict_data}        a=b    e=w   y=1    asd=123
&{dict_data1}       a=1    b=2    c=3    d=4
...                 r=7    u=90

*** Keywords ***
Print Dictionary Data
    [Documentation]    打印字典数据
    log    ${dict_data}
    log    &{dict_data}[a]
    log Many   &{dict_data}[a]    &{dict_data1}[u]
    log    ${dict_data1}


*** Test Cases ***
Test_01
    [Documentation]    测试打印字典数据
    Print Dictionary Data


你可能感兴趣的:(全面学习robotframework框架二:整体理解框架结构)