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

标签 python testing logging pytest

我现在开始为一个新项目使用 py.test。我们正在配置 Linux 服务器,我需要编写脚本来检查这些服务器的设置和配置。我认为 py.test 是实现这些测试的好方法,并且到目前为止它工作得很好。

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

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 的工作是捕获输出并将其呈现给运算符(operator)。因此,与其尝试让 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

关于python - py.test 将消息和测试结果/断言记录到一个文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24892396/

相关文章:

python - 如何使用 "create_web_element"方法

testing - 如何获取选择器的字符串值?

python - 在独立的 pytest 测试中使用 unittest 类断言(如 assertNotIn)的最佳实践?

php - PHP 测试套件

java - logback 根本不记录到控制台

python - 如何在 python 中打印元组列表中一列的所有值?

python - 比较 Conv2D 与 Tensorflow 和 PyTorch 之间的填充

python - 在Python中迭代列表并连接字母顺序

python - 如何自动化 python 日志记录

asp.net-mvc - Asp.Net MVC 错误处理和日志记录