追加到列表只是覆盖当前列表

问题描述 投票:0回答:1

我的关键字中有这个片段,用于将所有 id 存储在 test_id_list 中,但它似乎不起作用,因为最新的 test_id 只是覆盖了以前的 test_id,它不会添加到列表中。

${test_id}    Get Value From JSON    ${response}    $.id
${test_id}    Get From List    ${test_id}    0
${test_id_list}    Create List
Append to List    ${test_id_list}    ${test_id}

我已经尝试过:

删除第三行并将其声明为 @{test_id_list}= Create List 并且不删除第三行,只是将其更改为“@{test_id_list}”

python automation robotframework keyword robotframework-ide
1个回答
0
投票

由于上面的代码而发生覆盖:

${test_id_list}    Create List

因此,假设

test_id_list
是测试变量或全局变量,并且代码运行了多次。然后,它每次都会创建一个全新的空列表并将其分配给
${test_id_list}
变量,删除旧列表。

由于您的代码中没有更大的部分,我建议从表面上将列表创建从这段代码中移出。

为了说明这个想法并想象响应列表,伪代码可能如下所示:

${test_id_list}    Create List
FOR  ${response}  IN  @{responses}
   ${test_id}        Get Value From JSON    ${response}    $.id
   Append to List    ${test_id_list}    ${test_id}[0]
END
© www.soinside.com 2019 - 2024. All rights reserved.