即使测试通过,也让鼻子测试跑步者显示日志记录 [英] Make nose test runner show logging even if tests pass

查看:81
本文介绍了即使测试通过,也让鼻子测试跑步者显示日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nosetests test.py来运行单元测试:

I am using nosetests test.py to run unit tests:

import unittest
import logging


class Test(unittest.TestCase):
    def test_pass(self):
        logging.getLogger('do_not_want').info('HIDE THIS')
        logging.getLogger('test').info('TEST PASS')
        self.assertEqual(True, True)

    def test_fail(self):
        logging.getLogger('do_not_want').info('HIDE THIS')
        logging.getLogger('test').info('TEST FAIL')
        self.assertEqual(True, False)

测试失败时,它将打印出所有日志记录信息.我可以使用--logging-filter仅过滤掉某些记录器:

When test fails, it prints out all logging info. I can use --logging-filter to filer out only some loggers:

nosetests test.py --verbosity=2 --logging-filter=test
test_fail (test.Test) ... FAIL
test_pass (test.Test) ... ok

======================================================================
FAIL: test_fail (test.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../test.py", line 14, in test_fail
    self.assertEqual(True, False)
AssertionError: True != False
-------------------- >> begin captured logging << --------------------
test: INFO: TEST FAIL
--------------------- >> end captured logging << ---------------------

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (failures=1)

但是,测试通过时它不会显示任何内容.

我希望在测试通过时看到一个特定记录器的输出.我发现我可以使用-s来显示所有的stdout/stderr文本,而这并不是我真正需要的-它可以打印所有内容.我尝试使用各种设置,例如--nologcapture--nocapture--logging-filter,但是我无法获得理想的效果.

I would like to see output of one specific logger when tests pass. I have found that I can use -s to show all stdout / stderr text which is not exactly what I need - it prints everything. I tried to play with various settings such as --nologcapture, --nocapture, or --logging-filter but I was not able to get desired effect.

推荐答案

nosetests --help一点都不明显,但是答案是--debug标志.此标志将您要从中接收消息的记录器的名称作为参数.

nosetests --help does not make this obvious AT ALL, but the answer is the --debug flag. This flag takes as an argument the name of the logger that you'd like to receive messages from.

以下是OP代码的略微修改版本:

Here's a slightly modified version of the OP's code:

# test.py
import unittest
import logging

class Test(unittest.TestCase):
    def test_pass(self):
        logging.getLogger('hide.this').info('HIDE THIS')
        logging.getLogger('show.this').info('TEST PASS')
        self.assertEqual(True, True)

    def test_fail(self):
        logging.getLogger('hide.this').info('HIDE THIS')
        logging.getLogger('show.this').info('TEST FAIL')
        self.assertEqual(True, False)

在此示例中,nosetests test.py --debug=show.this应该可以解决问题.

For this example, nosetests test.py --debug=show.this should do the trick.

这篇关于即使测试通过,也让鼻子测试跑步者显示日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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