【发布时间】:2018-02-08 22:45:47
【问题描述】:
当我尝试运行pytest repo/tests/test_file.py 时出现以下错误:
$ pytest repo/tests/test_file.py
Traceback (most recent call last):
File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/_pytest/config.py", line 329, in _getconftestmodules
return self._path2confmods[path]
KeyError: local('/Users/marlo/repo/tests/test_file.py')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/_pytest/config.py", line 329, in _getconftestmodules
return self._path2confmods[path]
KeyError: local('/Users/marlo/repo/tests')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/_pytest/config.py", line 362, in _importconftest
return self._conftestpath2mod[conftestpath]
KeyError: local('/Users/marlo/repo/conftest.py')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/_pytest/config.py", line 368, in _importconftest
mod = conftestpath.pyimport()
File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/py/_path/local.py", line 686, in pyimport
raise self.ImportMismatchError(modname, modfile, self)
py._path.local.LocalPath.ImportMismatchError: ('conftest', '/home/venvuser/venv/conftest.py', local('/Users/marlo/repo/conftest.py'))
ERROR: could not load /Users/marlo/repo/conftest.py
我的回购结构是
lib/
-tests/
-test_file.py
app/
-test_settings.py
pytest.ini
conftest.py
...
其他人已经很好地运行了这段代码,并且根据this question(和this one),我的结构很好,我没有丢失任何文件。我只能得出结论,我的计算机或项目设置不正确。如果您有任何我可能遗漏的建议或见解,请发送给我!
-------------------更多详情-------------- ----------------
test_file.py:
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
pytest.ini:
[pytest]
DJANGO_SETTINGS_MODULE = app.test_settings
python_files = tests.py test_* *_tests.py *test.py
【问题讨论】:
-
1.假设包含
lib/、app/和conftest.py的根目录名为repo- 文件repo/tests/test_file.py是否存在? 2.运行cd repo; pytest --collect-only,所有的测试都找到了吗? 3、/home/venvuser/venv/conftest.py是什么文件,为什么要加载?它甚至属于另一个用户而不是你,这是故意的吗?检查您的sys.path是否包含一些不需要的条目。 -
@hoefling 感谢您的建议!事实证明,我有一个 docker 容器,它具有不同的文件路径,它干扰了 pytest。
-
我在
app/tests中添加了一个空的__init__.py,它起作用了。
标签: pytest