我有一个很长的正则表达式,我想把它放入一个变量来测试。我希望能够把它放在多行上,这样它就不那么难以理解了。我看到你可以用文档标签做多行。但是当我尝试这种格式化时,Robot似乎认为这是一个列表。有没有办法在Robot Framework中做到这一点?
考虑:
${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'
答案 0 :(得分:25)
不幸的是,Robot Framework不直接支持多行文字字符串。但是,您可以使用测试用例或关键字中的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}'
答案 1 :(得分:5)
Robot Framework 2.9添加了对每个the 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=
不指定分隔符您还可以考虑使用variable files,因为那样您将获得Python文字格式设置的所有功能,这可以使维护复杂的正则表达式之类的内容变得更加容易。如果您使用的是Robot Framework 3+和Python 3.5+(用于f
字符串),则它可能看起来像:
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
与上述结果相同。
答案 2 :(得分:0)
机器人框架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...