robotframeworkempty-list

[robotframework]Why Create a empty element to a list can treat as True, but not applicable to a empty variable


Create a empty element to a list, and return True value with "Should Be True" key word. but not suiteable for create a empty variable.Why is that? code:

${list1}    Create List    ${EMPTY}
${list2}    Set Variable    ${EMPTY}
Should Be True    ${list1}    Create empty list
Should Be True    ${list2}    Set empty variable

Log:

20211225 14:24:58.913 : INFO : ${list1} = ['']

20211225 14:24:58.916 : TRACE : Arguments: [ '' ]

20211225 14:24:58.916 : TRACE : Return: ''

20211225 14:24:58.917 : INFO : ${list2} =

20211225 14:24:58.918 : TRACE : Arguments: [ [''] | 'Create empty list' ]

20211225 14:24:58.918 : TRACE : Return: None

20211225 14:24:58.919 : TRACE : Arguments: [ '' | 'Set empty variable' ]

20211225 14:24:58.920 : FAIL : Evaluating expression '' failed: ValueError: Expression cannot be empty.


Solution

  • It is a bit counter-intuitive because ${list1} Create List ${EMPTY} is not creating an empty list, but a list with an empty element inside, e.g. [''], while ${list2} Set Variable ${EMPTY} will create an empty variable, e.g. ''. This is why Should Be True passes on the list (list is not empty) but fails on the variable (variable is empty).

    Below code exemplifies these cases:

        # empty_list == []
        ${empty_list} =     Create List
        log to console      ${empty_list}
        should be empty     ${empty_list}
    
        # empty_list_1 == ['']
        ${empty_list_1} =    Create List    ${EMPTY}
        log to console       ${empty_list_1}
        should not be empty  ${empty_list_1}
    
        # empty_var == '' (empty string with length 0)
        ${empty_var} =       Set Variable
        log to console       ${empty_var}
        should be empty      ${empty_var}
    
        # empty_var_1 == '' (empty string with length 0)
        ${empty_var_1} =     Set Variable    ${EMPTY}
        log to console       ${empty_var_1}
        should be empty      ${empty_var_1}