【问题标题】:Django finds tests but fail to import themDjango 找到测试但无法导入它们
【发布时间】:2014-05-25 11:12:20
【问题描述】:

我遇到了奇怪的错误,调用 ./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

堆栈跟踪

所以当我运行 ./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'...

所以测试运行器找到了我的测试,但由于某种原因,它们没有被导入。我不知道这是怎么回事。堆栈跟踪对我不是很有帮助:(

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

为了傻笑,我尝试在 inkasso.inkasso 中创建一个空模块“web”,结果不是抱怨 inkasso.web 不存在,而是现在抱怨 inkasso.web.tests 不存在。所以这表明它不是在根“inkasso”文件夹中查找,而是在“inkasso.inkasso”中查找。所以这就是问题所在。我该如何解决?

【问题讨论】:

    标签: python django testing


    【解决方案1】:

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

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

    在这种情况下,一个解决方案是永远不要使用manage.py,而只使用django-admin.py <commands> --settings=inkasso.inkasso.settings - 当然,这假设在运行此命令时,您在根文件夹@987654325 之外处于上一级@,或者你的主包安装在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")
        ....
    

    【讨论】:

    • 我删除了./__init__.py,现在单元测试运行得很好。功能测试仍然失败,但这似乎是由于 selenium 的语法在 python3 中无效。感谢您的帮助:)
    • 谢谢,对这个问题真的很恼火。
    • 老兄,你是绝对的救星。我在某个时候添加了一个 root dunder-init,它打破了 100 多个测试,我花了一个月的时间才找到这个线程。给我来杯电子啤酒!
    • 更改所有命令的目录似乎会使“runserver”命令停止工作。令人沮丧的是,即使您遵循所有指南,推荐的命令也不起作用。
    • @TonySuffolk66 django 是为开发人员准备的。我想很多时候人们只是想出开箱即用的东西。如果他们不这样做,那就是互联网 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 2021-03-30
    相关资源
    最近更新 更多