机器人框架不创建文件或写入文件

2024-01-10

我有一个Python脚本,它接受日志级别并将其设置为默认级别,因此一旦日志消息通过,它将根据级别层次结构打印或不打印(即,如果默认为DEBUG,则打印所有消息,仅打印CRITICAL & 错误消息(如果默认为错误)。

我的Python代码是这样的:

# Sets default log level.
def set_default_level(self,level):
    levels = {
        'DEBUG': logging.DEBUG,
        'INFO': logging.INFO,
        'WARNING': logging.WARNING,
        'ERROR': logging.ERROR,
        'CRITICAL': logging.CRITICAL
    }

    # Sets up configuration for logging message.
    logging.basicConfig(format='%(levelname)s: %(message)s', filename = 'log_file.log', filemode = 'w', level=levels[level])

def log_message(self, lvl, message):
    msg_print = {
        '[DEBUG]': logging.debug,
        '[INFO]': logging.info,
        '[WARNING]': logging.warning,
        '[ERROR]': logging.error,
        '[CRITICAL]': logging.critical
    } 
    msg_print[lvl](message)

这段代码在单独运行 Python 时确实可以工作。但是,当我使用 Robot Framework 运行此代码时,它不会创建/写入文件。我目前的框架是这样的:

Test Validate Info Prints
    [Documentation]     Checks if all BUT debug messages are printed.
    ...                 Output should not contain any DEBUG level messages.
    Set Default Level   ${INFO_LVL}
    Log Message         ${DEBUG}               ${DEBUG_MSG}
    Log Message         ${INFO}                ${INFO_MSG}
    Log Message         ${ERROR}               ${ERROR_MSG}
    Log Message         ${WARNING}             ${WARNING_MSG}
    Log Message         ${CRITICAL}            ${CRITICAL_MSG}

    ${LogFile}=         Get File                ./log_file.log

    Should contain      ${LogFile}      this build has no associated authentication!
    Should contain      ${LogFile}      S3 Bucket connected successfully
    Should contain      ${LogFile}      No working Internet connection available
    Should contain      ${LogFile}      Application has failed.

    Should not contain   ${LogFile}      please debug this

这只是测试 INFO 默认级别。其他测试用例与此测试用例非常相似,但它们都在文件创建方面存在相同的问题。我已经研究过这个问题,但没有发现任何有用的东西。我一开始使用 PowerShell 来运行框架,然后我切换到 Git BASH 并出现同样的问题。我也在 Windows 7 上运行。

提前致谢!


尝试用你的代码!我开始相信这是预期的行为。

为什么?因为机器人框架中的日志记录必须使用您在代码中使用的相同“日志记录”python 库进行处理。

因此,当您实际传递 [INFO]、[DEBUG] 或任何其他消息时,您实际上并没有创建新的记录器,这与将其传递给 Robot Framework 的现有记录器一样好!因此我们可以在robot框架的log.html中看到所有消息。如下:

20161209 12:24:58.497   WARN    Application has failed.     
20161209 12:24:58.499   WARN    No working Internet connection available    
20161209 12:24:58.501   WARN    this build has no associated authentication!

虽然这只是一个想法!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

机器人框架不创建文件或写入文件 的相关文章

随机推荐