How can I disable logging while running unit tests in Python Django

0 votes

I am using a simple unit test based test runner to test my Django application.

My application itself is configured to use a basic logger in settings.py using:

logging.basicConfig(level=logging.DEBUG)

And in my application code using:

logger = logging.getLogger(__name__)
logger.setLevel(getattr(settings, 'LOG_LEVEL', logging.DEBUG))

However, when running unittests, I'd like to disable logging so that it doesn't clutter my test result output. Is there a simple way to turn off logging in a global way, so that the application specific loggers aren't writing stuff out to the console when I run tests?

Jun 23, 2020 in Python by kartik
• 37,510 points
8,371 views

1 answer to this question.

0 votes

Hello @kartik,

Since you are in Django, you could add these lines to your settings.py:

import sys
import logging

if len(sys.argv) > 1 and sys.argv[1] == 'test':
    logging.disable(logging.CRITICAL)

That way you don't have to add that line in every setUp() on your tests.

You could also do a couple of handy changes for your test needs this way.

There is another "nicer" or "cleaner" way to add specifics to your tests and that is making your own test runner.

Just create a class like this:

import logging

from django.test.simple import DjangoTestSuiteRunner
from django.conf import settings

class MyOwnTestRunner(DjangoTestSuiteRunner):
    def run_tests(self, test_labels, extra_tests=None, **kwargs):

        # Don't show logging messages while testing
        logging.disable(logging.CRITICAL)

        return super(MyOwnTestRunner, self).run_tests(test_labels, extra_tests, **kwargs)

And now add to your settings.py file:

TEST_RUNNER = "PATH.TO.PYFILE.MyOwnTestRunner"
#(for example, 'utils.mytest_runner.MyOwnTestRunner'

Hope this is helpfull!
Thank You!

answered Jun 23, 2020 by Niroj
• 82,880 points

Related Questions In Python

0 votes
1 answer

Writing unit tests in Python: How do I start?

If you're brand new to using unittests, ...READ MORE

answered Sep 18, 2018 in Python by Priyaj
• 58,090 points
713 views
0 votes
1 answer

How should I write tests for Forms in Django with Python?

from django.tests import TestCase class MyTests(TestCase): ...READ MORE

answered Nov 20, 2018 in Python by Nymeria
• 3,560 points
1,179 views
0 votes
1 answer

How to run django unit-tests on production database in Python?

In case someone googles here searching for ...READ MORE

answered Nov 28, 2018 in Python by Nymeria
• 3,560 points
1,275 views
0 votes
1 answer

How can I have only positive decimal numbers in Django using Python?

Hi, are you aware of something called ...READ MORE

answered Jan 30, 2019 in Python by Nymeria
• 3,560 points
5,838 views
0 votes
3 answers

Python Selenium best tutorials for beginners

Hope this will help you...Python Tutorial READ MORE

answered Feb 11, 2019 in Python by aldrinjohn
• 140 points
3,440 views
0 votes
1 answer

How can I disable the Django Celery admin modules?

Hello @kartik, To be more specific, in admin.py of any ...READ MORE

answered Jun 12, 2020 in Python by Niroj
• 82,880 points
1,260 views
0 votes
1 answer

How Can I delete a record in Django models?

Hello @kartik, If you want to delete one ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
1,490 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP