I have an endpoint /docs
in django that I only want to be visible when DEBUG = True
in settings - otherwise, it should throw a 404. My setup looks like this
urls.py
urlpatterns = ...
if settings.DEBUG:
urlpatterns += [
url(r'^docs/$', SwaggerSchemaView.as_view(), name='api_docs'),
]
When doing testing, though, django doesn't automatically reload urls.py
, which means simply overriding DEBUG
to True
or False
doesn't work.
My tests look something like this
@override_settings(DEBUG=True)
@override_settings(ROOT_URLCONF='config.urls')
class APIDocsTestWithDebug(APITestCase):
# check for 200s
...
@override_settings(DEBUG=False)
@override_settings(ROOT_URLCONF='config.urls')
class APIDocsTestWithoutDebug(APITestCase):
# check for 404s
...
Now here's the weird part: When I run the tests individually using pytest path/to/test.py::APIDocsTestWithDebug
and pytest path/to/test.py::APIDocsTestWithoutDebug
, both tests pass. However, if I run the test file as a whole (pytest path/to/test.py
), APIDocsTestWithDebug
always fails. The fact that they work individually but not together tells me that the url override is working, but when the tests are in tandem, there is some bug that messes things up. I was wondering if anybody had come across a similar issue and either has an entirely different solution or can give me some tips as to what I'm doing wrong.
DEBUG
to be overridden, my urls are defined based on whether or notDEBUG
is True. I need a find a way to re-generate the URLs, or something similar – Iamburls.py
? – Mcallisterfrom django.conf import settings
– Iambinclude
– Mcallister