From cfa70d0c94a43d94e3ee48db87f2b88c29a862e1 Mon Sep 17 00:00:00 2001 From: Preston Holmes Date: Wed, 2 Jan 2013 16:00:39 -0800 Subject: [PATCH] Fixed #19546 - ensure that deprecation warnings are shown during tests refs #18985 --- django/test/simple.py | 13 +++++++++++ tests/regressiontests/logging_tests/tests.py | 23 ++++++++++++------- .../test_runner/deprecation_app/__init__.py | 0 .../test_runner/deprecation_app/models.py | 3 +++ .../test_runner/deprecation_app/tests.py | 9 ++++++++ tests/regressiontests/test_runner/tests.py | 19 +++++++++++++++ 6 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 tests/regressiontests/test_runner/deprecation_app/__init__.py create mode 100644 tests/regressiontests/test_runner/deprecation_app/models.py create mode 100644 tests/regressiontests/test_runner/deprecation_app/tests.py diff --git a/django/test/simple.py b/django/test/simple.py index bf0219d53f..8faf1e4f93 100644 --- a/django/test/simple.py +++ b/django/test/simple.py @@ -1,3 +1,4 @@ +import logging import unittest as real_unittest from django.conf import settings @@ -365,7 +366,19 @@ class DjangoTestSuiteRunner(object): self.setup_test_environment() suite = self.build_suite(test_labels, extra_tests) 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) + if self.verbosity > 0: + # remove the testing-specific handler + logger.removeHandler(handler) self.teardown_databases(old_config) self.teardown_test_environment() return self.suite_result(suite, result) diff --git a/tests/regressiontests/logging_tests/tests.py b/tests/regressiontests/logging_tests/tests.py index f9838949ab..b3d9f3b352 100644 --- a/tests/regressiontests/logging_tests/tests.py +++ b/tests/regressiontests/logging_tests/tests.py @@ -93,24 +93,31 @@ class WarningLoggerTests(TestCase): and captured to the logging system """ 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.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): - 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) def test_warnings_capture(self): - output = StringIO() - self.logger.handlers[0].stream = output 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): - output = StringIO() - self.logger.handlers[0].stream = output 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): diff --git a/tests/regressiontests/test_runner/deprecation_app/__init__.py b/tests/regressiontests/test_runner/deprecation_app/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/test_runner/deprecation_app/models.py b/tests/regressiontests/test_runner/deprecation_app/models.py new file mode 100644 index 0000000000..71a8362390 --- /dev/null +++ b/tests/regressiontests/test_runner/deprecation_app/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/tests/regressiontests/test_runner/deprecation_app/tests.py b/tests/regressiontests/test_runner/deprecation_app/tests.py new file mode 100644 index 0000000000..e676c3e3d5 --- /dev/null +++ b/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) + + diff --git a/tests/regressiontests/test_runner/tests.py b/tests/regressiontests/test_runner/tests.py index 5ef4d5537d..685e88d6ee 100644 --- a/tests/regressiontests/test_runner/tests.py +++ b/tests/regressiontests/test_runner/tests.py @@ -279,6 +279,25 @@ class DummyBackendTest(unittest.TestCase): 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): """ Here we test creating the same model two times in different test methods,