Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Test unittest.loader.ModuleImportFailure

I can't import my models in my tests directory, this is my error :

======================================================================
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')...

I did some research and couldn't find an answer ...

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 : my directory

Thanks in advance for your help !

like image 228
William P. Avatar asked Jun 02 '16 12:06

William P.


1 Answers

You shouldn't normally need a models.py in your tests directory.

If you want to import your Entreprise model from the front app, then change your import from

from .models import Entreprise

to

from front.models import Entreprise
like image 105
Alasdair Avatar answered Sep 21 '22 15:09

Alasdair