28

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.

Fenikso
  • 9,251
  • 5
  • 44
  • 72
  • The logcapture plugin is only ~250 lines. You can probably subclass or adapt it quite easily to do what you want. You can find it [here](https://github.com/nose-devs/nose/blob/master/nose/plugins/logcapture.py). – rkrzr Sep 14 '15 at 13:55
  • @Fenikso Did you find a solution for the problem? – Stefan Nov 15 '15 at 15:11
  • @Stefan No, it does not seem to exist in Nose. I am using a few ugly workarounds when needed, such as writing to file. – Fenikso Nov 15 '15 at 17:26
  • @Fenikso Thank you for your answer. It's a shame that nose doesn't support that... – Stefan Nov 15 '15 at 18:30

2 Answers2

25

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.

BrianTheLion
  • 2,618
  • 2
  • 29
  • 46
-2

I'm not sure if this is what you want.

In my case I use:

nosetest -v -s

-s, --nocapture
Don't capture stdout (any stdout output will be printed immediately) [NOSE_NOCAPTURE]

HTH