Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make nose test runner show logging even if tests pass

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.

like image 688
Fenikso Avatar asked Sep 14 '15 13:09

Fenikso


1 Answers

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.

like image 96
BrianTheLion Avatar answered Sep 20 '22 05:09

BrianTheLion