【问题标题】:Unable to import test module in Django 3无法在 Django 3 中导入测试模块
【发布时间】:2020-07-08 11:24:41
【问题描述】:

我正在尝试按照 Django 文档中的说明为我的应用运行测试:https://docs.djangoproject.com/en/3.0/intro/tutorial05/

但是,对于我在下面显示的最小示例,我收到一个错误,抱怨:

RuntimeError: Model class myproject.myapp.models.MyModel doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

是否需要进行一些额外的配置才能运行测试,或者我可能已经犯了或忘记了一些其他错误?

完整的堆栈跟踪:

$ ./manage.py test myapp


System check identified no issues (0 silenced).
E
======================================================================
ERROR: myproject.myapp.tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: myproject.myapp.tests
Traceback (most recent call last):
File "/usr/lib64/python3.8/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
File "/usr/lib64/python3.8/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
File "/home/simernes/workspace/myproject/myapp/tests.py", line 2, in <module>
    from .models import MyModel
File "/home/simernes/workspace/myproject/myapp/models.py", line 5, in <module>
    class MyModel(models.Model):
File "/home/simernes/workspace/myproject/env/lib/python3.8/site-packages/django/db/models/base.py", line 112, in __new__
    raise RuntimeError(
RuntimeError: Model class myproject.myapp.models.MyModel doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

settings.py:

INSTALLED_APPS = [
    'myapp.apps.MyAppConfig',
    ...
]

apps.py:

from django.apps import AppConfig


class MyAppConfig(AppConfig):
    name = 'myapp'

tests.py:

from django.test import TestCase
from .models import MyModel


# Create your tests here.
class MyModelTests(TestCase):

    def model_names_are_unique(self):
        """
        The database crashes if the same name is registered twice
        :return:
        """
        unique_name = "Unique Name"

        model_first = MyModel(name=unique_identifier)
        model_first.save()

        model_second = MyModel(name=unique_identifier)
        model_second.save()
        self.assertTrue(True)  # todo: assert that an error is raised

models.py:

from django.db import models


class MyModel(models.Model):
    name = models.CharField(max_length=255, unique=True)

    def __str__(self):
        return self.name

【问题讨论】:

    标签: python django django-testing django-tests


    【解决方案1】:

    这个问题是由于 __init__.py 被放置在 django 项目的根文件夹中造成的。删除这个文件就解决了。

    【讨论】:

      【解决方案2】:

      在您的“settings.py”文件中尝试此编辑:

      INSTALLED_APPS = [
          ...
          'myapp',
      ]
      

      【讨论】:

      • 谢谢,我试过了,但没用。我尝试逐步重新创建我的应用程序,并且使用新的应用程序我能够运行测试,但似乎以某种方式在没有应用程序配置的情况下加载了测试
      猜你喜欢
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多