Robotframework 当响应中不存在属性时,Robot框架对JSON测试的响应失败

Robotframework 当响应中不存在属性时,Robot框架对JSON测试的响应失败,robotframework,Robotframework,我对robotframework非常陌生,如果下两个条件有效,我需要编写通过的Robot测试: Should Not Contain ${response.json()['errorDetails']['errorDescription']} ${error1} Should Not Contain ${response.json()['errorDetails']['errorCode']} ${error2} 如果响应不是错误响应,则上述条件将失败,因

我对robotframework非常陌生,如果下两个条件有效,我需要编写通过的Robot测试:

Should Not Contain    ${response.json()['errorDetails']['errorDescription']}    ${error1}
Should Not Contain    ${response.json()['errorDetails']['errorCode']}           ${error2}
如果响应不是错误响应,则上述条件将失败,因为响应不包含键“errorDetails”

Resolving variable '${response.json()['errorDetails']['errorDescription']}' failed: KeyError: 'errorDetails'
例如,这也是一个没有“errorDetails”属性的有效响应:

{"respStatus":"SUCCESS","respObj":{"message":"Object created"}}

如果响应不包含此类属性“errorDetails”,如何重写测试条件以不失败?

您可以将检查放入关键字中,然后使用
运行关键字if
仅在通过条件时触发检查

以下是一个例子:

*** Test cases ***
my_test
    ${json} =  Create Dictionary  foo=bar
    # next statement will not be launched in that case
    # but it will be launched if errorDetails key is present in the JSON
    Run keyword if  'errorDetails' in ${json}  check_error_details  ${json}
    
*** Keywords ***
check_error_details
    [Arguments]  ${json}
    Should Not Contain    ${json['errorDetails']['errorDescription']}    ${error1}
    Should Not Contain    ${json['errorDetails']['errorCode']}           ${error2}
    

您可以将检查放入关键字中,然后使用
Run关键字if
仅在通过条件时触发检查

以下是一个例子:

*** Test cases ***
my_test
    ${json} =  Create Dictionary  foo=bar
    # next statement will not be launched in that case
    # but it will be launched if errorDetails key is present in the JSON
    Run keyword if  'errorDetails' in ${json}  check_error_details  ${json}
    
*** Keywords ***
check_error_details
    [Arguments]  ${json}
    Should Not Contain    ${json['errorDetails']['errorDescription']}    ${error1}
    Should Not Contain    ${json['errorDetails']['errorCode']}           ${error2}