[英]Log dictionary to console in `robot framework`
I have a dictionary like this 我有这样的字典
{ a:{red ,blue, green}, b: {head, eyes, nose} }
and I want to print this in the console formatted in a pretty way like this. 我想以类似这样的漂亮方式在控制台中打印它。
------------
a
------------
red
blue
green
-------------
b
-------------
head
eyes
nose
-------------
Since robot framework
does not support nested loops, I find it difficult to do this. 由于robot framework
不支持嵌套循环,因此我很难做到这一点。 I want to handle this in the job console and not the log.html
. 我想在作业控制台而不是log.html
。
This will print what you want to the console with only one loop: 只需一个循环,即可将所需的内容打印到控制台 :
from robot.api import logger
d = { "a":{"red" ,"blue", "green"}, "b": {"head", "eyes", "nose"} }
divider = "------------"
s = []
for item in d:
s.append(divider)
s.append(item)
s.append(divider)
s.extend(d[item])
s = "\n".join(s)
logger.console(s)
Although a Python solution is likely to give you a bit more flexibility with regards to formatting etc, the out-of-the-box keywords in Robot are sufficient to create the desired logic. 尽管Python解决方案在格式设置等方面可能会给您带来更大的灵活性,但是Robot中的即用型关键字足以创建所需的逻辑。 See below for the code example and the output: 参见下面的代码示例和输出:
*** Settings ***
Library Collections
*** Variables ***
@{a} red blue green
@{b} head eyes nose
&{DIC} a=${a} b=${b}
*** Test Cases ***
TC
Log Structure ${DIC}
*** Keywords ***
Log Structure
[Arguments] ${structure}
Log To Console \n
Log To Console ------------------------------
# For Loops only work on Lists, so get all the keys from the Dictionary
${keys} Get Dictionary Keys ${structure}
:FOR ${item} IN @{keys}
\ Log Many To Console ${item} ${structure["${item}"]}
Log Many To Console
[Arguments] ${name} ${list}
Log To Console ${name}
Log To Console ------------------------------
:FOR ${item} IN @{list}
\ Log To Console ${item}
Log To Console ------------------------------
This then results in the following console output: 然后,这将导致以下控制台输出:
==============================================================================
ConsoleLog.ConsoleLog
==============================================================================
TC
------------------------------
a
------------------------------
red
blue
green
------------------------------
b
------------------------------
head
eyes
nose
------------------------------
| PASS |
------------------------------------------------------------------------------
ConsoleLog.ConsoleLog | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.