NelttjeN
NelttjeN

Reputation: 90

Disable logging bad requests while unittest django app

I have a tests in my Django app. They're working well, but i want to disable showing console logging like .Bad Request: /api/v1/users/register/

One of my tests code

 def test_user_register_username_error(self):
        data = {
            'username': 'us',
            'email': '[email protected]',
            'password': 'pass123123',
            'password_again': 'pass123123'
        }

        url = self.register_url

        response = client.post(url, data=data)

        self.assertEqual(response.status_code, 400)
        self.assertFalse(User.objects.filter(username='us').first())

Console output

Found 1 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.Bad Request: /api/v1/users/register/
----------------------------------------------------------------------
Ran 1 tests in 0.414s

OK

Everything works nice, but i want to disable Bad Request: /api/v1/users/register/ output to console. I double checked, there's no print or logging functions, that can possibly log this to console.

How can i disable messages like Bad Request: /api/v1/users/register/ logging while tests

EDIT

To make question more understandable here's current console output:

Found 22 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
...........Bad Request: /api/v1/users/register/
.Bad Request: /api/v1/users/register/
.Bad Request: /api/v1/users/register/
.Bad Request: /api/v1/users/register/
.Bad Request: /api/v1/users/register/
..Bad Request: /api/v1/users/register/
.Bad Request: /api/v1/users/activate/
.Bad Request: /api/v1/users/login/
.Bad Request: /api/v1/users/recovery/
Bad Request: /api/v1/users/recovery/
.Bad Request: /api/v1/users/register/
.
----------------------------------------------------------------------
Ran 22 tests in 6.350s

OK

And what i expect:

Found 22 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
......................
----------------------------------------------------------------------
Ran 22 tests in 6.350s

OK

Upvotes: 3

Views: 657

Answers (2)

Matej
Matej

Reputation: 820

You can achieve this using filters. In settings.py add:

import sys

class NotInTestingFilter(Filter):
    def filter(self, record):
        return (
            "pytest" in sys.modules
            or "django.test" in sys.modules
            or "django_webtest" in sys.modules
        )

and to the LOGGING variable (https://docs.djangoproject.com/en/5.0/topics/logging/) inside settings.py add:

   "filters": {
        "testing": {"()": NotInTestingFilter},
    },
     "loggers": {
        "django.request": {
            "handlers": ["console"],
            "level": "ERROR",
            "filters": ["testing"],
            "propagate": False,
        },
    }

Upvotes: 2

F_C_T_L
F_C_T_L

Reputation: 76

You can log only errors during the tests, and after they complete, return the normal logging level.

class SampleTestCase(TestCase):
    def setUp(self) -> None:
        """Reduce the log level to avoid messages like 'bad request'"""
        logger = logging.getLogger("django.request")
        self.previous_level = logger.getEffectiveLevel()
        logger.setLevel(logging.ERROR)

    def tearDown(self) -> None:
        """Reset the log level back to normal"""
        logger = logging.getLogger("django.request")
        logger.setLevel(self.previous_level)

Upvotes: 2

Related Questions