py.test将消息和测试结果/断言记录到单个文件中 [英] py.test logging messages and test results/assertions into a single file

查看:428
本文介绍了py.test将消息和测试结果/断言记录到单个文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我开始使用py.test进行一个新项目.我们正在配置Linux服务器,我需要编写脚本来检查这些服务器的设置和配置.我认为py.test是实施这些测试的好方法,并且到现在为止还算不错.

I am starting to work with py.test at the moment for a new project. We are provisioning Linux servers and I need to write a script to check the setup and configuration of these servers. I thought that py.test is a good way to implement these tests and it is working quite fine until now.

我现在面临的问题是,在这些测试的末尾需要一个日志文件,其中显示了每个测试的一些日志消息以及测试结果.对于日志消息,我使用记录器:

The problem I face right now is that I need a log file at the end of these tests showing some log messages for each test and the result of the test. For the log messages I use logger:

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

作为示例测试,我有这个:

As an example test I have this:

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

所以在我的日志文件中,我想要这样的输出:

So in my logfile I would like an output like that:

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

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

But I cannot get the test results into the logfile, instead I get the standard output on stdout after the tests:

======================================= 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,因为在这里经常提到它,但是我确定做错了什么,因为我不明白.可能只是缺乏对这是如何工作的了解.希望你能给我一些提示.请让我知道这里是否有任何遗漏.

This may be quite confusing for the users of my script. I tried to get into logger and pytest_capturelog since it was mentioned here quite often but I am for sure doing something wrong since I just don't get it. Maybe just a lack of understanding how this really works. Hope you can give me some hints on this. Please let me know if anything is missing here.

预先感谢您的帮助,

斯蒂芬

推荐答案

pytest的工作是捕获输出并将其呈现给操作员.因此,您可以尝试将pylog内置到测试中,而不是试图让pytest进行所需的日志记录.

pytest's job is to capture output and present it to the operator. So, rather than trying to get pytest to do the logging the way you want it, you can build the logging into your tests.

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

Python's assert command just takes a truth value, and a message. So, instead of using a bare assert in your tests, you can write a small function that does the logging if the value is false (which is the same condition that triggers the assert to fail), then calls the assert, so that you get the logging you want, plus the assert-driven behavior that creates the console output.

这是使用此类功能的小型测试文件:

Here's a small test file using such a function:

# 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")

这是测试跑步者,与您非常相似:

Here's the test runner, very similar to yours:

# 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

这篇关于py.test将消息和测试结果/断言记录到单个文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆