Ticket #18985: 18985-3.diff

File 18985-3.diff, 3.5 KB (added by claudep, 2 years ago)

Setting py.warnings handler as soon as possible for tests (patch for 1.5)

  • django/core/management/commands/test.py

    diff --git a/django/core/management/commands/test.py b/django/core/management/commands/test.py
    index 48b330a..2b8e801 100644
    a b  
     1import logging
    12import sys
    23import os
    34from optparse import make_option, OptionParser
    from django.conf import settings 
    67from django.core.management.base import BaseCommand
    78from django.test.utils import get_runner
    89
     10
    911class Command(BaseCommand):
    1012    option_list = BaseCommand.option_list + (
    1113        make_option('--noinput',
    class Command(BaseCommand): 
    5759                            version=self.get_version(),
    5860                            option_list=options)
    5961
     62    def execute(self, *args, **options):
     63        if int(options['verbosity']) > 0:
     64            # ensure that deprecation warnings are displayed during testing
     65            # the following state is assumed:
     66            # logging.capturewarnings is true
     67            # a "default" level warnings filter has been added for
     68            # DeprecationWarning. See django.conf.LazySettings._configure_logging
     69            logger = logging.getLogger('py.warnings')
     70            handler = logging.StreamHandler()
     71            logger.addHandler(handler)
     72        super(Command, self).execute(*args, **options)
     73        if int(options['verbosity']) > 0:
     74            # remove the testing-specific handler
     75            logger.removeHandler(handler)
     76
    6077    def handle(self, *test_labels, **options):
    6178        from django.conf import settings
    6279        from django.test.utils import get_runner
  • django/test/simple.py

    diff --git a/django/test/simple.py b/django/test/simple.py
    index 8faf1e4..bf0219d 100644
    a b  
    1 import logging
    21import unittest as real_unittest
    32
    43from django.conf import settings
    class DjangoTestSuiteRunner(object): 
    366365        self.setup_test_environment()
    367366        suite = self.build_suite(test_labels, extra_tests)
    368367        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)
    378368        result = self.run_suite(suite)
    379         if self.verbosity > 0:
    380             # remove the testing-specific handler
    381             logger.removeHandler(handler)
    382369        self.teardown_databases(old_config)
    383370        self.teardown_test_environment()
    384371        return self.suite_result(suite, result)
  • tests/runtests.py

    diff --git a/tests/runtests.py b/tests/runtests.py
    index 8c56e27..90dc0f5 100755
    a b  
    11#!/usr/bin/env python
     2import logging
    23import os
    34import shutil
    45import subprocess
    def setup(verbosity, test_labels): 
    106107    # in our tests.
    107108    settings.MANAGERS = ("admin@djangoproject.com",)
    108109
     110    if verbosity > 0:
     111        # Ensure py.warnings are piped through a verbose logging handler
     112        logger = logging.getLogger('py.warnings')
     113        handler = logging.StreamHandler()
     114        logger.addHandler(handler)
     115
    109116    # Load all the ALWAYS_INSTALLED_APPS.
    110117    # (This import statement is intentionally delayed until after we
    111118    # access settings because of the USE_I18N dependency.)
Back to Top