Django/pytest: Requested setting REST_FRAMEWORK, but settings are not configured

201 views Asked by At

I'm working on a Django project and using pytest for testing. I have written a very simple test case -

class TestwordOfTheDay(TestCase):

    def setUp(self):
        print("Test passed")

    def test_json_response(self):
        json_response = {
            "id": 43,
            "wordOfTheDayinHindi": "अस्पष्ट",
            "wordOfTheDayinEnglish": "ambiguous",
            "wordOfTheDayinEnglish_Usage_Example": "Everyone listened to the weather report, but the forecast was ambiguous and no one knew if it was going to be rainy or sunny today.",
            "wordOfTheDayinHindi_Usage_Example": "हर किसी ने मौसम के समाचार को सुना था, लेकिन उनका पूर्वानुमान अस्पष्ट था और कोई भी यह नहीं जान पाया कि आज बरसात होगी या धूप खिलेगी।",
            "date": "2023-10-19"
        }
        response = JsonResponse(json_response)
        self.assertTrue(isinstance(json_response["id"], int))
        self.assertRegex(json_response["wordOfTheDayinHindi"], r"[ऀ-ॿ]+$")
        self.assertRegex(json_response["wordOfTheDayinEnglish"], r"^[a-zA-Z]+$")
        self.assertRegex(json_response["wordOfTheDayinEnglish_Usage_Example"], r"^.*\b" + re.escape(json_response["wordOfTheDayinEnglish"]) + r"\b.*[.?!]$")
        self.assertRegex(json_response["wordOfTheDayinHindi_Usage_Example"], r"^.*\b" + re.escape(json_response["wordOfTheDayinHindi"]) + r"\b.*[.?!]$")
        self.assertEqual(json_response["date"], str(date.today()))

However, when I run pytest, I encounter the following error:

django.core.exceptions.ImproperlyConfigured: Requested setting REST_FRAMEWORK, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

I'm not sure how to resolve this issue. Any help or guidance on how to fix this error would be greatly appreciated.

1

There are 1 answers

1
0urz4g On

The documentation may help you : Configuring Django settings

You can :

  • define an environment variable DJANGO_SETTINGS_MODULE
  • use the command line with the option --ds : pytest --ds=test.settings
  • create a file pytest.ini