How log the command output in the Robot framework log file after test execution?

非 Y 不嫁゛ 提交于 2021-02-07 03:51:22

问题


In Robot Framework log.html, I want to log the command output that I am executing from a python file . As shown in the attached screenshot of log.html, now I am not able to see the command output. Simple it prints or logs as PASS.

My Robot File:

*** Settings ***
Library         test


*** Test cases ***
check
    test

Python Keyword:

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

Can anyone guide me on this please?


回答1:


If you want the output of the command to appear in your log, there are three ways to do it: using the print statement, using the logging API, or using the built in log keyword. These methods are all documented in the robot framework users guide.

Of the three, the logging API is arguably the best choice.

Using print statements

You're already using this method. This is documented in the user guide, in a section named Logging information:

... methods can also send messages to log files simply by writing to the standard output stream (stdout) or to the standard error stream (stderr) ...

Example:

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

Using the logging API

There is a public API for logging, also documented in the user guide in a section named Public API for logging:

Robot Framework has a Python based logging API for writing messages to the log file and to the console. Test libraries can use this API like logger.info('My message') instead of logging through the standard output like print 'INFO My message'. In addition to a programmatic interface being a lot cleaner to use, this API has a benefit that the log messages have accurate timestamps.

Example:

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

Using the built-in Log keyword

Finally, you can also use the built-in log keyword. Using the built in keywords is documented in the user guide in a section titled Using BuiltIn Library.

Test libraries implemented with Python can use Robot Framework's internal modules, for example, to get information about the executed tests and the settings that are used. This powerful mechanism to communicate with the framework should be used with care, though, because all Robot Framework's APIs are not meant to be used by externally and they might change radically between different framework versions.

Example:

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


来源:https://stackoverflow.com/questions/33065809/how-log-the-command-output-in-the-robot-framework-log-file-after-test-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!