如何在测试执行后记录Robot框架日志文件中的命令输出?

时间:2015-10-11 13:56:01

标签: robotframework

在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::"

有人可以指导我吗?

enter image description here

1 个答案:

答案 0 :(得分:5)

如果希望命令的输出显示在日志中,有三种方法:使用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('我的消息')而不是登录标准   输出如print' INFO 我的留言'。除了程序化   界面使用起来要干净得多,这个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")