How do I prevent nosetests from interspersing logging output inside the output from its tests? I've just adding logging to my Django code like this:
import logging
logger = logging.getLogger(__name__)
def home_page(request, template):
device = get_device_capabilities(request)
device_type = get_device_type(device)
logger.info("device_type = " + device_type)
logger.info("screen_width = " + str(screen_width))
When I run the tests like this:
nosetests --nocapture
I get this:
[04/15/2014 02:42:57 PM] INFO [apps.home.views:24] device_type = computer
[04/15/2014 02:42:57 PM] INFO [apps.home.views:25] screen_width = 800
....................................................................................................................................................................................................................................
----------------------------------------------------------------------
Ran 230 tests in 42.521s
OK
I'm just getting started with raising exceptions and logging information and I certainly don't want my test output littered with the output from my logger statements. I thought the "--nocapture" flag was supposed to prevent this. I've skimmed all the nosetest documentation and didn't see anything else that would help. Am I missing something? Is there a way to stop nosetests from including my logger messages in its output?
Thanks!
Thanks to those who provided an answer to my question. I chose not to implement @amezhenin's solution as it was too different from how I run my tests and I didn't want to change. @Oleksiy's solutions got rid of some logging messages but not all of them. I didn't quite get what @gardenunez was getting at but that's my fault.
After more research, I realized that I was specifying the nosetest argument incorrectly. It's not --nocapture
as I indicated in my initial question but rather --nologcapture
. When I specified this argument, all of my logging messages were hidden.
You can always run nose with --logging-clear-handlers
to clear all other logging handlers.
I can suggest you to put something like this into your settings.py
:
if 'test' in sys.argv:
# disable loggers output
LOGGING["loggers"] = {}
btw, I using nose
and doctests
for testing purposes so my full template looks like this:
if 'test' in sys.argv:
# add Nose to INSTALLED_APPS for running tests
INSTALLED_APPS = INSTALLED_APPS + ('django_nose',)
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
NOSE_ARGS = ['--with-doctest']
# change DB to sqlite3, when running test for speedup
DATABASES['default'] = {'ENGINE': 'django.db.backends.sqlite3'}
# disable loggers output
LOGGING["loggers"] = {}
UPD. Forgot to tell that I run test with python manage.py test
, that is why I can refer to 'test'
in sys.argv
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With