23

我有一个很长的正则表达式,我想将它放入一个变量中进行测试。我希望能够把它放在多行上,这样它就不会那么难读了。我看到你可以用文档标签做多行。但是当我尝试这种格式时,Robot 似乎认为这是一个列表。有没有办法在机器人框架中做到这一点?

考虑:

${example_regex} =      '(?m)Setting IP address to [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\nSetting MAC address to [0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}\nSetting IP forwarding kernel options'

我希望能够写:

${example_regex}   '(?m)Setting IP address to [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\n
                     Setting MAC address to [0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}\n
                     Setting IP forwarding kernel options'
4

3 回答 3

33

在变量表中

如果您在表中创建字符串*** Variables ***,则可以将定义分布在多行中。您可以使用特殊参数SEPARATOR来定义单元格如何连接在一起。默认情况下,这些行由空格连接,因此您需要通过明确不给出SEPARATOR值来将其设置为空字符串。

有关详细信息,请参阅用户指南中的变量表

*** Variables ***
${example_regex}=  SEPARATOR=
...  (?m)Setting IP address to [0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\n
...  Setting MAC address to [0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}\\n
...  Setting IP forwarding kernel options

在测试用例或关键字中

如果您尝试在测试用例或关键字中执行此操作,则不能直接定义多行字符串。但是,您可以在测试用例中使用catenate关键字或使用关键字连接分布在多个单元格中的数据来获得相同的效果。如果您不希望数据中有换行符,请务必正确转义反斜杠,并将分隔符设置为空字符串。

*** Test Cases ***
Multiline variable example
  ${example_regex}=  catenate  SEPARATOR=
  ...  (?m)Setting IP address to [0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\n
  ...  Setting MAC address to [0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}\\n
  ...  Setting IP forwarding kernel options
  log  regex: '${example_regex}'
于 2013-05-15T17:11:58.207 回答
11

Robot Framework 2.9根据 docs添加了对多行文字字符串的支持。

test.robot

*** Variables ***
${example_regex} =  SEPARATOR=
...  (?m)Setting IP address to [0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\n
...  Setting MAC address to [0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}\\n
...  Setting IP forwarding kernel options

*** Test Cases ***
Show output
    Log  \n${example_regex}  console=yes

robot test.robot

==============================================================================
Test
==============================================================================
Show output
(?m)Setting IP address to [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\nSetting MAC address to [0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}\nSetting IP forwarding kernel options
Show output                                                           | PASS |
------------------------------------------------------------------------------
Test                                                                  | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================

几点注意事项:

  • 从每一行修剪所有前导和尾随空格
  • 第一行中的纯SEPARATOR=文本不指定分隔符

你也可以考虑使用变量文件,因为你获得了 Python 文字格式化的所有功能,这可以使维护复杂的正则表达式等内容变得更容易。如果您使用的是 Robot Framework 3+ 和 Python 3.5+(用于f-strings),那么它可能如下所示:

vars.py

ip_address_pattern = r'[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
lower_mac_address_pattern = '[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}'
example_regex = (
  rf'(?m)Setting IP address to {ip_address_pattern}\n'
  rf'Setting MAC address to {lower_mac_address_pattern}\n'
    'Setting IP forwarding kernel options'
)

test.robot

*** Settings ***
Variables  vars.py

*** Test Cases ***
Show output
    Log  \n${example_regex}  console=yes

这导致与上述相同的输出。

于 2018-09-02T15:24:28.503 回答
0

Robot Framework 2.9之前,可以使用python 的join函数:

*** Variables ***

@{example_regex}=
...  (?m)Setting IP address to [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\n
...  Setting MAC address to [0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}\n
...  Setting IP forwarding kernel options'

*** Test Cases ***

MultiLine
  ${example_regex}=  Evaluate  "".join(${example_regex})
  Log  "\n"${example_regex}

结果:

20190813 14:02:39.421 - INFO - ${example_regex} = (?m)Setting IP address to [0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}
Setting MAC address to [0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}
Setting IP forwarding kernel option...
于 2019-08-13T14:07:21.053 回答