Python
Django
Unit Testing
Logging
Software Development

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Logging noise during Django test runs can bury the failures you actually care about. The right fix depends on scope: you can silence logging globally for the whole test run, override Django logging settings in tests, or selectively mute only the loud loggers.

The Fastest Global Mute

If the goal is simply "make test output quiet," Python's logging module can disable messages at or below a chosen level.

python
import logging

logging.disable(logging.CRITICAL)

A practical place to do this is in a test setup module that runs early, such as a base test class or a dedicated test settings import path.

For example:

python
1import logging
2from django.test import TestCase
3
4
5class QuietTestCase(TestCase):
6    @classmethod
7    def setUpClass(cls):
8        super().setUpClass()
9        logging.disable(logging.CRITICAL)
10
11    @classmethod
12    def tearDownClass(cls):
13        logging.disable(logging.NOTSET)
14        super().tearDownClass()

This is blunt but effective.

Django Settings-Based Approach

If you want quieter tests without globally muting Python logging everywhere, define a test-specific logging configuration.

python
1LOGGING = {
2    "version": 1,
3    "disable_existing_loggers": True,
4}

A more selective version routes logs to NullHandler:

python
1LOGGING = {
2    "version": 1,
3    "disable_existing_loggers": False,
4    "handlers": {
5        "null": {
6            "class": "logging.NullHandler",
7        },
8    },
9    "loggers": {
10        "django": {
11            "handlers": ["null"],
12            "level": "CRITICAL",
13            "propagate": False,
14        },
15        "myapp": {
16            "handlers": ["null"],
17            "level": "CRITICAL",
18            "propagate": False,
19        },
20    },
21}

Put that in a test settings module and run tests with:

bash
python manage.py test --settings=myproject.settings_test

This is usually the cleanest approach for larger Django projects.

Temporarily Muting Logs in One Test

Sometimes you only want to silence logging for a noisy test case while leaving the rest of the suite alone.

python
1import logging
2from django.test import TestCase
3
4
5class ReportTests(TestCase):
6    def setUp(self):
7        logging.disable(logging.CRITICAL)
8
9    def tearDown(self):
10        logging.disable(logging.NOTSET)
11
12    def test_report_generation(self):
13        self.assertTrue(True)

That keeps the scope tight, though it can become repetitive if many tests need the same behavior.

Do Not Confuse Silencing with Testing Logs

Some tests should assert logging behavior rather than suppress it. In that case, use tools such as assertLogs or your test runner's capture features instead of muting the logger completely.

python
1import logging
2from django.test import SimpleTestCase
3
4
5logger = logging.getLogger("myapp")
6
7
8class LoggingTests(SimpleTestCase):
9    def test_warning_is_emitted(self):
10        with self.assertLogs("myapp", level="WARNING") as captured:
11            logger.warning("something happened")
12
13        self.assertIn("something happened", captured.output[0])

If logging is part of the behavior, do not turn it off for that test.

Why Django Logging Can Feel Noisy

Django projects often inherit logs from several places:

  • Django itself
  • your application loggers
  • database backends
  • third-party packages

That is why muting only one logger may not seem to help much. If the output is still noisy, identify which logger names are actually producing the messages before changing settings blindly.

A Practical Recommendation

For most Django projects:

  1. keep a dedicated settings_test.py
  2. lower or disable noisy loggers there
  3. use targeted log assertions in tests that care about log output

That gives you clean test runs without hiding behavior you genuinely want to verify.

Common Pitfalls

The biggest pitfall is calling logging.disable(logging.CRITICAL) and forgetting to re-enable logging later in the same process. That can make debugging other tests harder.

Another pitfall is setting disable_existing_loggers=True without realizing how broadly that affects third-party logging behavior.

A third pitfall is muting everything when the real problem is just one noisy logger, such as a database backend or an HTTP client library.

Finally, do not silence logs in tests that are explicitly meant to validate logging side effects.

Summary

  • You can silence Django test logging globally with logging.disable(...)
  • A dedicated test settings module is usually the cleanest long-term approach
  • 'NullHandler and per-logger overrides let you mute only the noisy parts'
  • Use assertLogs when log output is part of the behavior under test
  • Be deliberate about scope so you do not hide useful diagnostics unintentionally

Course illustration
Course illustration

All Rights Reserved.