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
这导致与上述相同的输出。