Make nose test runner show logging even if tests pass

依然范特西╮ 提交于 2019-12-04 15:28:42

问题


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)

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)

However, it does not show anything when tests pass.

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.


回答1:


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.

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)

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



来源:https://stackoverflow.com/questions/32565562/make-nose-test-runner-show-logging-even-if-tests-pass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!