在 Robot Framework ***Variables*** 部分中将列表变量初始化为空

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

我想定义一个最初为空的列表变量:

*** 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}

尽管我知道我可以使用 'Create List' 在测试用例中初始化 @{customers} 我很困惑为什么列表将“[]”作为第一个元素。如何在变量部分将列表初始化为空?

对于Robot来说,这样做就够了:

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

当您使用@时,Robot 知道您正在创建一个列表,而右侧没有任何内容,因此它将是空的。 = 是可选的,因此您还可以键入:

*** Variables ***
@{customers}

如果在 Variables部分,你也可以使用:

${customers}=    Create List

您可以使用内置变量 ${EMPTY} 显式初始化某些内容以清空。您不会收到警告。

这个也可以直接传递给关键字。有关更多信息,请查看用户指南,Space and empty variables