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': 'mail@mail.mail',
'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
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)