Pytest"错误:无法加载path / to / conftest.py"

时间:2018-02-08 22:45:47

标签: pytest

尝试运行$ 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
...

我的回购结构是

def func(x):
    return x + 1

def test_answer():
    assert func(3) == 5

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

-------------------------------更多详情-------------- ----------------

test_file.py

[pytest]
DJANGO_SETTINGS_MODULE = app.test_settings
python_files = tests.py test_* *_tests.py *test.py

pytest.ini

{{1}}

2 个答案:

答案 0 :(得分:37)

我也有docker以及在docker之外运行pytest,对我来说,只要删除所有已编译的python文件就会产生影响较小的修复程序

find . -name \*.pyc -delete

答案 1 :(得分:1)

我想出来了,如果其他人有同样的问题,我会回答:

我甚至没有考虑到我在repo目录中有一个docker容器(同一个应用程序),虽然我没有运行docker容器,但它以某种方式影响了文件路径。

解决此问题:

  1. 我将来自远程源的repo重新克隆到一个新文件夹中,这样旧回购中的任何内容都不会“污染”它。
  2. 使用clean repo的.yml规范更新了我的虚拟环境
  3. 
        $ conda env update --name project --file project.yml
    
    
    1. 我的项目使用postgres数据库,因此我删除了它并创建了一个新的
    2. 
          $ dropdb projectdb
          $ createdb projectdb
      
      
      1. 由于我的项目使用mongo,我也删除了该数据库
      2. 
            $ mongo projectdb --eval "db.dropDatabase()"
        
        
        1. 安装了干净的pytest
        2. 
              $ pip uninstall pytest
              $ pip install pytest
          
          

          ......瞧!我可以运行pytest。

          非常感谢@hoefling和其他帮助我调试的人。