I have this test for checking if I can ping the swagger endpoint
from django.test import SimpleTestCase
from django.test.utils import override_settings
from django.urls import reverse
from rest_framework import status
class SwaggerTest(SimpleTestCase):
@override_settings(DEBUG=True)
def test_successful_swagger_ping(self):
"""
Test to ensure that Swagger can be reached successfully. If this test
throws 5XX that means there's an error in swagger annotation on some view function.
"""
response = self.client.get(reverse('swagger'))
self.assertEqual(response.status_code, status.HTTP_200_OK)
So this test fails, because I only have swagger added to url when settings.DEBUG=True
. I thought @override_settings(DEBUG=TRUE)
would fix that, but it doesn't. My test passes when I manually set settings.DEBUG=True
under my settings.py
file, otherwise it throws an error because it can't find the endpoint. I've tried decorating both the class and the function with @override_settings
and in both cases it threw the error because it couldn't find the endpoint. I'm not particularly attached to this way of testing to see if Swagger is working. This test is just to check if Swagger isn't returning an error regarding annotation. If anyone knows a better way to test this I'm open to that.
I've also tried
from django.conf import settings
from django.test import TestCase
from django.urls import reverse
from rest_framework import status
class SwaggerTest(TestCase):
@staticmethod
def setUpClass() -> None:
settings.DEBUG = True
super(SwaggerTest, SwaggerTest).setUpClass()
def test_successful_swagger_ping(self):
"""
Test to ensure that Swagger can be reached successfully. If this test
throws 5XX that means there's an error in swagger annotation on some view function.
"""
response = self.client.get(reverse('swagger'))
self.assertEqual(response.status_code, status.HTTP_200_OK)
but that also gives django.urls.exceptions.NoReverseMatch: Reverse for 'swagger' not found. 'swagger' is not a valid view function or pattern name.
it only works when I set
test/__init__.py
settings.DEBUG = True