Avoid warnings on 404 during django test runs?

Viewed 3534

Just upgraded to Django 1.3. My test suite is now showing a bunch of useless warnings like this every time I check that a given URL is 404. It didn't do that under Django 1.2.

For example, let's say we have views and URLs wired up such that this test passes:

def test_foo(self):
    response = self.client.get('/foo/bar/')
    self.assertEqual(response.status_code, 200)
    response = self.client.get('/foo/bar2/')
    self.assertEqual(response.status_code, 404)

Although the test passes, the 404 (which we expect) triggers a warning to the console:

.WARNING Not Found: /foo/bar2/

This is just useless noise; I have about 30 of them in one of my current test suites.

Is there a way to silence those just during tests? I'd like to leave them on during normal operation. And I don't think I want to filter out all warnings from the 'django.request' logger.

3 Answers

A solution implementing @jterrace's solution is to decrease the log level in the setUp and tearDown methods:

class NotFoundTests(TestCase):
    def setUp(self) -> None:
        """Reduce the log level to avoid errors like 'not found'"""
        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)

Just copy/paste to the TestCase class with tests that check for pages that don't get found.

Related