Python 3.x 机器人框架:与FOR循环相连
我对使用RobotFramework和尝试使用Catenate格式化字符串相当陌生,不知道在格式化字符串时是否可以使用FOR循环,下面的格式是否正确?任何建议都将不胜感激,谢谢Python 3.x 机器人框架:与FOR循环相连,python-3.x,python-2.7,automated-tests,robotframework,Python 3.x,Python 2.7,Automated Tests,Robotframework,我对使用RobotFramework和尝试使用Catenate格式化字符串相当陌生,不知道在格式化字符串时是否可以使用FOR循环,下面的格式是否正确?任何建议都将不胜感激,谢谢 ${data}= Catenate ... { ... "email_address": "${NewUserEmailID}", ... "user_name": "${UserName}", ... "roles": [ ... :FOR ${roleId}, ${catID
${data}= Catenate
... {
... "email_address": "${NewUserEmailID}",
... "user_name": "${UserName}",
... "roles": [
... :FOR ${roleId}, ${catID}, ${subcatID} IN @{role_id_list}, @{category_id_list}, @{subCat_id_list}
... / {
... / "role_id": ${roleId},
... / "categories": [{ "category_id": "${catID}" }],
... / "subcats":[{ "sub_category_id": "${subcatID}" }]
... / },
... ],
... "line_manager": "${LineManageID}",
... "guest_user": ${GuestUser},
... "guest_invitation_text": "Invitation Text",
... "guest_redirect_url": "http://www.example.com/guest",
... "organization_id": 1
... }
在格式化字符串时是否可以使用FOR循环
否,在循环被执行的意义上,并将每个迭代的值附加到Catenate
目标。将发生的情况是,您将在结束字符串中包含“:FOR“,“@{role\u id\u list}”等
但是实现您想要的并不是那么难——只要有一个循环来构造中间字符串,并将其附加到Catenate
中即可。像这样:
${roles array}= Set Variable ${EMPTY} # initialize an empty string variable
:FOR ${roleId} ${catID} ${subcatID} IN ZIP ${role_id_list} ${category_id_list} ${subCat_id_list}
/ ${roles array}= Set Variable ${roles array}{ # you append the target string to the end of the current value of the variable
/ ${roles array}= Set Variable ${roles array}"role_id": ${roleId},
/ ${roles array}= Set Variable ${roles array}"categories": [{ "category_id": "${catID}" }],
/ ${roles array}= Set Variable ${roles array}"subcats":[{ "sub_category_id": "${subcatID}" }]
/ ${roles array}= Set Variable ${roles array}},
# and now use it inside the Catenate
${data}= Catenate
... {
... "email_address": "${NewUserEmailID}",
... "user_name": "${UserName}",
... "roles": [ ${roles array}
... ],
... "line_manager": "${LineManageID}",
... "guest_user": ${GuestUser},
... "guest_invitation_text": "Invitation Text",
... "guest_redirect_url": "http://www.example.com/guest",
... "organization_id": 1
... }
正如您所注意到的,迭代多个列表的语法有点不同-需要使用(来自/类似于python的
zip()
函数)。您试过运行它吗?;)不,至少不会以您希望的方式运行-循环不会执行,您只会得到包含“:FOR“,“@{role_id_list}”等的结束字符串。您应该在纯python中为此实现一个特殊的catenate关键字。