【问题标题】:py.test logging messages and test results/assertions into a single filepy.test 将消息和测试结果/断言记录到单个文件中
【发布时间】:2014-09-13 13:40:27
【问题描述】:

我现在开始为一个新项目使用 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

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

======================================= 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,因为这里经常提到它,但我肯定做错了什么,因为我只是不明白。也许只是缺乏了解这是如何真正起作用的。希望你能给我一些提示。如果这里有什么遗漏,请告诉我。

提前感谢您的帮助,

斯蒂芬

【问题讨论】:

  • py.test 在产生非常有用的输出方面非常出色,但输出在第一刻看起来很混乱。日志记录也是一种相当原始的通信方式,我不希望它有太大的改进。如果您的用户不支持py.test 所说的样式,我建议将输出保存到单元测试xml(使用--junit-xml xunit.xml 选项)并将其发送给您,或者放弃并搜索其他解决方案。跨度>
  • 感谢约翰的回答,我找到了一种适合我的方法。但你是对的,也许这不是 py.test 的真正用途。但它节省了我在编写和运行测试方面的大量工作,所以我对一个让我保留 py.test 的解决方案感到满意

标签: python testing logging pytest


【解决方案1】:

pytest 的工作是捕获输出并将其呈现给操作员。因此,与其尝试让 pytest 按您希望的方式进行日志记录,不如将日志记录构建到您的测试中。

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

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

# 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

【讨论】:

    【解决方案2】:

    从 3.3 版开始,pytest 支持实时记录到终端和文件。示例测试模块:

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

    配置日志到文件可以在pytest.ini:

    [pytest]
    log_file = my.log
    log_file_level = DEBUG
    log_file_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
    log_file_date_format=%Y-%m-%d %H:%M:%S
    

    像往常一样运行测试:

    $ pytest
    ======================================================= test session starts ========================================================
    ...
    collected 1 item                                                                                                                   
    
    test_spam.py .                                                                                                               [100%]
    
    ===================================================== 1 passed in 0.01 seconds =====================================================
    

    现在检查写入的日志文件:

    $ cat my.log
    2019-07-12 23:51:41 [    INFO] checking for tainted kernel (test_spam.py:6)
    

    有关向终端和日志文件发送实时日志的更多示例,请查看我对Logging within py.test tests 的回答。

    参考:pytest 文档中的Live Logs 部分。

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-11
      • 2019-08-16
      • 2016-06-16
      • 1970-01-01
      相关资源
      最近更新 更多