如何在Robot Framework中检查变量类型?

时间:2015-08-06 13:38:29

标签: robotframework

我正在使用Robot Framework来测试一个返回int的系统,该系统表示系统在测试时遇到的错误数。但是,如果系统无响应,则返回将为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}    ${0}    msg=System generated ${error_count} errors during test.

3 个答案:

答案 0 :(得分:2)

To check that ${error_count} is not None, you can do the following:

Should Be True    ${error_count} is not ${None}    msg=Returned None

So long as the previous statement passes, your test can move forward with printing your result like so:

Log to Console    System generated ${error_count} errors during test

Alternatively, you can merge everything onto one line using the Run Keyword Unless keyword:

Run Keyword Unless    ${error_count} is ${None}    Log to Console    System generated ${error_count} errors during test    

答案 1 :(得分:0)

你需要知道何时获得NoneType?你当然可以做你想做的事情,作为一个明显需要扩展的简单例子立即想到的东西

Run Keyword Unless    '${error_count}' == 'None'

此处提供更多信息:

http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Run%20Keyword%20If

答案 2 :(得分:0)

先前提供的解决方案将适用于这个原始问题(其中${error_count}是整数或${None}),但对于${error_count}的其他值可能无法达到预期的效果。检查无的更通用的解决方案是:

Run Keyword If    ${value == None}    Log To Console    value is None

使用Robot Framework 2.9,可以在Python评估空间中使用robot变量,该变量可以进行以下操作(这是在使用Robot Framework 2.9或更高版本时的首选解决方案):

Run Keyword If    $value is None    Log To Console    value is None