0

I have these urls under my urls.py. I didn't add a lot of URL patterns to the code as they're not relevant to the issue.

SWAGGER_URLS = [url(r'^swagger(?P<format>\.json|\.yaml)$',
                    schema_view.without_ui(cache_timeout=0), name='schema-json'),
                url(r'^swagger/$', schema_view.with_ui('swagger',
                                                       cache_timeout=0), name='swagger')]

urlpatterns = USER_URLS + USER_INTEREST_URLS + FOLLOW_USER_URLS + \
              GOAL_URLS + GOAL_CATEGORY_URLS + JOIN_GOAL_URLS + \
              POST_URLS + EXPLORE_URLS + HOME_FEED_URLS + SEARCH_URLS + FOLLOW_JOIN_GOAL_URLS

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += ADMIN_URLS
    urlpatterns += SWAGGER_URLS

then I have this test SwaggerTest.py

from django.test import TestCase
from django.urls import reverse
from rest_framework import status


class SwaggerTest(TestCase):
    @override_settings(DEBUG=True)
    def test_successful_swagger_pint(self):
        response = self.client.get(reverse('swagger'))
        self.assertEqual(response.status_code, status.HTTP_200_OK)

I'm getting the exception django.urls.exceptions.NoReverseMatch: Reverse for 'swagger' not found. 'swagger' is not a valid view function or pattern name. when I run this test. When I ping the url myself by going to localhost/swagger/ with my browser and it works fine. I'm using django-rest-swagger==2.2.0.

I run the test by running the commands python manage.py test <path to Swagger test>

  • I’m not seeing where you actually register the urls. Can you run `python manage.py show_urls`? –  Dec 02 '21 at 03:42
  • @KeoniGarner I'm getting `Unknown command: 'show_urls'` –  Dec 02 '21 at 04:33
  • Whoops that’s a django-extensions command –  Dec 02 '21 at 04:41
  • I see the edit now. If you move the SWAGGER_URLS outside of the settings.DEBUG conditional, do you get the error still? –  Dec 02 '21 at 04:44
  • https://stackoverflow.com/questions/7447134/how-do-you-set-debug-to-true-when-running-a-django-test –  Dec 02 '21 at 04:45
  • 1
    `DEBUG` is set to True. I updated the test and still the same issue. –  Dec 02 '21 at 04:52
  • I think it might be useful to show how you ping the url –  Dec 02 '21 at 04:59
  • I just run 'python manage.py test'. I have the swagger test up there and that gets run when I call the command. –  Dec 02 '21 at 06:13
  • 1
    I know that… I mean when you ping the url yourself and “it works” –  Dec 02 '21 at 13:21
  • @KeoniGarner o apologies I didn't understand the question. I just do 'localhost/swagger/' and it works fine. –  Dec 02 '21 at 22:30

0 Answers0