Python function not failing and returning none

Question:

I am writing robot test cases with python functions.
I have a python function like this

def ParseJSONUserData(jsonstring):
if len(jsonstring) == 0:
    print("String is empty. No processing is possible.")
json_dict = json.loads(jsonstring)
if len(json_dict) == 0:
    print("No data found in file.")
currentdt = json_dict[CURRENTDATETIME]
if len(currentdt) == 0:
    print ("The Current datetime property is empty")
print ("Systems found: ", len( json_dict[SYSTEMS]))
if len(json_dict[SYSTEMS]) == 0 :
    print("No systems found!")

for system_result in json_dict[SYSTEMS]:

    if system_result[SERIALNUM] in systems:
        print ("Duplicate systemserialnumber value: " + system_result[SERIALNUM] )
    systems.add(system_result[SERIALNUM])
    if len(system_result[PUBKEYS][0]["pubkey"]) == 0 :
        print("No pubkey found for system: " + system_result[SERIALNUM] )
    if len(system_result[PUBKEYS][0]["system"]) == 0 :
        print("No pubkey system designator (typically intraop) found for system: " + system_result[SERIALNUM] )

This is my robot framework code.

 ${response}=     GET     ${host}/data  ${data}   headers=${header}
 ${op}=           ParseJSONUserData    response.json() 
 Log to console    ${op}

I am trying any one of the validation is failed in python. It should fail here in robot code. But even if I pass wrong data python function getting executed and robot test case also getting success. Any help will be much appreciated.

Asked By: user3354840

||

Answers:

For a keyword to fail, it must thrown an exception. Instead of printing a string, raise an error. For example:

if len(jsonstring) == 0:
    raise Exception("String is empty. No processing is possible.")

For more information see Reporting keyword stats in the robot framework user guide, where it says this:


Reporting keyword status is done simply using exceptions. If an executed method raises an exception, the keyword status is FAIL, and if it returns normally, the status is PASS.

Normal execution failures and errors can be reported using the standard exceptions such as AssertionError, ValueError and RuntimeError. There are, however, some special cases explained in the subsequent sections where special exceptions are needed.


Answered By: Bryan Oakley