【问题标题】:How log the command output in the Robot framework log file after test execution?测试执行后如何在 Robot framework 日志文件中记录命令输出?
【发布时间】:2016-01-09 00:40:34
【问题描述】:

在 Robot Framework log.html 中,我想记录我从 python 文件执行的命令输出。如随附的 log.html 屏幕截图所示,现在我无法看到命令输出。很简单,它会打印或记录为 PASS。

我的机器人文件:

*** Settings ***
Library         test


*** Test cases ***
check
    test

Python 关键字:

def test():
    cmd = ' net test" '
    output = os.popen(cmd).read()
    match1 = re.findall('.* (successfully).*',output)
    mat1 = ['successfully']
    if match1 == mat1:
        print "PASS::"

有人可以指导我吗?

【问题讨论】:

    标签: robotframework


    【解决方案1】:

    如果您希望命令的输出出现在您的日志中,有三种方法可以做到:使用 print 语句、使用日志记录 API 或使用内置的 log 关键字。这些方法都记录在robot framework users guide中。

    在这三个中,日志 API 可以说是最佳选择。

    使用打印语句

    您已经在使用此方法。这在用户指南中的一个名为 Logging information 的部分中进行了记录:

    ... 方法也可以发送消息到日志 文件只需写入标准输出流 (stdout) 或 标准错误流 (stderr) ...

    例子:

    def test():
        cmd = ' net test" '
        output = os.popen(cmd).read()
        match1 = re.findall('.* (successfully).*',output)
        mat1 = ['successfully']
        if match1 == mat1:
            print "output: " + output
    

    使用日志记录 API

    有一个用于记录的公共 API,也记录在用户指南中 在名为Public API for logging的部分中:

    Robot Framework 有一个基于 Python 的日志记录 API,用于将消息写入 日志文件和控制台。测试库可以像这样使用这个 API logger.info('My message') 而不是通过标准记录 输出类似于 print 'INFO My message'。除了程序化 界面使用起来更加简洁,这个 API 的一个好处是 日志消息具有准确的时间戳。

    例子:

    from robot.api import logger
    def test():
        ...
        logger.info("output: " + output)
    

    使用内置的 Log 关键字

    最后,您还可以使用内置的log 关键字。用户指南中标题为 Using BuiltIn Library 的部分中记录了使用内置关键字。

    用 Python 实现的测试库可以使用 Robot Framework 的 内部模块,例如,获取有关执行的信息 测试和使用的设置。这种强大的机制 但是,与框架进行通信时应谨慎使用, 因为并非所有 Robot Framework 的 API 都可供以下用户使用 在外部,它们可能会在不同的框架之间发生根本性的变化 版本。

    例子:

    from robot.libraries import BuiltIn
    ...
    def test():
        ...
        BuiltIn().log("this is a debug message", "DEBUG")
    

    【讨论】:

      猜你喜欢
      • 2022-01-27
      • 2016-08-12
      • 1970-01-01
      • 2015-10-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      • 2020-11-23
      相关资源
      最近更新 更多