如何检查 Robot Framework 中的变量类型?
How to inspect variable type in Robot Framework?
我正在使用 Robot Framework 测试一个系统,该系统 return 是一个整数,表示系统在测试时遇到的错误数。但是,如果系统变得无响应,return 将是 NoneType,我会收到此错误:
'None' cannot be converted to an integer: TypeError: int() argument must be a string or a number, not 'NoneType'
我无法使用 Should Be True
关键字,因为传递的响应将为 0。
有什么方法可以在 Robot 中测试变量的类型吗?我的目标是做这样的事情:
Should Not Be NoneType ${error_count} msg=System is not responding after test.
Should Be Equal As Integers ${error_count} [=10=] msg=System generated ${error_count} errors during test.
要检查 ${error_count}
不是 None
,您可以执行以下操作:
Should Be True ${error_count} is not ${None} msg=Returned None
只要前面的语句通过,您的测试就可以继续打印结果,如下所示:
Log to Console System generated ${error_count} errors during test
或者,您可以使用 Run Keyword Unless
关键字将所有内容合并到一行:
Run Keyword Unless ${error_count} is ${None} Log to Console System generated ${error_count} errors during test
您需要知道何时获得 NoneType 吗?你当然可以做你想做的事,作为一个简单的例子立即浮现在脑海中,显然需要扩展
Run Keyword Unless '${error_count}' == 'None'
此处提供更多信息:
http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Run%20Keyword%20If
之前提供的解决方案将适用于这个原始问题(其中 ${error_count}
是一个整数或 ${None}
),但对于 ${error_count}
的其他值可能不会像预期的那样运行。检查 None 的更通用的解决方案是:
Run Keyword If ${value == None} Log To Console value is None
使用 Robot Framework 2.9,robot 变量在 Python 评估 space 中可用,这允许以下操作(这是我使用 Robot Framework 2.9 或更高版本时的首选解决方案):
Run Keyword If $value is None Log To Console value is None
我正在使用 Robot Framework 测试一个系统,该系统 return 是一个整数,表示系统在测试时遇到的错误数。但是,如果系统变得无响应,return 将是 NoneType,我会收到此错误:
'None' cannot be converted to an integer: TypeError: int() argument must be a string or a number, not 'NoneType'
我无法使用 Should Be True
关键字,因为传递的响应将为 0。
有什么方法可以在 Robot 中测试变量的类型吗?我的目标是做这样的事情:
Should Not Be NoneType ${error_count} msg=System is not responding after test.
Should Be Equal As Integers ${error_count} [=10=] msg=System generated ${error_count} errors during test.
要检查 ${error_count}
不是 None
,您可以执行以下操作:
Should Be True ${error_count} is not ${None} msg=Returned None
只要前面的语句通过,您的测试就可以继续打印结果,如下所示:
Log to Console System generated ${error_count} errors during test
或者,您可以使用 Run Keyword Unless
关键字将所有内容合并到一行:
Run Keyword Unless ${error_count} is ${None} Log to Console System generated ${error_count} errors during test
您需要知道何时获得 NoneType 吗?你当然可以做你想做的事,作为一个简单的例子立即浮现在脑海中,显然需要扩展
Run Keyword Unless '${error_count}' == 'None'
此处提供更多信息:
http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Run%20Keyword%20If
之前提供的解决方案将适用于这个原始问题(其中 ${error_count}
是一个整数或 ${None}
),但对于 ${error_count}
的其他值可能不会像预期的那样运行。检查 None 的更通用的解决方案是:
Run Keyword If ${value == None} Log To Console value is None
使用 Robot Framework 2.9,robot 变量在 Python 评估 space 中可用,这允许以下操作(这是我使用 Robot Framework 2.9 或更高版本时的首选解决方案):
Run Keyword If $value is None Log To Console value is None