Robot Framework(六):测试case创建初体验

 

测试用例语法

基础语法

测试用例是由测试库或者用户自定义的资源文件中的关键字组成的。基本格式如下:

*** Test Cases ***

Valid Login

   Open Login Page

Input Username    demo

    Input Password    mode

    Submit Credentials

    Welcome Page Should Be Open

 

Setting Variables

    Do Something    first argument    second argument

    ${value} =    Get Some Value

    Should Be Equal    ${value}    Expected value

 

说明:

*** Test Cases ***:表示测试用例

Valid Login:第1个测试用例的名称

Input Username demo:蓝色字体表示的是关键字,对应的就是RF中的一个函数,或者是用户自定义的关键字,红色字体表示的函数的参数

Setting Variables:表示第2个测试用例的名称

 

代码示例1:

*** Test Cases ***

logging

    [Template]  log

    张三

    李四

    王五

 

time

    ${t}  Get Time

log  ${t}

    

执行结果:

Robot Framework(六):测试case创建初体验

 

配置测试用例表

测试用例也有自己的配置项,可以描述测试用例的功能,设置超时时间,甚至重写测试套件中的关键字等等,支持的配置项如下:

[Documentation]:Used for specifying a test case documentation.

[Tags]:Used for tagging test cases.

[Setup][Teardown]:Specify test setup and teardown.

[Template]:Specifies the template keyword to use. The test itself will contain only data to use as arguments to that keyword.

[Timeout]:Used for setting a test case timeoutTimeouts are discussed in their own section.

代码示例:

*** Test Cases ***

Test With Settings

    [Documentation]    Another dummy test

    [Tags]    dummy    owner-johndoe

    Log    Hello, world!

执行结果:

Robot Framework(六):测试case创建初体验

 

​​​​​​​setup和teardown

和其它自动化测试框架类似,Robot Framework也支持setup 和teardown的功能,用来执行一些初始化和清理的工作。setup 和teardown分为测试套件的setup 和teardown,还有测试用例的setup 和teardown,测试套件级别的就是在所有的测试用例中都会执行,测试用例级别的就是在某个测试用例下执行,具体的结构如下:

*** Settings ***

Test Setup       Open Application    App A

Test Teardown    Close Application

 

*** Test Cases ***

Default values

    [Documentation]    Setup and teardown from setting table

    Do Something

 

Overridden setup

    [Documentation]    Own setup, teardown from setting table

    [Setup]    Open Application    App B

    Do Something

 

No teardown

    [Documentation]    Default setup, no teardown at all

    Do Something

    [Teardown]

 

No teardown 2

    [Documentation]    Setup and teardown can be disabled also with special value NONE

    Do Something

    [Teardown]    NONE

 

Using variables

    [Documentation]    Setup and teardown specified using variables

    [Setup]    ${SETUP}

    Do Something

    [Teardown]    ${TEARDOWN}

 

说明:

  1. Setting中声明的是测试用例的setup 和teardown,以test开头,如果是测试套件级别的话是,以Site开头
  2. 测试用例中默认会执行[Setup] or [Teardown](见Default Values
  3. 测试用例中使用关键字[Setup] or [Teardown]是对Setting中声明的关键字重写(见Overridden setup
  4. 如果使用了关键字[Setup] or [Teardown],但是后面紧跟的关键字是None的话,或者没有参数的话,那么相应的操作也不会执行(见No teardown和No teardown 2
  5. 可以使用变量的方式(${var})执行[Setup] or [Teardown](见Using Variables

测试模板

测试模板的作用是将关键字驱动的测试用例转换为数据驱动的测试用例。

基本用法

可以使用[Template] 或者在Setting中用Test Template实现,模板如下:

方式一:[Template]

*** Test Cases **

Normal test case    

Example keyword    first argument    second argument

 

Templated test case    

[Template]    Example keyword    

first argument    second argument

 

代码示例:

*** Test Cases ***

normal test case

    log    光荣之路

    log    测试开发培训

 

template test case

    [Template]    log

    光荣之路

    测试开发培训

执行结果:

Robot Framework(六):测试case创建初体验

方式二:

*** Settings ***

Test Template    Example keyword

 

*** Test Cases ***

Templated test case    

first round 1     first round 2    

second round 1    second round 2    

third round 1     third round 2

 

代码示例:

*** Settings ***

Test Template     log

 

*** Test Cases ***

template test case

    [Template]    log

    光荣之路

    测试开发培训

执行结果:

Robot Framework(六):测试case创建初体验

 

​​​​​​​内置参数的模板

这种类型的模板中参数类似于占位符,调用时就用指定位置的数据替换,所以传入的参数个数需要好定义的参数个数相等,但是参数名可以不一致。

代码示例1:

*** Test Cases ***

Normal test case with embedded arguments

    The result of 1 + 1 should be 2

    The result of 1 + 2 should be 3

 

Template with embedded arguments

    [Template]    The result of ${calculation} should be ${expected}

    1 + 1    2

    1 + 2    3

 

*** Keywords ***

The result of ${calculation} should be ${expected}

    ${result} =    Calculate    ${calculation}

    Should Be Equal    ${result}    ${expected}

 

Calculate

    [Arguments]    ${calculation}

    ${res}    evaluate    str(eval(str(${calculation})))

    [Return]    ${res}

执行结果:

Robot Framework(六):测试case创建初体验

 

代码示例2:

*** Test Cases ***

Different argument names

    [Template]    The result of ${foo} should be ${bar}

    1 + 1    2

    1 + 2    3

 

Only some arguments

    [Template]    The result of ${calculation} should be 3

    1 + 2

    4 - 1

 

New arguments

    [Template]    The ${meaning} of ${life} should be 42

    result    21 * 2

 

*** Keywords ***

The result of ${calculation} should be ${expected}

    ${result} =    Calculate    ${calculation}

    Should Be Equal    ${result}    ${expected}

 

Calculate

    [Arguments]    ${calculation}

    ${res}    evaluate    str(eval(str(${calculation})))

    [Return]    ${res}

 

执行结果:

Starting test: Em Arg Template1.Different argument names

20190627 13:52:45.376 :  INFO : ${res} = 2

20190627 13:52:45.378 :  INFO : ${result} = 2

20190627 13:52:45.383 :  INFO : ${res} = 3

20190627 13:52:45.385 :  INFO : ${result} = 3

Ending test:   Em Arg Template1.Different argument names

 

Starting test: Em Arg Template1.Only some arguments

20190627 13:52:45.394 :  INFO : ${res} = 3

20190627 13:52:45.395 :  INFO : ${result} = 3

20190627 13:52:45.401 :  INFO : ${res} = 3

20190627 13:52:45.402 :  INFO : ${result} = 3

Ending test:   Em Arg Template1.Only some arguments

 

Starting test: Em Arg Template1.New arguments

20190627 13:52:45.411 :  INFO : ${res} = 42

20190627 13:52:45.412 :  INFO : ${result} = 42

Ending test:   Em Arg Template1.New arguments