在 Robot Framework 中解析布尔值

Parsing booleans in Robot Framework

Robot Framework User Guide, section 6.6 Boolean arguments 说:

Many keywords in Robot Framework standard libraries accept arguments that are handled as Boolean values true or false. If such an argument is given as a string, it is considered false if it is either empty or case-insensitively equal to false or no. Other strings are considered true regardless their value, and other argument types are tested using same rules as in Python.

如何在我自己的用户关键字中复制这种行为?

内置关键字Convert To Boolean更严格:

Converts the given item to Boolean true or false.

Handles strings True and False (case-insensitive) as expected, otherwise returns item's truth value using Python's bool() method.

robot.utils 中有两个函数用于处理布尔参数 - is_truthy 和 is_falsy。日期时间使用 is_falsy。要表现得像那个库,您可以简单地调用这些库使用的相同函数。下面是 is_falsy 在 Robot 语法中的实现,以及使用它来转换参数的示例关键字。您还可以根据需要使用相同的评估语句转换参数并避免相互依赖。

*** Test Cases ***
Boolean
  [Template]    Some Keyword
  truE
  
  FAlsE
  no
  [=10=]

*** Keywords ***
Some Keyword
    [Arguments]    ${option}
    ${option as bool}    Is Truthy    ${option}
    Log To Console    ${option} -> ${option as bool}

Is Truthy
  [Arguments]    ${arg}
  ${arg as bool}    Evaluate    robot.utils.is_truthy($arg)    modules=robot
  [Return]    ${arg as bool}