Rudi
Rudi

Reputation: 1179

Initialize list variable as empty within Robot Framework ***Variables*** section

I would like to define a list variable which is empty initially:

*** Settings ***
Library  Collections

*** Variables ***
@{customers}=  []    # how to do this the right way? I expect an empty list.
    
*** Test Cases ***
Create Customers
    Add New Customer
    Add New Customer
    Log to console  ${customers}
    # results in:  ['[]', {'name': 'Ben', 'id': '123'}, {'name': 'Ben', 'id': '123'}]     

*** Keywords ***
Add New Customer
    ${customer}=  Create Dictionary  name=Ben  id=123
    Append To List  ${customers}  ${customer}

Regardless I know I could initialize @{customers} within testcase with 'Create List' I'm confused why the list has '[]' as first element. How I could initialize a list as empty in Variables section?

Upvotes: 1

Views: 5116

Answers (2)

Bence Kaulics
Bence Kaulics

Reputation: 7271

You could use the built in variable ${EMPTY} to explicitly init something to empty. You won't have a warning with it.

This one could be passed to keywords directly as well. For more check the User Guide, Space and empty variables.

Upvotes: 1

pavelsaman
pavelsaman

Reputation: 8352

For Robot, it's enough to do this:

*** Variables ***
@{customers}=

when you use @, Robot knows you're creating a list, and there's nothing on the right side, so it will be empty. And = is optional, so you could also type:

*** Variables ***
@{customers}

If it were outside Variables section, you could also use:

${customers}=    Create List

Upvotes: 3

Related Questions