Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use relative imports from conftest.py in test package with --import-mode=importlib #7652

Open
mje-nz opened this issue Aug 15, 2020 · 0 comments
Labels
topic: collection related to the collection phase topic: config related to config handling, argument parsing and config file

Comments

@mje-nz
Copy link

mje-nz commented Aug 15, 2020

With tests outside application code in a tests package containing conftest.py, pytest --import-mode=importlib doesn't import conftest as part of the package.

Example

Files:

test/__init__.py
test/util.py:
    def helper_function():
        pass
test/conftest.py:
    import pytest
    from .util import helper_function
    
    @pytest.fixture
    def common_fixture():
        helper_function()
test/test_example.py:
    def test_function(common_fixture):
        pass

Repro:

$ pytest                        
================================= test session starts =================================
platform darwin -- Python 3.7.3, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: mwe
collected 1 item                                                                                                                                                                    

test/test_example.py .                                                                                                                                                        [100%]

================================== 1 passed in 0.01s ==================================

$ pytest --import-mode=importlib
ImportError while loading conftest 'mwe/test/conftest.py'.
test/conftest.py:3: in <module>
    from .util import helper_function
E   ImportError: attempted relative import with no known parent package

Environment

macOS 10.13.6, Python 3.7.3, pytest 6.0.1

$ pip list                        
Package            Version
------------------ -------
attrs              19.3.0
importlib-metadata 1.7.0
iniconfig          1.0.1
more-itertools     8.4.0
packaging          20.4
pip                20.2.2
pluggy             0.13.1
py                 1.9.0
pyparsing          2.4.7
pytest             6.0.1
setuptools         49.2.0
six                1.15.0
toml               0.10.1
wheel              0.34.2
zipp               3.1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: collection related to the collection phase topic: config related to config handling, argument parsing and config file
Projects
None yet
Development

No branches or pull requests

3 participants