ionelmc / cookiecutter-pylibrary

Enhanced cookiecutter template for Python libraries.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pytest command does not work without workarounds

parisni opened this issue · comments

hi,

I installed a python-nameless project with pytest support. tox works out of the box for testing.

However, running pytest does not (see below). The only way to make it work is by running PYTHONPATH=src pytest or by adding an empty conftest.py into src. BTW, I cannot figure out why tox is running pytest without trouble.

Thanks for the explanations

(cookiecutter)~/git/python-nameless$ pytest tests/

============================================================================================================= test session starts =============================================================================================================
platform linux -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /home/me/git/python-nameless, configfile: setup.cfg
collected 0 items / 2 errors                                                                                                                                                                                                                  

=================================================================================================================== ERRORS ====================================================================================================================
___________________________________________________________________________________________________ ERROR collecting tests/test_nameless.py ___________________________________________________________________________________________________
tests/test_nameless.py:2: in <module>
    from nameless.cli import main
E   ModuleNotFoundError: No module named 'nameless'
___________________________________________________________________________________________________ ERROR collecting tests/test_nameless.py ___________________________________________________________________________________________________
ImportError while importing test module '/home/me/git/python-nameless/tests/test_nameless.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_nameless.py:2: in <module>
    from nameless.cli import main
E   ModuleNotFoundError: No module named 'nameless'
=========================================================================================================== short test summary info ===========================================================================================================
ERROR tests/test_nameless.py - ModuleNotFoundError: No module named 'nameless'
ERROR tests/test_nameless.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================================================== 2 errors in 0.12s ==============================================================================================================

This is intentional - you are prevented to run the code without installing it. I wrote about this here https://blog.ionelmc.ro/2014/05/25/python-packaging/

If you'd like to work virtualenv-style on your project then perhaps run something alone these lines?

source .tox/py38/bin/activate
pytest ...

then what trick in the tox.ini makes pytest working then ? I cannot figure out. For example, PYTHONPATH=tests shouldn't help

It installs your code in the virtualenv (eg .tox/py38). This a bit more complicated but it could be summed up as python setup.py sdist && .tox/py38/bin/pip install .tox/dists/project-0.1.tar.gz or something like that.

Where are you going with this? What's the actual problem you're having?

well, i am trying to reproduce your config on one on my existing project (moving the project_name into src/project_name) and so far my pytests are not running as smoth as before. Anyway thanks for your efforts/time ! This is a hot topic.

Well you need to install the code into the virtualenv you're gonna tests with. At the very least a venv/bin/python setup.py install. Or just use tox?