1

Whenever I'm running my Django-Tests via the unittests.py file in the main folder the SECRET KEY in .env is not found. What is the correct way to set up the regular Unittests-File for a series of Django-Tests? I wrote my settings.py in a way it's able to write a new key if no other key is found:

---settings.py---

try:
    print("Current key: " + os.environ['SECRET_KEY'])
    SECRET_KEY = os.environ['SECRET_KEY']
except KeyError as kerr:
    logging.error("Can't find Secret Key in settings.py")
    path_env = os.path.join(BASE_DIR, '.env')
    utils.generate_secret_key(path_env)
    dotenv.read_dotenv(path_env)
    SECRET_KEY = os.environ['SECRET_KEY'] 

This has worked, but as on every run of the unittests another new key is created, which is not intended. So far I'm running the unittests with a default script which works fine, except for the issue with the key:

---unittests.py---

import DjangoProject.tests_client
suite = unittest.TestSuite()
loader = unittest.TestLoader()
suite.addTest(loader.loadTestsFromModule(DjangoProject.tests_client))

The implemented Test is not a too huge surprise:

from django.test import TestCase
import django
django.setup()

class TestBase(TestCase):

    def setUp(self):
        #Stuff

    def tearDown(self):
        self.removeData()

    def test_Clienttest(self):
        #Stuff

The Unittests-Classes inherit as required from from django.test.TestCase and do what they are supposed to:

Running the Unittest-Script with these settings will result in an error as it can't find the SECRET KEY (I did some debugging and indeed, the key is not loaded in the environment). For some testing I disabled the lines in the settings.py which create a new key and immediately get the error:

$ python3.7 unittests.py
ERROR:root:'SECRET_KEY'
Traceback (most recent call last):
  File "unittests.py", line 25, in <module>
    import DjangoProject.tests_client
  File "/home/djangohome/DjangoProject/tests_client.py", line 23, in <module>
    django.setup()
  File "/usr/local/lib/python3.7/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 79, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 66, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 176, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

Running the test via manage.py works perfectly fine too:

$ python3.7 manage.py test DjangoProject.tests_client
Current key: o5at!z(_()s2vbp=n4exot@gjo1==!w39uby*eju)ej#1o^lwn
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
... And everything as supposed to ...

But how would I have to implement the run via manage.py in the regular Unittests so the SECRET KEY is loaded correctly?

Qohelet
  • 1,459
  • 4
  • 24
  • 41

0 Answers0