微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – py.test将消息和测试结果/断言记录到单个文件中

我现在开始使用py.test进行新项目.我们正在配置Linux服务器,我需要编写一个脚本来检查这些服务器的设置和配置.我认为py.test是实现这些测试的好方法,直到现在它才能正常工作.

我现在面临的问题是,在这些测试结束时我需要一个日志文件,显示每个测试的一些日志消息和测试结果.对于日志消息,我使用logger:

logging.basicConfig(filename='config_check.log', level=logging.INFO)
pytest.main()
logging.info('all done')

作为示例测试我有这个:

def test_taintedKernel():
    logging.info('checking for tainted kernel')
    output = runcmd('cat /proc/sys/kernel/tainted')
    assert output == '0', 'tainted kernel found'

所以在我的日志文件中我想要一个像这样的输出

INFO:root:checking for tainted kernel
ERROR:root:tainted kernel found
INFO:root:next test
INFO:root:successful
INFO:root:all done

但是我无法将测试结果输入到日志文件中,而是在测试后获得stdout上的标准输出

======================================= test session starts =======================================
platform linux2 -- Python 2.6.8 -- py-1.4.22 -- pytest-2.6.0
collected 14 items 

test_basicLinux.py .............F

============================================ FAILURES =============================================
_______________________________________ test_taintedKernel ________________________________________

    def test_taintedKernel():
        logging.info('checking for tainted kernel')
        output = runcmd('cat /proc/sys/kernel/tainted')
>       assert output == '0', 'tainted kernel found'
E       AssertionError: tainted kernel found

test_basicLinux.py:107: AssertionError
=============================== 1 Failed, 13 passed in 6.07 seconds ===============================

对于我的脚本用户来说,这可能会让人感到困惑.我试图进入logger和pytest_capturelog,因为这里经常提到它,但我确实做错了,因为我没有得到它.也许只是缺乏理解这是如何工作的.希望你能给我一些暗示.如果这里遗漏了什么,请告诉我.

在此先感谢您的帮助,

斯蒂芬

解决方法:

pytest的工作是捕获输出并将其呈现给运算符.因此,您可以构建日志记录到测试中,而不是尝试让pytest以您希望的方式进行日志记录.

Python的assert命令只需要一个真值和一条消息.因此,不是在测试中使用裸断言,而是可以编写一个函数来执行日志记录,如果值为false(这与触发断言失败的条件相同),然后调用断言,以便获得您想要的日志记录,以及创建控制台输出的断言驱动行为.

这是一个使用这样一个函数的小测试文件

# test_foo.py
import logging

def logAssert(test,msg):
    if not test:
        logging.error(msg)
        assert test,msg

def test_foo():
    logging.info("testing foo")
    logAssert( 'foo' == 'foo', "foo is not foo")

def test_foobar():
    logging.info("testing foobar")
    logAssert( 'foobar' == 'foo', "foobar is not foo")

这是测试运行器,非常类似于你的:

# runtests.py
import logging
import pytest

logging.basicConfig(filename='config_check.log', level=logging.INFO)
logging.info('start')
pytest.main()
logging.info('done')

这是输出

# python runtests.py
==== test session starts ========================
platform linux2 -- Python 2.6.6 -- py-1.4.22 -- pytest-2.6.0
collected 2 items

test_foo.py .F

========== FAILURES ============================
________ test_foobar __________________________

    def test_foobar():
        logging.info("testing foobar")
>       logAssert( 'foobar' == 'foo', "foobar is not foo")

test_foo.py:14:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

test = False, msg = 'foobar is not foo'

    def logAssert(test,msg):
        if not test:
            logging.error(msg)
>           assert test,msg
E           AssertionError: foobar is not foo

test_foo.py:6: AssertionError    ==== 1 Failed, 1 passed in 0.02 seconds =======

这是写入的日志:

# cat config_check.log 
INFO:root:start
INFO:root:testing foo
INFO:root:testing foobar
ERROR:root:foobar is not foo
INFO:root:done

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐