内置变量

Robot Framework 提供了若干的内置变量, 这些变量在测试中自动可用.

操作系统变量

操作系统相关的内置变量使得编写针对不同操作系统的测试数据变的轻松.

可用的操作系统相关的内置变量
VariableExplanation
${CURDIR}An absolute path to the directory where the test datafile is located. This variable is case-sensitive.
${TEMPDIR}An absolute path to the system temporary directory. In UNIX-likesystems this is typically /tmp, and in Windowsc:\Documents and Settings\<user>\Local Settings\Temp.
${EXECDIR}An absolute path to the directory where test execution wasstarted from.
${/}The system directory path separator. / in UNIX-likesystems and \ in Windows.
${:}The system path element separator. : in UNIX-likesystems and ; in Windows.
${\n}The system line separator. \n in UNIX-like systems and\r\n in Windows. New in version 2.7.5.
  1. *** Test Cases ***
  2. Example
  3. Create Binary File ${CURDIR}${/}input.data Some text here${\n}on two lines
  4. Set Environment Variable CLASSPATH ${TEMPDIR}${:}${CURDIR}${/}foo.jar

数字变量

变量的语法可以用来创建整型整数和浮点型数字. 如下例所示. 因为 Robot Framework默认传递的是字符串, 显式的传递数字对那些预期接受参数是数字(而不是数字字符串)的关键字来说很有用.

  1. *** Test Cases ***
  2. Example 1A
  3. Connect example.com 80 # Connect gets two strings as arguments
  4.  
  5. Example 1B
  6. Connect example.com ${80} # Connect gets a string and an integer
  7.  
  8. Example 2
  9. Do X ${3.14} ${-1e-4} # Do X gets floating point numbers 3.14 and -0.0001

使用 0b 0o0x 前缀还可以创建二进制, 八进制 和十六进制的数字. 注意这里的语法不区分大小写.

  1. *** Test Cases ***
  2. Example
  3. Should Be Equal ${0b1011} ${11}
  4. Should Be Equal ${0o10} ${8}
  5. Should Be Equal ${0xff} ${255}
  6. Should Be Equal ${0B1010} ${0XA}

布尔值和None/null值

布尔值和Python中的 None 以及Java中的 null 也可以使用类似数字变量的语法来表示.

  1. *** Test Cases ***
  2. Boolean
  3. Set Status ${true} # Set Status gets Boolean true as an argument
  4. Create Y something ${false} # Create Y gets a string and Boolean false
  5.  
  6. None
  7. Do XYZ ${None} # Do XYZ gets Python None as an argument
  8.  
  9. Null
  10. ${ret} = Get Value arg # Checking that Get Value returns Java null
  11. Should Be Equal ${ret} ${null}

这些变量都不区分大小写, 例如 ${True}${true} 是一样的. 同样, ${None}${null} 也是同义的, 因为当使用Jython解释器运行时, Jython会视情况自动转换.

空格和空字符串/列表/字典

变量 ${SPACE}${EMPTY} 分别用来创建空格和空字符串. 使用这些变量相对于使用反斜杠来 转义空格 容易的多. 同时还可以使用 扩展变量语法 表示连续的多个空格, 例如 ${SPACE * 5}

下面的例子中, 关键字 Should Be Equal 接收到两个等价的入参, 可以看出使用变量的形式比使用反斜杠看上去容易理解的多.

  1. *** Test Cases ***
  2. One Space
  3. Should Be Equal ${SPACE} \ \
  4.  
  5. Four Spaces
  6. Should Be Equal ${SPACE * 4} \ \ \ \ \
  7.  
  8. Ten Spaces
  9. Should Be Equal ${SPACE * 10} \ \ \ \ \ \ \ \ \ \ \
  10.  
  11. Quoted Space
  12. Should Be Equal "${SPACE}" " "
  13.  
  14. Quoted Spaces
  15. Should Be Equal "${SPACE * 2}" " \ "
  16.  
  17. Empty
  18. Should Be Equal ${EMPTY} \

同样还可以使用 列表变量 的格式 @{EMPTY} 表示空列表, 字典变量 的格式 &{EMPTY} 表示空字典.

在某些情况下, 它们会很有用. 比如, 当使用 测试模板模板关键字调用无需参数 <template keyword is used without arguments> 时; 或者想要覆盖不同作用域中的列表或字典变量时. 注意, 没法改变 @{EMPTY}&{EMPTY} 的值.

  1. *** Test Cases ***
  2. Template
  3. [Template] Some keyword
  4. @{EMPTY}
  5.  
  6. Override
  7. Set Global Variable @{LIST} @{EMPTY}
  8. Set Suite Variable &{DICT} &{EMPTY}

注解

@{EMPTY} 在Robot Framework 2.7.4版本可用, &{EMPTY} 在2.9版本后可用.

自动变量

Robot Framework还提供了若干的自动变量. 这些变量在测试执行过程中有不同的值, 有些还是全局可用的. 改变这些变量的值不会影响其初始值, 不过其中某些可用通过 BuiltIn 库中的关键字进行动态修改.

Available automatic variables
VariableExplanationAvailable
${TEST NAME}The name of the current test case.Test case
@{TEST TAGS}Contains the tags of the current test case inalphabetical order. Can be modified dynamically usingSet Tags and Remove Tags keywords.Test case
${TEST DOCUMENTATION}The documentation of the current test case. Can be setdynamically using using Set Test Documentation_keyword. New in Robot Framework 2.7.Test case
${TEST STATUS}The status of the current test case, either PASS orFAIL.Setup和Teardown
${TEST MESSAGE}The message of the current test case.Setup和Teardown
${PREV TEST NAME}The name of the previous test case, or an empty stringif no tests have been executed yet.Everywhere
${PREV TEST STATUS}The status of the previous test case: either PASS,FAIL, or an empty string when no tests have beenexecuted.Everywhere
${PREV TEST MESSAGE}The possible error message of the previous test case.Everywhere
${SUITE NAME}The full name of the current test suite.Everywhere
${SUITE SOURCE}An absolute path to the suite file or directory.Everywhere
${SUITE DOCUMENTATION}The documentation of the current test suite. Can beset dynamically using using _Set SuiteDocumentation keyword. New in Robot Framework 2.7.Everywhere
&{SUITE METADATA}The free metadata of the current test suite. Can beset using Set Suite Metadata keyword.New in Robot Framework 2.7.4.Everywhere
${SUITE STATUS}The status of the current test suite, either PASS orFAIL.套件的Setup和Teardown
${SUITE MESSAGE}The full message of the current test suite, includingstatistics.套件的Setup和Teardown
${KEYWORD STATUS}The status of the current keyword, either PASS orFAIL. New in Robot Framework 2.7用户关键字的Teardown
${KEYWORD MESSAGE}The possible error message of the current keyword.New in Robot Framework 2.7.用户关键字的Teardown
${LOG LEVEL}Current log level_. New in Robot Framework 2.8.Everywhere
${OUTPUT FILE}An absolute path to the output file_.Everywhere
${LOG FILE}An absolute path to the 日志文件 or string NONEwhen no log file is created.Everywhere
${REPORT FILE}An absolute path to the 报告文件 or stringNONE when no report is created.Everywhere
${DEBUG FILE}An absolute path to the 调试文件 or stringNONE when no debug file is created.Everywhere
${OUTPUT DIR}An absolute path to the 输出目录.Everywhere

测试套件相关的变量 ${SUITE SOURCE} ${SUITE NAME} ${SUITE DOCUMENTATION}&{SUITE METADATA} 在测试库和变量文件被导入时即可访问. 除了在 Robot Framework 2.8 和 2.8.1 版本里. 不过, 上表中其它的某些自动变量在导入时刻还没有解析.