在测试中间件期间未设置设置变量 (p
Setting variable is not being set up during testing middleware (p
settings.py
MY_VAR = os.get("MY_VAR", False)
custom_middleware.py
from my_proj.settings import MY_VAR
from django.core.exceptions import MiddlewareNotUsed
class CustomMiddleware:
def _init_(self, get_response):
if MY_VAR == 'False':
raise MiddlewareNotUsed
self.get_response = get_response
def __call__(self, request):
if MY_VAR == 'True':
#My custom logic
return
response = self.get_response(request)
return response
test_custom_middleware.py
import os from unittest.mock import Mock
from api.middlewares.custom_middleware import CustomMiddleware
class TestLCustomMiddleware:
def test(self, settings):
request = Mock()
settings.MY_VAR = 'True'
assert settings.MY_VAR
with patch.dict('os.environ', {'MY_VAR': 'True'}):
assert 'MY_VAR' in os.environ
middleware = CustomMiddleware(get_response='response')
middleware(request)
在 CustomMiddleware
中,我总是在 MY_VAR
设置变量中得到 "False"
。我该如何设置?
已更新。
感谢 Philippe 和 Dharman。
效果很好
custom_middleware.py
from django.conf import settings
from django.core.exceptions import MiddlewareNotUsed
class CustomMiddleware:
def _init_(self, get_response):
if settings.MY_VAR == 'False':
raise MiddlewareNotUsed
self.get_response = get_response
def __call__(self, request):
if settings.MY_VAR == 'True':
#My custom logic
return
response = self.get_response(request)
return response
test_custom_middleware.py
from api.middlewares.custom_middleware import CustomMiddleware
from django.test import override_settings
class TestLCustomMiddleware:
@override_settings(MY_VAR="True")
def test(self):
request = Mock()
middleware = CustomMiddleware(get_response='response')
middleware(request)
如 Django documentation 中所述,您应该使用以下方式导入您的设置:
from django.conf import settings
if settings.MY_VAR:
# Do something
"django.conf.settings" 不是一个模块,它是一个对象。因此无法导入个人设置。下面的示例无法正常工作:
from django.conf.settings import MY_VAR
此外,不建议change setting during Runtime :
from django.conf import settings
settings.DEBUG = True # Don't do this!
您应该尝试使用 override_settings() or modify_settings()。
例如:
from django.test import TestCase, override_settings
class TestLCustomMiddleware(TestCase):
@override_settings(MY_VAR=True) # without quote
def test(self):
...
settings.py
MY_VAR = os.get("MY_VAR", False)
custom_middleware.py
from my_proj.settings import MY_VAR
from django.core.exceptions import MiddlewareNotUsed
class CustomMiddleware:
def _init_(self, get_response):
if MY_VAR == 'False':
raise MiddlewareNotUsed
self.get_response = get_response
def __call__(self, request):
if MY_VAR == 'True':
#My custom logic
return
response = self.get_response(request)
return response
test_custom_middleware.py
import os from unittest.mock import Mock
from api.middlewares.custom_middleware import CustomMiddleware
class TestLCustomMiddleware:
def test(self, settings):
request = Mock()
settings.MY_VAR = 'True'
assert settings.MY_VAR
with patch.dict('os.environ', {'MY_VAR': 'True'}):
assert 'MY_VAR' in os.environ
middleware = CustomMiddleware(get_response='response')
middleware(request)
在 CustomMiddleware
中,我总是在 MY_VAR
设置变量中得到 "False"
。我该如何设置?
已更新。
感谢 Philippe 和 Dharman。 效果很好
custom_middleware.py
from django.conf import settings
from django.core.exceptions import MiddlewareNotUsed
class CustomMiddleware:
def _init_(self, get_response):
if settings.MY_VAR == 'False':
raise MiddlewareNotUsed
self.get_response = get_response
def __call__(self, request):
if settings.MY_VAR == 'True':
#My custom logic
return
response = self.get_response(request)
return response
test_custom_middleware.py
from api.middlewares.custom_middleware import CustomMiddleware
from django.test import override_settings
class TestLCustomMiddleware:
@override_settings(MY_VAR="True")
def test(self):
request = Mock()
middleware = CustomMiddleware(get_response='response')
middleware(request)
如 Django documentation 中所述,您应该使用以下方式导入您的设置:
from django.conf import settings
if settings.MY_VAR:
# Do something
"django.conf.settings" 不是一个模块,它是一个对象。因此无法导入个人设置。下面的示例无法正常工作:
from django.conf.settings import MY_VAR
此外,不建议change setting during Runtime :
from django.conf import settings
settings.DEBUG = True # Don't do this!
您应该尝试使用 override_settings() or modify_settings()。 例如:
from django.test import TestCase, override_settings
class TestLCustomMiddleware(TestCase):
@override_settings(MY_VAR=True) # without quote
def test(self):
...