Django找到测试但无法导入它们

Eld*_*mir 15 python testing django

我得到了奇怪的错误,在那里打电话./manage.py test会发现我的测试,但抱怨他们无法导入.

版本

Python 3.4

Django 1.7b4

我的文件结构

看起来像这样(只是相关的位):

inkasso
??? db.sqlite3
??? functional_tests
?   ??? base.py
?   ??? base.pyc
?   ??? __init__.py
?   ??? __init__.pyc
?   ??? __pycache__
?   ??? test_login.py
?   ??? test_login.pyc
??? __init__.py
??? inkasso
?   ??? __init__.py
?   ??? __init__.pyc
?   ??? migrations
?   ??? models.py
?   ??? settings.py
?   ??? settings.pyc
?   ??? urls.py
?   ??? wsgi.py
??? manage.py
??? static
?   ??? ...
??? templates
?   ??? ...
??? web
    ??? admins.py
    ??? tests
    ?   ??? __init__.py
    ?   ??? test_forms.py
    ?   ??? test_models.py
    ?   ??? test_views.py
    ??? urls.py
    ??? views.py
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪

因此,当我运行时,./manage.py test我得到以下stak-trace:

 $ ./manage.py test
Creating test database for alias 'default'...
EEEE
======================================================================
ERROR: inkasso.functional_tests.test_login (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.4/unittest/case.py", line 57, in testPartExecutor
    yield
  File "/usr/lib/python3.4/unittest/case.py", line 574, in run
    testMethod()
  File "/usr/lib/python3.4/unittest/loader.py", line 32, in testFailure
    raise exception
ImportError: Failed to import test module: inkasso.functional_tests.test_login
Traceback (most recent call last):
  File "/usr/lib/python3.4/unittest/loader.py", line 312, in _find_tests
    module = self._get_module_from_name(name)
  File "/usr/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
    __import__(name)
ImportError: No module named 'inkasso.functional_tests'


======================================================================
ERROR: inkasso.web.tests.test_forms (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.4/unittest/case.py", line 57, in testPartExecutor
    yield
  File "/usr/lib/python3.4/unittest/case.py", line 574, in run
    testMethod()
  File "/usr/lib/python3.4/unittest/loader.py", line 32, in testFailure
    raise exception
ImportError: Failed to import test module: inkasso.web.tests.test_forms
Traceback (most recent call last):
  File "/usr/lib/python3.4/unittest/loader.py", line 312, in _find_tests
    module = self._get_module_from_name(name)
  File "/usr/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
    __import__(name)
ImportError: No module named 'inkasso.web'


======================================================================
ERROR: inkasso.web.tests.test_models (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.4/unittest/case.py", line 57, in testPartExecutor
    yield
  File "/usr/lib/python3.4/unittest/case.py", line 574, in run
    testMethod()
  File "/usr/lib/python3.4/unittest/loader.py", line 32, in testFailure
    raise exception
ImportError: Failed to import test module: inkasso.web.tests.test_models
Traceback (most recent call last):
  File "/usr/lib/python3.4/unittest/loader.py", line 312, in _find_tests
    module = self._get_module_from_name(name)
  File "/usr/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
    __import__(name)
ImportError: No module named 'inkasso.web'


======================================================================
ERROR: inkasso.web.tests.test_views (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.4/unittest/case.py", line 57, in testPartExecutor
    yield
  File "/usr/lib/python3.4/unittest/case.py", line 574, in run
    testMethod()
  File "/usr/lib/python3.4/unittest/loader.py", line 32, in testFailure
    raise exception
ImportError: Failed to import test module: inkasso.web.tests.test_views
Traceback (most recent call last):
  File "/usr/lib/python3.4/unittest/loader.py", line 312, in _find_tests
    module = self._get_module_from_name(name)
  File "/usr/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
    __import__(name)
ImportError: No module named 'inkasso.web'


----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED (errors=4)
Destroying test database for alias 'default'...
Run Code Online (Sandbox Code Playgroud)

所以测试运行器找到我的测试,但由于某种原因,他们没有导入.我不知道它到底发生了什么.堆栈跟踪对我没有多大帮助:(

由于调用了根文件夹inkasso并且它有一个同名的模块,我尝试将print(os.getcwd)print(sys.path)放入manage.py,并且显示的是CWD和路径都设置为指向根文件夹,所以它应该都是好的,不是吗?应用程序本身按预期运行.只有测试不起作用.

对于咯咯笑,我尝试在inkasso.inkasso中创建一个空模块"web",结果是它不会抱怨inkasso.web不存在,它现在抱怨inkasso.web.tests不存在.所以这表明它不是查看根'inkasso'文件夹,而是查看'inkasso.inkasso'.这就是问题所在.我该如何解决?

vla*_*ean 23

是的...运行时出现问题./manage.py,因为它将当前目录添加到PYTHONPATH.

放入__init__.py根文件夹时会发生此问题.

在这种情况下,一种解决方案是永远不会使用manage.py,但只是django-admin.py <commands> --settings=inkasso.inkasso.settings- 当然,这假设在运行此命令时,您只需一级,在根文件夹之外inkasso,或者您已安装主程序包site-packages.

例如,如果settings.py文件的完整路径是/home/user/projects/inkasso/inkasso/settings.py,则需要在/home/user/projects运行此命令时进入.

但是,如果您的网站包中已安装了您的软件包,则上述限制会发生变化:您可以在除/home/user/projects/inkasso其子文件夹或其任何子文件夹之外的任何位置进行更改.

另一种解决方案是编辑您的manage.py文件以添加此行:

if __name__ =='__main__': #line already present
    #this will make the python interpreter see your packages as inkasso.inkasso.whatever
    os.chdir('..')  # <<<---This is what you want to add

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mvod.dev_settings")
    ....
Run Code Online (Sandbox Code Playgroud)

  • 我删除了./__init__.py,现在单元测试运行得很糟糕.功能测试仍然失败,但这似乎是由于selenium在python3中的语法无效.谢谢你的助攻:) (3认同)