2. RobotFramework 测试数据格式与测试用例

测试数据语法支持的文件格式

创建 Robot Framework 数据最常见的方式是使用空格分隔格式,其中数据片段用两个或两个以上的空格相互分隔。另一种方式是使用管道分隔格式,其中分隔符是用空格包围的管道字符(|)。

执行的文件通常使用 .robot 扩展名,但是也可以使用 --extension 选项进行配置。资源文件也可以使用 .robot 扩展名,但是建议使用专用的 .resource 扩展名。

Robot Framework 还支持 reStructuredText 文件,以便将普通的 Robot Framework 数据嵌入到代码块中。可以对 reStructuredText 文件使用 .rst 或者 .rest 扩展名,但是必须使用前面提到的 --extension 选项来进行配置。

早期的 Robot Framework 版本也支持 HTML 和 TSV 格式的数据。如果数据与空格分隔格式兼容,那么 TSV 格式仍然有效,但对于 HTML 格式已完全删除。

空格分隔格式

当 Robot Framework 解析数据时,它首先将数据拆分为行,然后将行拆分为关键字和参数等标记。当使用空格分隔格式时,标记之间的分隔符是两个或多个空格,或者一个或多个制表符。虽然使用两个空格作为分隔符就足够了,但还是建议使用四个空格来使分隔符更容易识别。用作分隔符的空格数可以变化,只要至少有两个,就可以在设置和其他地方很好地对齐数据,从而使数据更易于理解。

*** Settings ***
Documentation     Example using the space separated format.
Library           OperatingSystem

*** Variables ***
${MESSAGE}        Hello, world!

*** Test Cases ***
My Test
    [Documentation]    Example test.
    Log    ${MESSAGE}
    My Keyword    ${CURDIR}

Another Test
    Should Be Equal    ${MESSAGE}    Hello, world!

*** Keywords ***
My Keyword
    [Arguments]    ${path}
    Directory Should Exist    ${path}

由于制表符和连续空格被视为分隔符,因此如果关键字参数或者实际数据中的其他地方需要,则必须对其进行转义。可以使用特殊的转义语法,例如 \t 制表符和 \xA0 不间断空格以及内置变量 ${SPACE}${EMPTY}

管道分隔格式

一个文件可以包含空格分隔和管道分隔的行。管道分隔的行由强制性的前导管道字符识别,但行尾的管道是可选的。除行首和行尾外,管道两侧必须至少有一个空格或制表符。无需对齐管道,但对齐管道会使数据更易于阅读。

| *** Settings ***   |
| Documentation      | Example using the pipe separated format.
| Library            | OperatingSystem

| *** Variables ***  |
| ${MESSAGE}         | Hello, world!

| *** Test Cases *** |                 |               |
| My Test            | [Documentation] | Example test. |
|                    | Log             | ${MESSAGE}    |
|                    | My Keyword      | ${CURDIR}     |
| Another Test       | Should Be Equal | ${MESSAGE}    | Hello, world!

| *** Keywords ***   |                        |         |
| My Keyword         | [Arguments]            | ${path} |
|                    | Directory Should Exist | ${path} |

当使用管道分隔格式时,参数内的连续空格或制表符不需要转义。同样,空列也不需要转义,除非它们位于末尾。但是,实际测试数据中可能被空格包围的管道必须使用反斜杠进行转义。

创建测试用例

测试用例基本语法

测试用例是根据可用关键字在测试用例部分中构建的。关键字可以从测试库或资源文件中导入,也可以在测试用例文件本身的关键字部分创建。

测试用例部分的第一列包含测试用例名称。测试用例从该列中包含某些内容的行开始,并继续到下一个测试用例名称或该部分的末尾。

*** Test Cases ***
First Test Case
    Log    First Test Case
Second Test Case
    Log    Second Test Case

setup 和 teardown

setup 是在测试用例之前执行的语句,而 teardown 是在测试用例之后执行的语句。在 Robot Framework 中,setup 和 teardown 只是带有可能参数的普通关键字。

*** Settings ***
Test Setup    Test Begin
Test Teardown    Test End
Suite Setup    Suite Begin
Suite Teardown    Suite End


*** Test Cases ***
First Test Case
    Sleep    1
    Log    First Test Case
Second Test Case
    Sleep    1
    Log    Second Test Case


*** Keywords ***
Test Begin
    Sleep    1
    Log    Test Begin
Test End
    Sleep    1
    Log    Test End
Suite Begin
    Sleep    1
    Log    Suite Begin
Suite End
    Sleep    1
    Log    Suite End

根据日志打印时间判断执行顺序为:Suite Begin -> Test Begin -> First Test Case -> Test End -> Test Begin -> Second Test Case -> Test End -> Suite End

Documentation

可以用 [Documentation] 为测试用例添加文档,该文本显示在命令行输出以及生成的测试日志和测试报告中。可以在文档中使用简单的 HTML 格式,并且可以使用变量来使文档动态化。可能不存在的变量保持不变。

如果文档被分成多列,则一行中的单元格用空格连接在一起。如果文档被分成多行,则创建的文档行本身使用换行符连接。如果一行已经以换行符或转义反斜杠结尾,则不添加换行符。

*** Test Cases ***
Simple
    [Documentation]    Simple documentation
    No Operation

Formatting
    [Documentation]    *This is bold*, _this is italic_  and here is a link: http://robotframework.org
    No Operation

Variables
    [Documentation]    Executed at ${HOST} by ${USER}
    No Operation

Splitting
    [Documentation]    This documentation    is split    into multiple columns
    No Operation

Many lines
    [Documentation]    Here we have
    ...                an automatic newline
    No Operation

Tags

在 Robot Framework 中使用标签是一种简单而强大的测试用例分类机制。标签是自由文本,它们至少可以用于以下目的:

  • 标签显示在测试报告、日志中,当然还有测试数据中,因此它们为测试用例提供元数据。
  • 有关测试用例的统计信息(根据标签自动收集总、通过、失败)。
  • 使用标签,您可以包含或排除要执行的测试用例。
  • 使用标签,您可以指定应该跳过哪些测试用例。
*** Test Cases ***
First Test Case
    [Tags]    First Test Case
    Log    First Test Case
Second Test Case
    [Tags]    Second Test Case
    Log    Second Test Case

假设上述文件命名为 main.robot,则可以通过命令 robot --include "First Test Case" main.robot 根据 Tags 指定只执行 First Test Case。

还有一些更加复杂的使用方法如下:

*** Settings ***
Force Tags      req-42
Default Tags    owner-john    smoke

*** Variables ***
${HOST}         10.0.1.42

*** Test Cases ***
No own tags
    [Documentation]    This test has tags owner-john, smoke and req-42.
    No Operation

With own tags
    [Documentation]    This test has tags not_ready, owner-mrx and req-42.
    [Tags]    owner-mrx    not_ready
    No Operation

Own tags with variables
    [Documentation]    This test has tags host-10.0.1.42 and req-42.
    [Tags]    host-${HOST}
    No Operation

Empty own tags
    [Documentation]    This test has only tag req-42.
    [Tags]
    No Operation

Set Tags and Remove Tags Keywords
    [Documentation]    This test has tags mytag and owner-john.
    Set Tags    mytag
    Remove Tags    smoke    req-*
posted @ 2022-05-14 16:27  Jathon-cnblogs  阅读(413)  评论(0编辑  收藏  举报