0

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

1 Answers1

-1

Is it ok to change settings?

Short answer: No, unless you do it during the startup.

Long answer: You should not modify settings at runtime. This means, no settings modifications after the app has been started, like changing configuration in views.py, serializers.py, models.py or other modules you add during the development. But it is OK to modify settings if they depend on local variables if you do it at startup and you are fully aware of what happens.

But this is how I usually override settings in tests:

from django.conf import settings
from django.test import TestCase


class MyTestCase(TestCase):  # noqa

    @staticmethod
    def setUpClass():  # noqa
        settings.DEBUG = True
        super(ExampleTestCase, ExampleTestCase).setUpClass()

But maybe you should run your tests twice for that?

Mojtaba Arezoomand
  • 2,140
  • 8
  • 23
  • hmm this should work but for w.e reason it isn't. –  Dec 11 '21 at 07:46
  • I updated my post with what I have so far. –  Dec 11 '21 at 07:52
  • when I debug and get to the `reverse` function `settings.DEBUG` is `True`, but it's not working for w.e reason. –  Dec 11 '21 at 07:57
  • I'm not sure maybe Django find urls before your test runs. that means in your tests the debug is true but before it the debug was False so it couldn't find the url. have you tried printing the output of `settings.DEBUG` in the test case? – Mojtaba Arezoomand Dec 11 '21 at 08:29
  • Yes in the comment above was me referring to that. I think you're probably right that URL gets set prior to running all this. That would make sense since `settings.DEBUG` is outputting `True` is there a way to see the list of URLs from `TestCase`? –  Dec 11 '21 at 08:35
  • or even add a URL to that list that you can ping? –  Dec 11 '21 at 08:35
  • in this [link](https://stackoverflow.com/questions/1275486/django-how-can-i-see-a-list-of-urlpatterns) you can find the answer of how to see Django urls. But it doesn't make sense to do it manually in your test case because it always passes anyway. I think you can add two settings in your project. set the debug to false in one of them and in another one set it to true. then in your test cases you can write tests for both of them and separate the execution with `if DEBUG` then do smth `else` do something else. and you can run your tests with `python manage.py test --settings=site.settings` – Mojtaba Arezoomand Dec 11 '21 at 08:45
  • is there a way to run both with `python manage.py test`? As in run a tests without debug and then start a new test suite and run with debug true? Because it's only 1 test. –  Dec 11 '21 at 08:48
  • you can write a bash script for that exactly and run them with that with one command – Mojtaba Arezoomand Dec 11 '21 at 08:49
  • that's fair. Is it possible to change URLs mid test run btw? That's also a solution is just to add it in for that test if it's possible. –  Dec 11 '21 at 09:03
  • I have no idea but so far I haven't seen it in Django docs or somewhere else. – Mojtaba Arezoomand Dec 11 '21 at 09:11