Django找到测试,但未能导入它们

2024-04-27 14:53:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到了一些奇怪的错误,调用./manage.py test会发现我的测试,但会抱怨它们无法导入。

版本

Python3.4

Django 17亿B4

我的文件结构

看起来是这样(只是相关的部分):

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跟踪:

 $ ./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和path都设置为指向根文件夹,所以应该一切正常,不是吗?应用程序本身按预期运行。只是测试不起作用。

为了搞笑,我尝试在inkasso.inkasso中创建一个空模块“web”,结果不是抱怨inkasso.web不存在,而是抱怨inkasso.web.tests不存在。因此,这表明它不是在根“inkasso”文件夹中查找,而是在“inkasso.inkasso”中查找。所以这就是问题所在。我怎样才能修好它?


Tags: nameinpytestweblibusrline