Skip to content

Running tests inside namespace packages ? #2371

Open
@asmodehn

Description

@asmodehn

I am having import issues with namespaces packages and pytest.
Something along the lines of pytest already having the namespace imported and not looking deeper into the subpackage for the actual package (while running the AssertionRewritingHook:find_modules method in rewrite.py)

I am currently trying to reproduce the issue on a minimal example, so I made https://github.com/asmodehn/python_ns_pkgs.

But it seems the test on these installed packages are not even collected...

Envrionment setup :

alexv@AlexV-Linux:~$ git clone https://github.com/asmodehn/python_ns_pkgs
Cloning into 'python_ns_pkgs'...
remote: Counting objects: 25, done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 25 (delta 3), reused 25 (delta 3), pack-reused 0
Unpacking objects: 100% (25/25), done.
Checking connectivity... done.

alexv@AlexV-Linux:~$ mkvirtualenv pytest_nspkgs
New python executable in /home/alexv/.virtualenvs/pytest_nspkgs/bin/python
Installing setuptools, pip, wheel...done.

(pytest_nspkgs) alexv@AlexV-Linux:~$ cd python_ns_pkgs/
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ tree
.
├── foo1
│   ├── foo
│   │   ├── foo
│   │   │   ├── __init__.py
│   │   │   └── test_import.py
│   │   └── __init__.py
│   └── setup.py
└── foo2
    ├── foo
    │   ├── bar
    │   │   ├── __init__.py
    │   │   └── test_import.py
    │   └── __init__.py
    └── setup.py
6 directories, 8 files
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ pip install foo1/
Processing ./foo1
Installing collected packages: foo1
  Running setup.py install for foo1 ... done
Successfully installed foo1-1.0
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ pip install foo2/
Processing ./foo2
Installing collected packages: foo2
  Running setup.py install for foo2 ... done
Successfully installed foo2-1.0
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ ls ~/.virtualenvs/pytest_nspkgs/lib/python2.7/site-packages/foo
bar  foo
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ tree ~/.virtualenvs/pytest_nspkgs/lib/python2.7/site-packages/foo
/home/alexv/.virtualenvs/pytest_nspkgs/lib/python2.7/site-packages/foo
├── bar
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── test_import.py
│   └── test_import.pyc
└── foo
    ├── __init__.py
    ├── __init__.pyc
    ├── test_import.py
    └── test_import.pyc

2 directories, 8 files
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ python -c "from foo import foo, bar"
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ 

The things I attempted with pytest so far :

(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ pytest --pyargs foo.bar
============================================================================================================================= test session starts =============================================================================================================================
platform linux2 -- Python 2.7.6, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: /home/alexv/python_ns_pkgs, inifile:
plugins: hypothesis-3.1.0

======================================================================================================================== no tests ran in 0.00 seconds =========================================================================================================================
ERROR: file or package not found: foo.bar (missing __init__.py?)
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ pytest --pyargs foo.foo
============================================================================================================================= test session starts =============================================================================================================================
platform linux2 -- Python 2.7.6, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: /home/alexv/python_ns_pkgs, inifile:
plugins: hypothesis-3.1.0

======================================================================================================================== no tests ran in 0.00 seconds =========================================================================================================================
ERROR: file or package not found: foo.foo (missing __init__.py?)
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ pytest ~/.virtualenvs/pytest_nspkgs/lib/python2.7/site-packages/foo/foo/test_import.py
============================================================================================================================= test session starts =============================================================================================================================
platform linux2 -- Python 2.7.6, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: /home/alexv, inifile:
plugins: hypothesis-3.1.0
collected 0 items / 1 errors 

=================================================================================================================================== ERRORS ====================================================================================================================================
_______________________________________________________________________________________ ERROR collecting .virtualenvs/pytest_nspkgs/lib/python2.7/site-packages/foo/foo/test_import.py ________________________________________________________________________________________
ImportError while importing test module '/home/alexv/.virtualenvs/pytest_nspkgs/lib/python2.7/site-packages/foo/foo/test_import.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../.virtualenvs/pytest_nspkgs/lib/python2.7/site-packages/foo/foo/test_import.py:1: in <module>
    from foo import foo,bar
E   ImportError: cannot import name foo
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================================================================================== 1 error in 0.11 seconds ===========================================================================================================================
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ pytest ~/.virtualenvs/pytest_nspkgs/lib/python2.7/site-packages/foo/bar/test_import.py
============================================================================================================================= test session starts =============================================================================================================================
platform linux2 -- Python 2.7.6, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: /home/alexv, inifile:
plugins: hypothesis-3.1.0
collected 0 items / 1 errors 

=================================================================================================================================== ERRORS ====================================================================================================================================
_______________________________________________________________________________________ ERROR collecting .virtualenvs/pytest_nspkgs/lib/python2.7/site-packages/foo/bar/test_import.py ________________________________________________________________________________________
ImportError while importing test module '/home/alexv/.virtualenvs/pytest_nspkgs/lib/python2.7/site-packages/foo/bar/test_import.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../.virtualenvs/pytest_nspkgs/lib/python2.7/site-packages/foo/bar/test_import.py:1: in <module>
    from foo import foo,bar
E   ImportError: cannot import name foo
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================================================================================== 1 error in 0.06 seconds ===========================================================================================================================
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ pytest --pyargs foo
============================================================================================================================= test session starts =============================================================================================================================
platform linux2 -- Python 2.7.6, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: /home/alexv/python_ns_pkgs, inifile:
plugins: hypothesis-3.1.0

======================================================================================================================== no tests ran in 0.00 seconds =========================================================================================================================
ERROR: file or package not found: foo (missing __init__.py?)
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ cd foo1/
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs/foo1$ ls foo/
foo  __init__.py
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs/foo1$ python -c "import foo.bar"
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs/foo1$ python -c "import broken"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named broken
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs/foo1$ pytest foo/foo/test_import.py 
============================================================================================================================= test session starts =============================================================================================================================
platform linux2 -- Python 2.7.6, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: /home/alexv/python_ns_pkgs/foo1, inifile:
plugins: hypothesis-3.1.0
collected 0 items / 1 errors 

=================================================================================================================================== ERRORS ====================================================================================================================================
___________________________________________________________________________________________________________________ ERROR collecting foo/foo/test_import.py ___________________________________________________________________________________________________________________
ImportError while importing test module '/home/alexv/python_ns_pkgs/foo1/foo/foo/test_import.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
foo/foo/test_import.py:1: in <module>
    from foo import foo,bar
E   ImportError: cannot import name bar
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================================================================================== 1 error in 0.06 seconds ===========================================================================================================================
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs/foo1$ python -c "from foo import foo, bar"
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs/foo1$ 

then I realize I might have been using an old pytest version, so I ran these :

(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs/foo1$ pip install pytest
Collecting pytest
/home/alexv/.virtualenvs/pytest_nspkgs/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/home/alexv/.virtualenvs/pytest_nspkgs/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Using cached pytest-3.0.7-py2.py3-none-any.whl
Requirement already satisfied: setuptools in /home/alexv/.virtualenvs/pytest_nspkgs/lib/python2.7/site-packages (from pytest)
Collecting py>=1.4.29 (from pytest)
  Using cached py-1.4.33-py2.py3-none-any.whl
Requirement already satisfied: appdirs>=1.4.0 in /home/alexv/.virtualenvs/pytest_nspkgs/lib/python2.7/site-packages (from setuptools->pytest)
Requirement already satisfied: packaging>=16.8 in /home/alexv/.virtualenvs/pytest_nspkgs/lib/python2.7/site-packages (from setuptools->pytest)
Requirement already satisfied: six>=1.6.0 in /home/alexv/.virtualenvs/pytest_nspkgs/lib/python2.7/site-packages (from setuptools->pytest)
Requirement already satisfied: pyparsing in /home/alexv/.virtualenvs/pytest_nspkgs/lib/python2.7/site-packages (from packaging>=16.8->setuptools->pytest)
Installing collected packages: py, pytest
Successfully installed py-1.4.33 pytest-3.0.7
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs/foo1$ which pytest
/home/alexv/.virtualenvs/pytest_nspkgs/bin/pytest
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs/foo1$ pytest --pyargs foo
============================================================================================================================= test session starts =============================================================================================================================
platform linux2 -- Python 2.7.6, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: /home/alexv/python_ns_pkgs/foo1, inifile:
plugins: hypothesis-3.1.0
collected 0 items / 1 errors 

=================================================================================================================================== ERRORS ====================================================================================================================================
___________________________________________________________________________________________________________________ ERROR collecting foo/foo/test_import.py ___________________________________________________________________________________________________________________
ImportError while importing test module '/home/alexv/python_ns_pkgs/foo1/foo/foo/test_import.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
foo/foo/test_import.py:1: in <module>
    from foo import foo,bar
E   ImportError: cannot import name bar
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================================================================================== 1 error in 0.06 seconds ===========================================================================================================================
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs/foo1$ cd ..
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ pytest --pyargs foo
============================================================================================================================= test session starts =============================================================================================================================
platform linux2 -- Python 2.7.6, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: /home/alexv/python_ns_pkgs, inifile:
plugins: hypothesis-3.1.0

======================================================================================================================== no tests ran in 0.00 seconds =========================================================================================================================
ERROR: file or package not found: foo (missing __init__.py?)
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ pytest --pyargs foo.bar
============================================================================================================================= test session starts =============================================================================================================================
platform linux2 -- Python 2.7.6, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: /home/alexv/python_ns_pkgs, inifile:
plugins: hypothesis-3.1.0

======================================================================================================================== no tests ran in 0.00 seconds =========================================================================================================================
ERROR: file or package not found: foo.bar (missing __init__.py?)
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ pytest --pyargs foo.foo
============================================================================================================================= test session starts =============================================================================================================================
platform linux2 -- Python 2.7.6, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: /home/alexv/python_ns_pkgs, inifile:
plugins: hypothesis-3.1.0

======================================================================================================================== no tests ran in 0.00 seconds =========================================================================================================================
ERROR: file or package not found: foo.foo (missing __init__.py?)
(pytest_nspkgs) alexv@AlexV-Linux:~/python_ns_pkgs$ 

But still no success...

So

  • How can I run these test modules ?
  • Why the packages foo.foo and foo.bar are not found even though python finds them ?
  • Does pytest support running tests from installed namespace packages at all ?

Thanks to shed some light on these...

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: collectionrelated to the collection phasetype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions