运行py.test时出现错误ImportMismatchError [英] Getting error ImportMismatchError while running py.test

查看:635
本文介绍了运行py.test时出现错误ImportMismatchError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在本地运行测试时,它的工作正常,但是在创建docker并在容器内运行后,我得到了错误提示。 $ c> /usr/local/lib/python3.5/site-packages/_pytest/config.py:325:在_getconftestmodules中
返回self._path2confmods [path]
E KeyError:local('/ apis / db / tests')

在处理上述异常期间,发生了另一个异常:
/usr/local/lib/python3.5/site-packages/_pytest/config.py :356:在_importconftest
中返回self._conftestpath2mod [conftestpath]
E KeyError:local('/ apis / db / tests / conftest.py')

上面的异常,发生了另一个异常:
/usr/local/lib/python3.5/site-packages/_pytest/config.py:362:在_importconftest
mod = conftestpath.pyimport()
/usr/local/lib/python3.5/site-packages/py/_path/local.py:680:在pyimport
中提高self.ImportMismatchError(modname,modfile,self)

_pytest.config.ConftestImp ortFailure:ImportMismatchError('conftest','/projects/my_project/db/tests/conftest.py',local('/ apis / db / tests / conftest.py'))

/ apis-Dockerfile中的WORKDIR。

解决方案

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



结论:清除您的 *。pyc __ pycache __ 文件,然后再创建Docker映像。


When I am running tests locally its working fine, but after creating the docker and running inside the container I am getting below error.

    /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 - its the WORKDIR in Dockerfile.

解决方案

I have fixed it by removing all __pycache__ pkg under test/ directory, the issue was when I was creating docker image its picking all my __pycache__ and *.pyc files too, at the time when test are running its using my local machine path instead of the path in docker container.

Conclusion: Clear your *.pyc and __pycache__ files before creating a docker image.

这篇关于运行py.test时出现错误ImportMismatchError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆