Ticket #19546: 19546-2.diff

File 19546-2.diff, 5.9 KB (added by Preston Holmes, 12 years ago)
  • django/test/simple.py

    diff --git a/django/test/simple.py b/django/test/simple.py
    index bf0219d..3bcfec1 100644
    a b  
     1import logging
    12import unittest as real_unittest
     3import warnings
    24
    35from django.conf import settings
    46from django.core.exceptions import ImproperlyConfigured
    class DjangoTestSuiteRunner(object):  
    365367        self.setup_test_environment()
    366368        suite = self.build_suite(test_labels, extra_tests)
    367369        old_config = self.setup_databases()
     370        if self.verbosity > 0:
     371            # duplicate some of the steps taken in logging configuration
     372            # to insure that DeprecationWarnings are displayed on sys.stderr
     373            # during testing regardless of the Python default in >2.6
     374            if logging._warnings_showwarning is None:
     375                old_captureWarnings = False
     376                logging.captureWarnings(True)
     377            else:
     378                old_captureWarnings = True
     379            warnings.simplefilter("default", DeprecationWarning)
     380            new_filter = warnings.filters[0]
     381            logger = logging.getLogger('py.warnings')
     382            handler = logging.StreamHandler()
     383            logger.addHandler(handler)
    368384        result = self.run_suite(suite)
     385        if self.verbosity > 0:
     386            # make every effort to restore global logging and warning state
     387            if warnings.filters[0] == new_filter:
     388                warnings.filters.pop(0)
     389            logger.removeHandler(handler)
     390            if not old_captureWarnings:
     391                logging.captureWarnings(False)
    369392        self.teardown_databases(old_config)
    370393        self.teardown_test_environment()
    371394        return self.suite_result(suite, result)
  • tests/regressiontests/logging_tests/tests.py

    diff --git a/tests/regressiontests/logging_tests/tests.py b/tests/regressiontests/logging_tests/tests.py
    index f983894..b3d9f3b 100644
    a b class WarningLoggerTests(TestCase):  
    9393    and captured to the logging system
    9494    """
    9595    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
    9699        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]
    98106
    99107    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]
    101110
    102111    @override_settings(DEBUG=True)
    103112    def test_warnings_capture(self):
    104         output = StringIO()
    105         self.logger.handlers[0].stream = output
    106113        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)
    108116
    109117    def test_warnings_capture_debug_false(self):
    110         output = StringIO()
    111         self.logger.handlers[0].stream = output
    112118        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)
    114121
    115122
    116123class CallbackFilterTest(TestCase):
  • new file tests/regressiontests/test_runner/deprecation_app/models.py

    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 0000000..e69de29
    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 0000000..71a8362
    - +  
     1from django.db import models
     2
     3# Create your models here.
  • new file tests/regressiontests/test_runner/deprecation_app/tests.py

    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 0000000..e676c3e
    - +  
     1import warnings
     2
     3from django.test import TestCase
     4
     5class DummyTest(TestCase):
     6    def test_warn(self):
     7        warnings.warn("warning from test", DeprecationWarning)
     8
     9
  • tests/regressiontests/test_runner/tests.py

    diff --git a/tests/regressiontests/test_runner/tests.py b/tests/regressiontests/test_runner/tests.py
    index c723f16..5e58388 100644
    a b class Sqlite3InMemoryTestDbs(unittest.TestCase):  
    261261                db.connections = old_db_connections
    262262
    263263
     264class DeprecationDisplayTest(AdminScriptTestCase):
     265    # tests for 19546
     266    def setUp(self):
     267        settings = {'INSTALLED_APPS': '("regressiontests.test_runner.deprecation_app",)' }
     268        self.write_settings('settings.py', sdict=settings)
     269
     270    def tearDown(self):
     271        self.remove_settings('settings.py')
     272
     273    def test_runner_deprecation_verbosity_default(self):
     274        args = ['test', '--settings=regressiontests.settings']
     275        out, err = self.run_django_admin(args)
     276        self.assertTrue("DeprecationWarning: warning from test" in err)
     277
     278    def test_runner_deprecation_verbosity_zero(self):
     279        args = ['test', '--settings=regressiontests.settings', '--verbosity=0']
     280        out, err = self.run_django_admin(args)
     281        self.assertFalse("DeprecationWarning: warning from test" in err)
     282
    264283class AutoIncrementResetTest(TransactionTestCase):
    265284    """
    266285    Here we test creating the same model two times in different test methods,
Back to Top