py.test 无法从 conftest 正确导入模块 - 失败并显示 ConftestImportFailure: ModuleNotFound

2024-02-27

pytest无法导入模块,即使这些模块在普通模式下导入正常python口译员。特别是从以下位置导入的模块conftest未能解决。当 pytest 尝试重写模块中的断言信息时,会引发 ImportErrori。

回溯示例:

================================================================================================================================================ test session starts ================================================================================================================================================
platform darwin -- Python 3.6.2, pytest-3.8.1, py-1.5.4, pluggy-0.7.1 -- /yyy/venv/bin/python3.6
cachedir: .pytest_cache
rootdir: /yyy/corporategovernance, inifile: setup.cfg
plugins: timeout-1.3.1, cov-2.5.1, populus-1.9.0, flaky-3.4.0, celery-4.1.0, splinter-1.8.5+headless
collecting 0 items / 1 errors                                                                                                                                                                                                                                                                                       
====================================================================================================================================================== ERRORS =======================================================================================================================================================
_________________________________________________________________________________________________________________________________ ERROR collecting corporategovernance/__init__.py __________________________________________________________________________________________________________________________________
../venv/lib/python3.6/site-packages/_pytest/config/__init__.py:381: in _getconftestmodules
    return self._path2confmods[path]
E   KeyError: local('/xxx/tests')

During handling of the above exception, another exception occurred:
../venv/lib/python3.6/site-packages/_pytest/config/__init__.py:412: in _importconftest
    return self._conftestpath2mod[conftestpath]
E   KeyError: local('/xxx/tests/conftest.py')

During handling of the above exception, another exception occurred:
../venv/lib/python3.6/site-packages/_pytest/config/__init__.py:418: in _importconftest
    mod = conftestpath.pyimport()
../venv/lib/python3.6/site-packages/py/_path/local.py:668: in pyimport
    __import__(modname)
../venv/lib/python3.6/site-packages/_pytest/assertion/rewrite.py:290: in load_module
    six.exec_(co, mod.__dict__)
corporategovernance/tests/conftest.py:4: in <module>
    import corporategovernance.backend
E   ModuleNotFoundError: No module named 'corporategovernance.backend'

During handling of the above exception, another exception occurred:
../venv/lib/python3.6/site-packages/_pytest/runner.py:201: in __init__
    self.result = func()
../venv/lib/python3.6/site-packages/_pytest/runner.py:261: in <lambda>
    call = CallInfo(lambda: list(collector.collect()), "collect")
../venv/lib/python3.6/site-packages/_pytest/python.py:624: in collect
    for x in self._collectfile(path):
../venv/lib/python3.6/site-packages/_pytest/python.py:579: in _collectfile
    ihook = self.gethookproxy(path)
../venv/lib/python3.6/site-packages/_pytest/python.py:568: in gethookproxy
    my_conftestmodules = pm._getconftestmodules(fspath)
../venv/lib/python3.6/site-packages/_pytest/config/__init__.py:395: in _getconftestmodules
    mod = self._importconftest(conftestpath)
../venv/lib/python3.6/site-packages/_pytest/config/__init__.py:431: in _importconftest
    raise ConftestImportFailure(conftestpath, sys.exc_info())
E   _pytest.config.ConftestImportFailure: ModuleNotFoundError("No module named 'corporategovernance.backend'",)
E     File "/yyy/venv/lib/python3.6/site-packages/_pytest/assertion/rewrite.py", line 290, in load_module
E       six.exec_(co, mod.__dict__)
E     File "/xxx/tests/conftest.py", line 4, in <module>
E       import corporategovernance.backend
===Flaky Test Report===

如何解决?


如果你有的话 pytest 可能会很挑剔__init__.py你下的文件tests文件夹和子文件夹。在某些情况下,这可能会导致 pytest 以错误的顺序解析 Python 模块。

解决方法是删除所有__init__.py正在测试的文件。

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

py.test 无法从 conftest 正确导入模块 - 失败并显示 ConftestImportFailure: ModuleNotFound 的相关文章

随机推荐