Open
Description
Some things I stumbled upon that others might find useful
Issue 1: how to make sure the right urlconf is used based on host when running tests
Some helpers to survive tests:
class DjangoHostsTestCase(TestCase):
"""Extends django.test.TestCase by setting a default SERVER_NAME to test client.
This is most likely necessary when testing code using django_hosts.
"""
@property
def client(self):
return self._client
@client.setter
def client(self, value):
value.defaults.setdefault('SERVER_NAME', 'the-new-hostname')
self._client = value
def reverse(*args, **kwargs):
"""Improved reverse that uses a specific django_hosts host."""
kwargs.setdefault('host', 'the-default-host')
return django_hosts.resolvers.reverse(*args, **kwargs)
NOTE: you could also just
response = self.client.get('/', SERVER_NAME='the-new-hostname')
Issue 2: django_hosts messes up with my existing tests that use reverse (namespace not registed)
Add following to TestCase:
from django.urls.base import set_urlconf
def tearDown(self):
# revert urlconf as django_hosts & self.client SERVER_NAME mess with it
set_urlconf(settings.ROOT_URLCONF)
Metadata
Metadata
Assignees
Labels
No labels