【问题标题】:Log dictionary to console in `robot framework`在“机器人框架”中将字典记录到控制台
【发布时间】:2018-06-11 04:54:32
【问题描述】:

我有一本这样的字典

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

我想在控制台中打印出来,格式如下。

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

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

【问题讨论】:

  • 是的。如果我在 Python 代码中打印任何内容,它会打印在 log.html 而不是控制台中。有没有办法覆盖它?
  • Robot Framework 中的嵌套循环是通过调用包含嵌套循环的关键字来支持的。比用 Python 编程要少一些传统。 Robot Framework 用户指南有一个关于Nested Loops 的特定部分

标签: python robotframework


【解决方案1】:

这将只用一个循环将您想要的内容打印到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)

【讨论】:

    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-09
      • 2021-04-10
      • 2017-10-13
      • 2014-10-31
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      相关资源
      最近更新 更多