【问题标题】:Is it possible to have a variable span multiple lines in Robot Framework?机器人框架中是否可以有一个变量跨越多行?
【发布时间】:2013-05-09 05:48:36
【问题描述】:

我有一个很长的正则表达式,我想将它放入一个变量中进行测试。我希望能够把它放在多行上,这样它就不会那么难读了。我看到你可以用文档标签做多行。但是当我尝试这种格式时,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'

【问题讨论】:

    标签: robotframework


    【解决方案1】:

    在变量表中

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

    有关详细信息,请参阅用户指南中的Variable table

    *** 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}'
    

    【讨论】:

    • 这是一个非常有用的技术。我的变量需要使用 ${SPACE} 变量来强制在行尾使用空格字符。
    • 对于那些想要换行的人,在文本中输入\n${\n}
    【解决方案2】:

    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-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
    

    这会产生与上面相同的输出。

    【讨论】:

      【解决方案3】:

      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...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-02
        • 1970-01-01
        • 2016-04-16
        • 2018-08-22
        • 2016-12-05
        • 2014-10-06
        • 2020-12-07
        • 1970-01-01
        相关资源
        最近更新 更多