在`robot framework`中将字典记录到控制台

时间:2018-06-08 16:03:22

标签: python robotframework

我有这样的字典

{ a:{red ,blue, green}, b: {head, eyes, nose} }

我希望在控制台中以这样的格式打印出来。

------------
a
------------
red
blue
green
-------------
b
-------------
head
eyes
nose
-------------

由于robot framework不支持嵌套循环,我发现很难做到这一点。我想在作业控制台中处理这个,而不是log.html

2 个答案:

答案 0 :(得分:5)

这将只用一个循环打印您想要的console

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)

答案 1 :(得分:2)

尽管Python解决方案可能会在格式化等方面提供更多灵活性,但Robot中的开箱即用关键字足以创建所需的逻辑。请参阅下面的代码示例和输出:

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

然后会产生以下控制台输出:

==============================================================================
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