mercredi 28 octobre 2020

Django RuntimeError when using models for unittests

I'm using multiple models for running tests like this one on a django-2.2 project:

class Person(models.Model):
name = models.CharField(max_length=200)

Running a simple test:

class TestRegistration(SimpleTestCase):

def setUp(self):
    self.site = admin.AdminSite()

def test_bare_registration(self):    
    self.site.register(Person)
    self.assertIsInstance(self.site._registry[Person], admin.ModelAdmin)

But always getting the same kind of RuntimeError, with this one as with other models (the model raised changes but the RuntimeError persists):

    "INSTALLED_APPS." % (module, name)
    RuntimeError: Model class tests.models.Person doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

I've already tried:

Here is the complete message error:

Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/core/management/commands/test.py", line 23, in run_from_argv
super().run_from_argv(argv)
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/core/management/commands/test.py", line 53, in handle
failures = test_runner.run_tests(test_labels)
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/test/runner.py", line 627, in run_tests
suite = self.build_suite(test_labels, extra_tests)
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/test/runner.py", line 488, in build_suite
tests = self.test_loader.loadTestsFromName(label)
File "/usr/lib/python3.6/unittest/loader.py", line 153, in loadTestsFromName
module = __import__(module_name)
File "/home/codea/Workspace/ecoclimasol/climfarm/tests/__init__.py", line 1, in <module>
from tests.models import *
File "/home/codea/Workspace/ecoclimasol/climfarm/tests/models.py", line 15, in <module>
class Person(models.Model):
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/db/models/base.py", line 111, in __new__
"INSTALLED_APPS." % (module, name)
RuntimeError: Model class tests.models.Person doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

Aucun commentaire:

Enregistrer un commentaire