lundi 4 janvier 2021

Django AppRegistryNotReady Exception after import from django.contrib.auth.models

Here is my folder structure:

.
├── app
├── project
├── scripts
└── tests
    ├── fuctional -> test_views.py
    └── unit

Here is my test_script.py

from django.test import TestCase
from django.urls import reverse
from django.contrib.auth.models import User
from django.contrib.auth.hashers import make_password

from rest_framework.test import APIClient

from mrx.views import LoginView, LogoutView

When I launch python -m unittest tests/fuctional/test_views.py I get this error:

Traceback (most recent call last):
  File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/unittest/__main__.py", line 18, in <module>
    main(module=None)
  File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/unittest/main.py", line 100, in __init__
    self.parseArgs(argv)
  File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/unittest/main.py", line 147, in parseArgs
    self.createTests()
  File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/unittest/main.py", line 158, in createTests
    self.test = self.testLoader.loadTestsFromNames(self.testNames,
  File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/unittest/loader.py", line 220, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/unittest/loader.py", line 220, in <listcomp>
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "/home/edx/PycharmProjects/mrx_3/tests/fuctional/test_views.py", line 3, in <module>
    from django.contrib.auth.models import User
  File "/home/edx/.pyenv/versions/MRX/lib/python3.8/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/home/edx/.pyenv/versions/MRX/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
    class AbstractBaseUser(models.Model):
  File "/home/edx/.pyenv/versions/MRX/lib/python3.8/site-packages/django/db/models/base.py", line 108, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/edx/.pyenv/versions/MRX/lib/python3.8/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
    self.check_apps_ready()
  File "/home/edx/.pyenv/versions/MRX/lib/python3.8/site-packages/django/apps/registry.py", line 136, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

I've tried with import Django and then django.setup() and it works but: why I can't import from django.contrib.auth.models import User ?

How do you test outside a Django app code inside a Django app?

Aucun commentaire:

Enregistrer un commentaire