Django无法正确发现测试

时间:2014-11-01 11:17:26

标签: python django unit-testing

我正在尝试对我的项目实施一些测试,但我目前遇到了问题。

项目结构:

manage.py
-- src
---- apps
------- app1
------- app2
---- project
-------- settings.py
-------- urls.py

如果我将 init .py添加到apps文件夹

,我会得到
RuntimeError: Conflicting 'entry_tags' models in application 'entries': <class '
    entries.models.Entry_tags'> and <class 'src.apps.entries.models.Entry_tags'>.

如果我删除 init .py并将我的应用程序目录添加到sys.path,则不会发现我的测试。

PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))

sys.path.append(os.path.join(PROJECT_DIR, os.path.pardir, os.path.pardir, 'apps'))



(env) D:\projekty\ogloszenia>python manage.py test

Creating test database for alias 'default'...
.E
======================================================================
ERROR: src.apps.entries.tests (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: src.apps.entries.tests
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 "D:\projekty\ogloszenia\src\apps\entries\tests.py", line 10, in <module>
    from .models import Entry
  File "D:\projekty\ogloszenia\src\apps\entries\models.py", line 32, in <module>

    class Entry(EntryMetaDataMixin):
  File "D:\projekty\ogloszenia\env\lib\site-packages\django\db\models\base.py",
line 170, in __new__
    new_class.add_to_class(obj_name, obj)
  File "D:\projekty\ogloszenia\env\lib\site-packages\django\db\models\base.py",
line 299, in add_to_class
    value.contribute_to_class(cls, name)
  File "D:\projekty\ogloszenia\env\lib\site-packages\django\db\models\fields\rel
ated.py", line 2254, in contribute_to_class
    self.rel.through = create_many_to_many_intermediary_model(self, cls)
  File "D:\projekty\ogloszenia\env\lib\site-packages\django\db\models\fields\rel
ated.py", line 1860, in create_many_to_many_intermediary_model
    to: models.ForeignKey(to_model, related_name='%s+' % name, db_tablespace=fie
ld.db_tablespace, db_constraint=field.rel.db_constraint)
  File "D:\projekty\ogloszenia\env\lib\site-packages\django\db\models\base.py",
line 285, in __new__
    new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
  File "D:\projekty\ogloszenia\env\lib\site-packages\django\apps\registry.py", l
ine 213, in register_model
    (model_name, app_label, app_models[model_name], model))
RuntimeError: Conflicting 'entry_tags' models in application 'entries': <class '
entries.models.Entry_tags'> and <class 'src.apps.entries.models.Entry_tags'>.

sys.path中:

D:\projekty\ogloszenia
D:\projekty\ogloszenia\env\lib\site-packages\psycopg2-2.5.4-py2.7-win32.egg
D:\projekty\ogloszenia\env\lib\site-packages\test_pkg-0.0-py2.7.egg
C:\Program Files (x86)\Csound6\bin
C:\Windows\SYSTEM32\python27.zip
D:\projekty\ogloszenia\env\DLLs
D:\projekty\ogloszenia\env\lib
D:\projekty\ogloszenia\env\lib\plat-win
D:\projekty\ogloszenia\env\lib\lib-tk
D:\projekty\ogloszenia\env\Scripts
C:\Python27\Lib
C:\Python27\DLLs
C:\Python27\Lib\lib-tk
D:\projekty\ogloszenia\env
D:\projekty\ogloszenia\env\lib\site-packages
D:\projekty\ogloszenia\src\project\project_settings\..\..\apps

可能有什么问题:)?

1 个答案:

答案 0 :(得分:0)

它可能无法完全修复django'RuntimeError:Conflicting ...'但不是使用:

python manage.py test 

我安装了py.test并使用以下配置创建了pytest.ini:

[pytest]
DJANGO_SETTINGS_MODULE = project.settings
python_paths = apps apps/entries/
python_files=test*.py

我现在没有收到任何错误,我的测试就像我想要的那样被发现。