Robotframework 入门教程(五)

本文介绍使用RobotFramework进行数据驱动测试的方法,并提供了一个实际案例。通过设置Template,实现关键字的重复执行,专注于输入数据的变化,提高代码的可维护性和复用性。
==test case 风格==
前面写的几个case都是关键字驱动(keyword-driven)一步一步关键字顺序执行。RF另一种风格就是数据驱动(data-driven)修改demo.robot的Fourth Case如下:
*** Settings ***
[Documentation]    Example test cases using the keyword-driven testing approach.

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

*** Test Cases ***
First Case
    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
    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
    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}

*** 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}
运行robot -t “Second Case” demo.robot得到如下结果:


通过case中设置Template使得关键字可以不止一次的执行,同时将测试步骤执行流程隐藏,专注于输入数据。data-driven比较适合接口的测试,针对同一操作不同的数据反复进行访问。这样写还有个好处提高了代码的可维护性,比如关键字的名字变化了,只需要修改template中一行就可以,不像second case中每行都要替换。

RF中还有一种风格是行为驱动(Behavior-driven),我也没用过,感觉就是关键字前加些固定的词使case更容易理解,(Given,When,and,Then)执行的时候是被去掉的, 撸个例子占个坑。

*** Test Cases ***
Valid Login
    Given login page is open
    When valid username and password are inserted
    and credentials are submitted
    Then welcome page should be open
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值