运行 py.test 时出现错误 ImportMismatchError

新手上路,请多包涵

当我在本地运行测试时,它工作正常,但在创建 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 许可协议

阅读 864
1 个回答

我已经通过删除 test/ 目录下的所有 pycache pkg 来修复它,问题是当我创建 docker image 时它也选择了我所有的 pycache*.pyc 文件,在测试运行时它使用我的本地机器路径而不是docker 容器中的路径。

结论:在创建 docker 镜像之前清除你的 *.pycpycache 文件。

原文由 A J 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题