运行 py.test 时出现错误 ImportMismatchError

2024-05-01

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


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

结论:清除你的*.pyc and __pycache__创建 docker 映像之前的文件。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

运行 py.test 时出现错误 ImportMismatchError 的相关文章

随机推荐