Search code examples
pythondjangopycharmdjango-south

See django.db.backends logs in stdout on pycharm


I got a problem on Pycharm.

When I run my tests I can't display django.db.backends logs on stdout/stderr.

When I use runserver it works like a charm.

Do you have any simple solution to display all logs with level=DEBUG on stdout on my tests?

More info:

  • my DEBUG is set to True in my settings
  • I use SOUTH_TESTS_MIGRATE = False and SKIP_SOUTH_TESTS = True

Thank you


Solution

  • First, django forces settings.DEBUG to False during UnitTest (Writing and running tests in Django). So you should activate temporary this settings using override_settings decorator:

    From django documentation:

    from django.test import TestCase
    from django.test.utils import override_settings
    
    class LoginTestCase(TestCase):
    
        @override_settings(LOGIN_URL='/other/login/')
        def test_login(self):
            response = self.client.get('/sekrit/')
            self.assertRedirects(response, '/other/login/?next=/sekrit/')
    

    But reading the django source code, you can also set the use_debug_cursor attribute of the connection object. If set to True, the CursorDebugWrapper will be used and queries logs will appear.

    Extract from django source (django/db/backends/__init__.py):

    class BaseDatabaseWrapper(object):
        # [...]
        def cursor(self):
            self.validate_thread_sharing()
            if (self.use_debug_cursor or
                (self.use_debug_cursor is None and settings.DEBUG)):
                cursor = self.make_debug_cursor(self._cursor())
            else:
                cursor = util.CursorWrapper(self._cursor(), self)
            return cursor
    
        def make_debug_cursor(self, cursor):
            return util.CursorDebugWrapper(cursor, self)