Navigation Menu

Skip to content

Commit

Permalink
Fixed #19546 - ensure that deprecation warnings are shown during tests
Browse files Browse the repository at this point in the history
refs #18985
  • Loading branch information
ptone committed Jan 9, 2013
1 parent 1884868 commit cfa70d0
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 8 deletions.
13 changes: 13 additions & 0 deletions django/test/simple.py
@@ -1,3 +1,4 @@
import logging
import unittest as real_unittest import unittest as real_unittest


from django.conf import settings from django.conf import settings
Expand Down Expand Up @@ -365,7 +366,19 @@ def run_tests(self, test_labels, extra_tests=None, **kwargs):
self.setup_test_environment() self.setup_test_environment()
suite = self.build_suite(test_labels, extra_tests) suite = self.build_suite(test_labels, extra_tests)
old_config = self.setup_databases() old_config = self.setup_databases()
if self.verbosity > 0:
# ensure that deprecation warnings are displayed during testing
# the following state is assumed:
# logging.capturewarnings is true
# a "default" level warnings filter has been added for
# DeprecationWarning. See django.conf.LazySettings._configure_logging
logger = logging.getLogger('py.warnings')
handler = logging.StreamHandler()
logger.addHandler(handler)
result = self.run_suite(suite) result = self.run_suite(suite)
if self.verbosity > 0:
# remove the testing-specific handler
logger.removeHandler(handler)
self.teardown_databases(old_config) self.teardown_databases(old_config)
self.teardown_test_environment() self.teardown_test_environment()
return self.suite_result(suite, result) return self.suite_result(suite, result)
23 changes: 15 additions & 8 deletions tests/regressiontests/logging_tests/tests.py
Expand Up @@ -93,24 +93,31 @@ class WarningLoggerTests(TestCase):
and captured to the logging system and captured to the logging system
""" """
def setUp(self): def setUp(self):
# this convoluted setup is to avoid printing this deprecation to
# stderr during test running - as the test runner forces deprecations
# to be displayed at the global py.warnings level
self.logger = logging.getLogger('py.warnings') self.logger = logging.getLogger('py.warnings')
self.old_stream = self.logger.handlers[0].stream self.outputs = []
self.old_streams = []
for handler in self.logger.handlers:
self.old_streams.append(handler.stream)
self.outputs.append(StringIO())
handler.stream = self.outputs[-1]


def tearDown(self): def tearDown(self):
self.logger.handlers[0].stream = self.old_stream for i, handler in enumerate(self.logger.handlers):
self.logger.handlers[i].stream = self.old_streams[i]


@override_settings(DEBUG=True) @override_settings(DEBUG=True)
def test_warnings_capture(self): def test_warnings_capture(self):
output = StringIO()
self.logger.handlers[0].stream = output
warnings.warn('Foo Deprecated', DeprecationWarning) warnings.warn('Foo Deprecated', DeprecationWarning)
self.assertTrue('Foo Deprecated' in force_text(output.getvalue())) output = force_text(self.outputs[0].getvalue())
self.assertTrue('Foo Deprecated' in output)


def test_warnings_capture_debug_false(self): def test_warnings_capture_debug_false(self):
output = StringIO()
self.logger.handlers[0].stream = output
warnings.warn('Foo Deprecated', DeprecationWarning) warnings.warn('Foo Deprecated', DeprecationWarning)
self.assertFalse('Foo Deprecated' in force_text(output.getvalue())) output = force_text(self.outputs[0].getvalue())
self.assertFalse('Foo Deprecated' in output)




class CallbackFilterTest(TestCase): class CallbackFilterTest(TestCase):
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions tests/regressiontests/test_runner/deprecation_app/models.py
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
9 changes: 9 additions & 0 deletions tests/regressiontests/test_runner/deprecation_app/tests.py
@@ -0,0 +1,9 @@
import warnings

from django.test import TestCase

class DummyTest(TestCase):
def test_warn(self):
warnings.warn("warning from test", DeprecationWarning)


19 changes: 19 additions & 0 deletions tests/regressiontests/test_runner/tests.py
Expand Up @@ -279,6 +279,25 @@ def test_setup_databases(self):
db.connections = old_db_connections db.connections = old_db_connections




class DeprecationDisplayTest(AdminScriptTestCase):
# tests for 19546
def setUp(self):
settings = {'INSTALLED_APPS': '("regressiontests.test_runner.deprecation_app",)' }
self.write_settings('settings.py', sdict=settings)

def tearDown(self):
self.remove_settings('settings.py')

def test_runner_deprecation_verbosity_default(self):
args = ['test', '--settings=regressiontests.settings']
out, err = self.run_django_admin(args)
self.assertTrue("DeprecationWarning: warning from test" in err)

def test_runner_deprecation_verbosity_zero(self):
args = ['test', '--settings=regressiontests.settings', '--verbosity=0']
out, err = self.run_django_admin(args)
self.assertFalse("DeprecationWarning: warning from test" in err)

class AutoIncrementResetTest(TransactionTestCase): class AutoIncrementResetTest(TransactionTestCase):
""" """
Here we test creating the same model two times in different test methods, Here we test creating the same model two times in different test methods,
Expand Down

0 comments on commit cfa70d0

Please sign in to comment.