python – Django测试unittest.loader.ModuleImportFailure

我无法在我的测试目录中导入我的模型,这是我的错误:

======================================================================
ERROR: tests.test_views (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests.test_views
Traceback (most recent call last):
  File "C:\Python27\lib\unittest\loader.py", line 254, in _find_tests
    module = self._get_module_from_name(name)
  File "C:\Python27\lib\unittest\loader.py", line 232, in _get_module_from_name
    __import__(name)
  File "c:\wamp\www\km0\tests\test_views.py", line 3, in <module>
    from .models import Entreprise
  File "c:\wamp\www\km0\tests\models.py", line 6, in <module>
    class Entreprise(models.Model):
  File "C:\Python27\lib\site-packages\django-1.9.5-py2.7.egg\django\db\models\ba
se.py", line 102, in __new__
    "INSTALLED_APPS." % (module, name)
RuntimeError: Model class tests.models.Entreprise doesn't declare an explicit ap
p_label and isn't in an application in INSTALLED_APPS.


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)
Preserving test database for alias 'default' ('test_km0')...

我做了一些研究,找不到答案……

test_views.py:

from django.test import TestCase
import unittest
from .models import Entreprise

class Km0ViewsTestCase(TestCase):
    def test_cart(self):
        resp = self.client.get('/fr/cart/')
        self.assertEqual(resp.status_code, 200)
        resp = self.client.get('/en/cart/')
        self.assertEqual(resp.status_code, 200)
        resp = self.client.get('/de/cart/')
        self.assertEqual(resp.status_code, 200)

我的目录:
my directory

在此先感谢您的帮助 !

最佳答案 您通常不需要在tests目录中使用models.py.

如果要从前端应用程序导入Entreprise模型,请从中更改导入

from .models import Entreprise

from front.models import Entreprise
点赞