robotframework

Robot if statement that checks if a variable is None


Consider the following example:

*** Variables ***
${myVariable}=    ${None}

*** Test cases ***
MyTest
    A Keyword That May Or May Not Set myVariable
    IF    "${myVariable}" != ${None}
        A Keyword
    ELSE
        Another Keyword
    END

My problem is that even if ${myVariable} is not reassigned, the boolean expression "${myVariable}" != ${None} is still false and the ELSE branch will be executed instead of the IF branch.

So what is the correct way of writing an if else statement that executes different keywords depending on whether or not a variable is ${None}?


Solution

  • To keep the original type of compared variable, without assigning it to string, the $variable syntax can be used.

    *** Variables ***
    ${myVariable}=    ${None}
    
    *** Test cases ***
    MyTest
        A Keyword That May Or May Not Set myVariable
        IF    $myVariable != ${None}
            A Keyword
        ELSE
            Another Keyword
        END
    

    To keep such evaluations consistent, Robocop linter has a rule that might prevent doing unnecessary string conversion.