当我在本地运行测试时,它工作正常,但在创建 docker 并在容器内运行后,我遇到了以下错误。
/usr/local/lib/python3.5/site-packages/_pytest/config.py:325: in _getconftestmodules
return self._path2confmods[path]
E KeyError: local('/apis/db/tests')
During handling of the above exception, another exception occurred:
/usr/local/lib/python3.5/site-packages/_pytest/config.py:356: in _importconftest
return self._conftestpath2mod[conftestpath]
E KeyError: local('/apis/db/tests/conftest.py')
During handling of the above exception, another exception occurred:
/usr/local/lib/python3.5/site-packages/_pytest/config.py:362: in _importconftest
mod = conftestpath.pyimport()
/usr/local/lib/python3.5/site-packages/py/_path/local.py:680: in pyimport
raise self.ImportMismatchError(modname, modfile, self)
_pytest.config.ConftestImportFailure: ImportMismatchError('conftest', '/projects/my_project/db/tests/conftest.py', local('/apis/db/tests/conftest.py'))
/apis - 它是 Dockerfile 中的 WORKDIR。
原文由 A J 发布,翻译遵循 CC BY-SA 4.0 许可协议
我已经通过删除 test/ 目录下的所有 pycache pkg 来修复它,问题是当我创建 docker image 时它也选择了我所有的 pycache 和 *.pyc 文件,在测试运行时它使用我的本地机器路径而不是docker 容器中的路径。
结论:在创建 docker 镜像之前清除你的 *.pyc 和 pycache 文件。