1
0
mirror of https://github.com/django/django.git synced 2025-06-03 10:39:12 +00:00

Fixed #24965 -- Made LiveServerTestCase.live_server_url accessible from class

This commit is contained in:
Moritz Sichert 2015-06-10 22:57:51 +02:00 committed by Tim Graham
parent e93e0c03b2
commit 296919e7a5
4 changed files with 64 additions and 5 deletions

View File

@ -144,3 +144,15 @@ if ContextDecorator is None:
with self: with self:
return func(*args, **kwargs) return func(*args, **kwargs)
return inner return inner
class classproperty(object):
def __init__(self, method=None):
self.fget = method
def __get__(self, instance, owner):
return self.fget(owner)
def getter(self, method):
self.fget = method
return self

View File

@ -789,6 +789,12 @@ via the :djadminopt:`--liveserver` option, for example:
$ ./manage.py test --liveserver=localhost:8082 $ ./manage.py test --liveserver=localhost:8082
.. versionchanged:: 1.9
In older versions ``live_server_url`` could only be accessed from an
instance. It now is a class property and can be accessed from class methods
like ``setUpClass()``.
Another way of changing the default server address is by setting the Another way of changing the default server address is by setting the
`DJANGO_LIVE_TEST_SERVER_ADDRESS` environment variable somewhere in your `DJANGO_LIVE_TEST_SERVER_ADDRESS` environment variable somewhere in your
code (for example, in a :ref:`custom test runner<topics-testing-test_runner>`):: code (for example, in a :ref:`custom test runner<topics-testing-test_runner>`)::

View File

@ -11,6 +11,7 @@ from django.core.exceptions import ImproperlyConfigured
from django.test import LiveServerTestCase, override_settings from django.test import LiveServerTestCase, override_settings
from django.utils._os import upath from django.utils._os import upath
from django.utils.http import urlencode from django.utils.http import urlencode
from django.utils.six import text_type
from django.utils.six.moves.urllib.error import HTTPError from django.utils.six.moves.urllib.error import HTTPError
from django.utils.six.moves.urllib.request import urlopen from django.utils.six.moves.urllib.request import urlopen
@ -71,6 +72,9 @@ class LiveServerAddress(LiveServerBase):
else: else:
del os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] del os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS']
# put it in a list to prevent descriptor lookups in test
cls.live_server_url_test = [cls.live_server_url]
@classmethod @classmethod
def tearDownClass(cls): def tearDownClass(cls):
# skip it, as setUpClass doesn't call its parent either # skip it, as setUpClass doesn't call its parent either
@ -87,10 +91,9 @@ class LiveServerAddress(LiveServerBase):
finally: finally:
super(LiveServerAddress, cls).tearDownClass() super(LiveServerAddress, cls).tearDownClass()
def test_test_test(self): def test_live_server_url_is_class_property(self):
# Intentionally empty method so that the test is picked up by the self.assertIsInstance(self.live_server_url_test[0], text_type)
# test runner and the overridden setUpClass() method is executed. self.assertEqual(self.live_server_url_test[0], self.live_server_url)
pass
class LiveServerViews(LiveServerBase): class LiveServerViews(LiveServerBase):

View File

@ -2,7 +2,7 @@ from django.http import HttpResponse
from django.template import engines from django.template import engines
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.test import RequestFactory, SimpleTestCase from django.test import RequestFactory, SimpleTestCase
from django.utils.decorators import decorator_from_middleware from django.utils.decorators import classproperty, decorator_from_middleware
class ProcessViewMiddleware(object): class ProcessViewMiddleware(object):
@ -107,3 +107,41 @@ class DecoratorFromMiddlewareTests(SimpleTestCase):
self.assertTrue(getattr(request, 'process_response_reached', False)) self.assertTrue(getattr(request, 'process_response_reached', False))
# Check that process_response saw the rendered content # Check that process_response saw the rendered content
self.assertEqual(request.process_response_content, b"Hello world") self.assertEqual(request.process_response_content, b"Hello world")
class ClassPropertyTest(SimpleTestCase):
def test_getter(self):
class Foo(object):
foo_attr = 123
def __init__(self):
self.foo_attr = 456
@classproperty
def foo(cls):
return cls.foo_attr
class Bar(object):
bar = classproperty()
@bar.getter
def bar(cls):
return 123
self.assertEqual(Foo.foo, 123)
self.assertEqual(Foo().foo, 123)
self.assertEqual(Bar.bar, 123)
self.assertEqual(Bar().bar, 123)
def test_override_getter(self):
class Foo(object):
@classproperty
def foo(cls):
return 123
@foo.getter
def foo(cls):
return 456
self.assertEqual(Foo.foo, 456)
self.assertEqual(Foo().foo, 456)