Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #19546 - ensure that deprecation warnings are shown during tests

refs #18985
  • Loading branch information...
commit cfa70d0c94a43d94e3ee48db87f2b88c29a862e1 1 parent 1884868
Preston Holmes authored January 02, 2013
13  django/test/simple.py
... ...
@@ -1,3 +1,4 @@
  1
+import logging
1 2
 import unittest as real_unittest
2 3
 
3 4
 from django.conf import settings
@@ -365,7 +366,19 @@ def run_tests(self, test_labels, extra_tests=None, **kwargs):
365 366
         self.setup_test_environment()
366 367
         suite = self.build_suite(test_labels, extra_tests)
367 368
         old_config = self.setup_databases()
  369
+        if self.verbosity > 0:
  370
+            # ensure that deprecation warnings are displayed during testing
  371
+            # the following state is assumed:
  372
+            # logging.capturewarnings is true
  373
+            # a "default" level warnings filter has been added for
  374
+            # DeprecationWarning. See django.conf.LazySettings._configure_logging
  375
+            logger = logging.getLogger('py.warnings')
  376
+            handler = logging.StreamHandler()
  377
+            logger.addHandler(handler)
368 378
         result = self.run_suite(suite)
  379
+        if self.verbosity > 0:
  380
+            # remove the testing-specific handler
  381
+            logger.removeHandler(handler)
369 382
         self.teardown_databases(old_config)
370 383
         self.teardown_test_environment()
371 384
         return self.suite_result(suite, result)
23  tests/regressiontests/logging_tests/tests.py
@@ -93,24 +93,31 @@ class WarningLoggerTests(TestCase):
93 93
     and captured to the logging system
94 94
     """
95 95
     def setUp(self):
  96
+        # this convoluted setup is to avoid printing this deprecation to
  97
+        # stderr during test running - as the test runner forces deprecations
  98
+        # to be displayed at the global py.warnings level
96 99
         self.logger = logging.getLogger('py.warnings')
97  
-        self.old_stream = self.logger.handlers[0].stream
  100
+        self.outputs = []
  101
+        self.old_streams = []
  102
+        for handler in self.logger.handlers:
  103
+            self.old_streams.append(handler.stream)
  104
+            self.outputs.append(StringIO())
  105
+            handler.stream = self.outputs[-1]
98 106
 
99 107
     def tearDown(self):
100  
-        self.logger.handlers[0].stream = self.old_stream
  108
+        for i, handler in enumerate(self.logger.handlers):
  109
+            self.logger.handlers[i].stream = self.old_streams[i]
101 110
 
102 111
     @override_settings(DEBUG=True)
103 112
     def test_warnings_capture(self):
104  
-        output = StringIO()
105  
-        self.logger.handlers[0].stream = output
106 113
         warnings.warn('Foo Deprecated', DeprecationWarning)
107  
-        self.assertTrue('Foo Deprecated' in force_text(output.getvalue()))
  114
+        output = force_text(self.outputs[0].getvalue())
  115
+        self.assertTrue('Foo Deprecated' in output)
108 116
 
109 117
     def test_warnings_capture_debug_false(self):
110  
-        output = StringIO()
111  
-        self.logger.handlers[0].stream = output
112 118
         warnings.warn('Foo Deprecated', DeprecationWarning)
113  
-        self.assertFalse('Foo Deprecated' in force_text(output.getvalue()))
  119
+        output = force_text(self.outputs[0].getvalue())
  120
+        self.assertFalse('Foo Deprecated' in output)
114 121
 
115 122
 
116 123
 class CallbackFilterTest(TestCase):
0  tests/regressiontests/test_runner/deprecation_app/__init__.py
No changes.
3  tests/regressiontests/test_runner/deprecation_app/models.py
... ...
@@ -0,0 +1,3 @@
  1
+from django.db import models
  2
+
  3
+# Create your models here.
9  tests/regressiontests/test_runner/deprecation_app/tests.py
... ...
@@ -0,0 +1,9 @@
  1
+import warnings
  2
+
  3
+from django.test import TestCase
  4
+
  5
+class DummyTest(TestCase):
  6
+    def test_warn(self):
  7
+        warnings.warn("warning from test", DeprecationWarning)
  8
+
  9
+
19  tests/regressiontests/test_runner/tests.py
@@ -279,6 +279,25 @@ def test_setup_databases(self):
279 279
             db.connections = old_db_connections
280 280
 
281 281
 
  282
+class DeprecationDisplayTest(AdminScriptTestCase):
  283
+    # tests for 19546
  284
+    def setUp(self):
  285
+        settings = {'INSTALLED_APPS': '("regressiontests.test_runner.deprecation_app",)' }
  286
+        self.write_settings('settings.py', sdict=settings)
  287
+
  288
+    def tearDown(self):
  289
+        self.remove_settings('settings.py')
  290
+
  291
+    def test_runner_deprecation_verbosity_default(self):
  292
+        args = ['test', '--settings=regressiontests.settings']
  293
+        out, err = self.run_django_admin(args)
  294
+        self.assertTrue("DeprecationWarning: warning from test" in err)
  295
+
  296
+    def test_runner_deprecation_verbosity_zero(self):
  297
+        args = ['test', '--settings=regressiontests.settings', '--verbosity=0']
  298
+        out, err = self.run_django_admin(args)
  299
+        self.assertFalse("DeprecationWarning: warning from test" in err)
  300
+
282 301
 class AutoIncrementResetTest(TransactionTestCase):
283 302
     """
284 303
     Here we test creating the same model two times in different test methods,

0 notes on commit cfa70d0

Please sign in to comment.
Something went wrong with that request. Please try again.