Code

Ticket #18985: 18985-3.diff

File 18985-3.diff, 3.5 KB (added by claudep, 13 months ago)

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

Line 
1diff --git a/django/core/management/commands/test.py b/django/core/management/commands/test.py
2index 48b330a..2b8e801 100644
3--- a/django/core/management/commands/test.py
4+++ b/django/core/management/commands/test.py
5@@ -1,3 +1,4 @@
6+import logging
7 import sys
8 import os
9 from optparse import make_option, OptionParser
10@@ -6,6 +7,7 @@ from django.conf import settings
11 from django.core.management.base import BaseCommand
12 from django.test.utils import get_runner
13 
14+
15 class Command(BaseCommand):
16     option_list = BaseCommand.option_list + (
17         make_option('--noinput',
18@@ -57,6 +59,21 @@ class Command(BaseCommand):
19                             version=self.get_version(),
20                             option_list=options)
21 
22+    def execute(self, *args, **options):
23+        if int(options['verbosity']) > 0:
24+            # ensure that deprecation warnings are displayed during testing
25+            # the following state is assumed:
26+            # logging.capturewarnings is true
27+            # a "default" level warnings filter has been added for
28+            # DeprecationWarning. See django.conf.LazySettings._configure_logging
29+            logger = logging.getLogger('py.warnings')
30+            handler = logging.StreamHandler()
31+            logger.addHandler(handler)
32+        super(Command, self).execute(*args, **options)
33+        if int(options['verbosity']) > 0:
34+            # remove the testing-specific handler
35+            logger.removeHandler(handler)
36+
37     def handle(self, *test_labels, **options):
38         from django.conf import settings
39         from django.test.utils import get_runner
40diff --git a/django/test/simple.py b/django/test/simple.py
41index 8faf1e4..bf0219d 100644
42--- a/django/test/simple.py
43+++ b/django/test/simple.py
44@@ -1,4 +1,3 @@
45-import logging
46 import unittest as real_unittest
47 
48 from django.conf import settings
49@@ -366,19 +365,7 @@ class DjangoTestSuiteRunner(object):
50         self.setup_test_environment()
51         suite = self.build_suite(test_labels, extra_tests)
52         old_config = self.setup_databases()
53-        if self.verbosity > 0:
54-            # ensure that deprecation warnings are displayed during testing
55-            # the following state is assumed:
56-            # logging.capturewarnings is true
57-            # a "default" level warnings filter has been added for
58-            # DeprecationWarning. See django.conf.LazySettings._configure_logging
59-            logger = logging.getLogger('py.warnings')
60-            handler = logging.StreamHandler()
61-            logger.addHandler(handler)
62         result = self.run_suite(suite)
63-        if self.verbosity > 0:
64-            # remove the testing-specific handler
65-            logger.removeHandler(handler)
66         self.teardown_databases(old_config)
67         self.teardown_test_environment()
68         return self.suite_result(suite, result)
69diff --git a/tests/runtests.py b/tests/runtests.py
70index 8c56e27..90dc0f5 100755
71--- a/tests/runtests.py
72+++ b/tests/runtests.py
73@@ -1,4 +1,5 @@
74 #!/usr/bin/env python
75+import logging
76 import os
77 import shutil
78 import subprocess
79@@ -106,6 +107,12 @@ def setup(verbosity, test_labels):
80     # in our tests.
81     settings.MANAGERS = ("admin@djangoproject.com",)
82 
83+    if verbosity > 0:
84+        # Ensure py.warnings are piped through a verbose logging handler
85+        logger = logging.getLogger('py.warnings')
86+        handler = logging.StreamHandler()
87+        logger.addHandler(handler)
88+
89     # Load all the ALWAYS_INSTALLED_APPS.
90     # (This import statement is intentionally delayed until after we
91     # access settings because of the USE_I18N dependency.)