Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

enter image description here

like image 924
rcubefather Avatar asked Oct 11 '15 13:10

rcubefather


1 Answers

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")
like image 194
Bryan Oakley Avatar answered Sep 28 '22 02:09

Bryan Oakley