Pytest"错误:无法加载路径/到/ conftest.py"

Mar*_*rlo 15 pytest

我尝试运行时收到以下错误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
Run Code Online (Sandbox Code Playgroud)

我的回购结构是

lib/
    -tests/
        -test_file.py
app/
    -test_settings.py
pytest.ini
conftest.py
...
Run Code Online (Sandbox Code Playgroud)

其他人运行此代码很好,根据这个问题(和这个),我的结构很好,我不缺少任何文件.我只能得出结论,关于我的计算机或项目设置是不对的.如果您有任何我可能遗失的建议或见解,请将其发送给我们!

- - - - - - - - - - - - - - - -更多细节 - - - - - - - - - -------------

test_file.py:

def func(x):
    return x + 1

def test_answer():
    assert func(3) == 5
Run Code Online (Sandbox Code Playgroud)

pytest.ini:

[pytest]
DJANGO_SETTINGS_MODULE = app.test_settings
python_files = tests.py test_* *_tests.py *test.py
Run Code Online (Sandbox Code Playgroud)

Lis*_*saD 45

我也有docker以及在docker之外运行pytest,对我来说,一个影响小得多的修复程序,只要删除所有已编译的python文件

find . -name \*.pyc -delete

  • 我敢肯定,我也尝试过这样做,但这还不足以解决我的问题。但是,如果有更多人支持它,我将接受它作为答案:) (2认同)
  • 对于我的情况来说已经足够了(但我用 `git clean -dfqx` 做到了)。 (2认同)
  • @JulienPalard 您应该警告人们不要使用该命令。它删除了更多的 .pyc 文件 (2认同)