py.test does not correctly import modules from conftest - fails with ConftestImportFailure: ModuleNotFound

Mikko Ohtamaa

pytest fails to import modules even though these modules import fine under the vanilla python interpreter. Especially modules imported from conftest fails to resolve. ImportErrori is raised when pytest tries to rewrite assert information in modules.

An example traceback:

================================================================================================================================================ 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===

How to resolve?

Mikko Ohtamaa

pytest may be picky if you have __init__.py files under your tests folder and subfolders. This may cause pytest to resolve Python modules in incorrect order under certain circumstances.

The workaround is to remove all __init__.py files under tests.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Accessing test file name from conftest.py

Trying to import python modules returns ModuleNotFound Error

How to import modules from py files

Pytest parametrization of test for a list generated from cmdline options in conftest.py

How do I correctly import packages with py.test?

How to retrieve the markers for py.test in conftest.py?

ModuleNotFound error in Python when trying to import nested modules

Javascript modules import fails

Why does __init__.py import all files in the folder as modules?

how to set config before import in pytest conftest.py?

Unable to access methods of WebDriver class when the WebDriver instance is passed to test class from conftest.py (Python + Selenium)

How to put parser.addoption in a test module, not in conftest.py?

Can I use single conftest.py for different test directories?

How to import select objects from various modules into __init__.py?

from ... import OR import ... as for modules

VSCode import "C" fails on modules

How to apply fixture from conftest.py to inner folders only

Is there a way to customise the format for logging in pytest from conftest.py

Import Python file [ModuleNotFound]

How do I get py.test to recognize conftest.py in a subdirectory?

How does "import * as bootstrap from '../node_modules/bootstrap' work?

Webpack does not import bundles from node_modules (js only)

How to import modules to threejs project correctly?

Versioned import in go using modules fails

how to access a json file(test data like config.json) in conftest.py

All my test functions are loading a fixture that is in the conftest.py, even when they don't need it

Import Python Modules without running .Py

Can't import my own .py modules in python when run from apache uwsgi

Calling script.py from c# with process but getting error import modules like cv2