Robot Framework中判断变量是否为${None}的正确IF语句写法
解决Robot Framework中判断变量是否为${None}的问题
问题出在你给${myVariable}加了双引号,这会把变量转换成字符串类型。当变量值是${None}时,"${myVariable}"实际变成了字符串"None",而${None}是Robot Framework的特殊空值对象,两者类型和值都不相等,所以表达式结果为false,导致执行ELSE分支。
正确的写法有两种:
方式一:直接比较变量与${None}(无引号)
*** Variables *** ${myVariable}= ${None} *** Test cases *** MyTest A Keyword That May Or May Not Set myVariable IF ${myVariable} != ${None} A Keyword ELSE Another Keyword END
方式二:使用Python风格的is not判断
*** Variables *** ${myVariable}= ${None} *** Test cases *** MyTest A Keyword That May Or May Not Set myVariable IF ${myVariable} is not None A Keyword ELSE Another Keyword END
注意:如果你的变量是从外部获取的字符串值"None"(而非Robot Framework内置的${None}),则需要用字符串比较"${myVariable}" != "None",但针对内置空值${None},直接用上述两种方式即可。
内容的提问来源于stack exchange,提问作者Thomas Fritz
相关产品推荐
相关产品推荐

