Search code examples
robotframework

Robot if statement that checks if a variable is None


Consider the following example:

*** Variables ***
${myVaraible}=    ${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

  • It seems that writing "${myVariable}" in quotes converts the value of ${myVariable} into a string, in this case '"None"', which is of course not equivalent to ${None}.

    The correct way of checking if a variable is None would be this:

    IF    ${{$myVariable is None}}
        A Keyword
    ELSE
        Another Keyword
    END
    

    Or, to check if a variable is not None:

    IF    ${{$myVariable is not None}}
        A Keyword
    ELSE
        Another Keyword
    END