测试执行后如何在机器人框架日志文件中记录命令输出?
How log the command output in the Robot framework log file after test execution?
在 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::"
有人可以指导我吗?
如果您希望命令的输出出现在您的日志中,可以使用三种方法:使用打印语句、使用日志记录 API 或使用内置的 log 关键字。这些方法都记录在 robot framework users guide.
在这三个中,日志记录 API 可以说是最佳选择。
使用打印语句
您已经在使用此方法。这记录在用户指南中名为 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) ...
示例:
def test():
cmd = ' net test" '
output = os.popen(cmd).read()
match1 = re.findall('.* (successfully).*',output)
mat1 = ['successfully']
if match1 == mat1:
print "output: " + output
使用日志记录API
有一个 public API 用于日志记录,也记录在用户指南中
在名为 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.
示例:
from robot.api import logger
def test():
...
logger.info("output: " + output)
使用内置的 Log 关键字
最后,你还可以使用内置的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.
示例:
from robot.libraries import BuiltIn
...
def test():
...
BuiltIn().log("this is a debug message", "DEBUG")
在 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::"
有人可以指导我吗?
如果您希望命令的输出出现在您的日志中,可以使用三种方法:使用打印语句、使用日志记录 API 或使用内置的 log 关键字。这些方法都记录在 robot framework users guide.
在这三个中,日志记录 API 可以说是最佳选择。
使用打印语句
您已经在使用此方法。这记录在用户指南中名为 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) ...
示例:
def test():
cmd = ' net test" '
output = os.popen(cmd).read()
match1 = re.findall('.* (successfully).*',output)
mat1 = ['successfully']
if match1 == mat1:
print "output: " + output
使用日志记录API
有一个 public API 用于日志记录,也记录在用户指南中 在名为 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.
示例:
from robot.api import logger
def test():
...
logger.info("output: " + output)
使用内置的 Log 关键字
最后,你还可以使用内置的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.
示例:
from robot.libraries import BuiltIn
...
def test():
...
BuiltIn().log("this is a debug message", "DEBUG")