Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/woutdenolf/pytest_pep420
Test pytest for implicit namespace packages
https://github.com/woutdenolf/pytest_pep420
Last synced: 4 days ago
JSON representation
Test pytest for implicit namespace packages
- Host: GitHub
- URL: https://github.com/woutdenolf/pytest_pep420
- Owner: woutdenolf
- Created: 2024-03-24T10:09:01.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-03-26T20:13:26.000Z (8 months ago)
- Last Synced: 2024-10-13T01:42:57.540Z (about 1 month ago)
- Language: Python
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Pytest and implicit namespace packages
## Implicit namespace packages (PEP 420)
The default way of defining namespace packages in python
https://packaging.python.org/en/latest/guides/packaging-namespace-packages/#native-namespace-packages
```
.
├── pyproject.toml
├── setup.cfg
└── src
├── mynamespace
│ └── myproject
│ ├── __init__.py
│ └── tests
│ ├── __init__.py
│ └── test_myproject_ns.py
└── myproject
├── __init__.py
└── tests
├── __init__.py
└── test_myproject.py
```The package `mynamespace` is a namespace package since the directory `./src/mynamespace/myproject`
does NOT have a `__init__.py` file and `setup.cfg` has```
[options]
package_dir=
=src
packages=find_namespace:
```## Pytest cannot handle this by default
This fails
```bash
pytest====================================== ERRORS ======================================
______________ ERROR collecting src/myproject/tests/test_myproject.py ______________
ImportError while importing test module '/home/denolf/projects/pytest_pep420/src/myproject/tests/test_myproject.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../.pyenv/versions/3.11.8/lib/python3.11/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
E ModuleNotFoundError: No module named 'myproject.tests.test_myproject'
============================= short test summary info ==============================
ERROR src/myproject/tests/test_myproject.py
!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!
================================= 1 error in 0.06s =================================
```Using the `importlib` import mode solves the issue but has limitations (test modules
are non-importable by each other, utility modules in the tests directories are not
automatically importable)```bash
pytest --import-mode=importlibsrc/mynamespace/myproject/tests/test_myproject_ns.py . [ 50%]
src/myproject/tests/test_myproject.py . [100%]=============================== 2 passed in 0.01s ===============================
```## consider_namespace_packages
Adding this option to `pyproject.toml` solves the issue:
https://docs.pytest.org/en/stable/reference/reference.html#confval-consider_namespace_packages
```bash
(pybox_9S5crM) :~/tmp/pytest_pep420$ pytest -x
======================================== test session starts =========================================
platform linux -- Python 3.11.8, pytest-8.1.1, pluggy-1.4.0
rootdir: /home/denolf/tmp/pytest_pep420
configfile: pyproject.toml
collected 2 itemssrc/mynamespace/myproject/tests/test_myproject_ns.py . [ 50%]
src/myproject/tests/test_myproject.py . [100%]========================================= 2 passed in 0.01s ==========================================
```