运行py.test时出现错误ImportMismatchError

当我在本地运行testing它的工作正常,但是在创builddocker并在容器内部运行后,我得到下面的错误。

/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。

我已经通过删除testing/目录下的所有pycache pkg来解决这个问题,问题是当我创builddocker镜像的时候,它也select了我所有的pycache和* .pyc文件,在testing运行时使用我的本地机器path而不是docker集装箱中的path。

结论:在创buildDocker镜像之前清除* .pyc和pycache文件。

HTH