Django 1.9 までは、SimpleTestCase に urls 属性を書いていたようですが、コードを読むと
def _urlconf_setup(self):
if hasattr(self, 'urls'):
warnings.warn(
"SimpleTestCase.urls is deprecated and will be removed in "
"Django 1.10. Use @override_settings(ROOT_URLCONF=...) "
"in %s instead." % self.__class__.__name__,
RemovedInDjango110Warning, stacklevel=2)
set_urlconf(None)
self._old_root_urlconf = settings.ROOT_URLCONF
settings.ROOT_URLCONF = self.urls
clear_url_caches()
deprecated と書いてあったので、これにならって
@override_settings(ROOT_URLCONF=...)
で書いたらうまくいきました。
from django.conf.urls import url
from django.core.urlresolvers import reverse
from django.http import JsonResponse
from django.test import TestCase, override_settings
from lib.current_request import get_current_request
def current_request_view(request):
"""
current_request() 動作確認のためのビュー
"""
current_request = get_current_request()
return JsonResponse({
'current_request_path': current_request.path,
'request_path': request.path,
})
urlpatterns = [
url(r'^current-request/$', current_request_view,
name='current_request_view')
]
class CurrentRequestTest(TestCase):
@override_settings(ROOT_URLCONF="lib.tests.test_current_request")
def test_current_request(self):
response = self.client.get(reverse('current_request_view'))
data = response.json()
self.assertEqual(data['current_request_path'], '/current-request/')
self.assertEqual(data['request_path'], '/current-request/')
コメント